Client and backend. This class is shared: the same code is compiled into your app and into your server, so everything on this page works in both.

public final class Duration

  1. Object
  2. Duration

ImplementsComparable<Duration>, TemporalAmount

An amount of time in seconds and nanoseconds, such as 34.5 seconds.

Arithmetic is exact: a result longer than a Duration holds throws ArithmeticException rather than wrapping around, as the JDK’s does.

Fields

public static final Duration ZEROA duration of zero.

Methods

public static Duration ofDays(long days)A duration of days standard 24 hour days.
public static Duration ofHours(long hours)A duration of hours hours.
public static Duration ofMinutes(long minutes)A duration of minutes minutes.
public static Duration ofSeconds(long seconds)A duration of seconds seconds.
public static Duration ofSeconds(long seconds, long nanoAdjustment)A duration of seconds seconds adjusted by nanoAdjustment nanoseconds, which may take it across any number of seconds.
public static Duration ofMillis(long millis)A duration of millis milliseconds.
public static Duration ofNanos(long nanos)A duration of nanos nanoseconds.
public static Duration of(long amount, TemporalUnit unit)A duration of amount of unit.
public static Duration from(TemporalAmount amount)The duration amount adds up to.
public static Duration between(Temporal startInclusive, Temporal endExclusive)The duration from one value to another of the same kind, negative when endExclusive comes first.
public static Duration parse(CharSequence text)
public long get(TemporalUnit unit)The amount of unit held: the seconds or the nanoseconds.
public List<TemporalUnit> getUnits()Seconds and nanoseconds, the two units a duration is made of.
public Temporal addTo(Temporal temporal)A zero duration hands temporal back untouched, null included: JDK 8, the conformance host, does the same and throws nothing.
public Temporal subtractFrom(Temporal temporal)temporal moved back by this amount.
public boolean isZero()Whether this is zero long.
public boolean isNegative()Whether this is shorter than zero.
public long getSeconds()The whole seconds, rounded towards negative infinity.
public int getNano()The nanoseconds within the second, 0 to 999,999,999.
public Duration withSeconds(long seconds)This duration with seconds whole seconds.
public Duration withNanos(int nanoOfSecond)This duration with nanoOfSecond nanoseconds within the second.
public Duration plus(Duration duration)
public Duration plus(long amountToAdd, TemporalUnit unit)This duration plus amountToAdd of unit, which must have an exact length.
public Duration plusDays(long daysToAdd)
public Duration plusHours(long hoursToAdd)
public Duration plusMinutes(long minutesToAdd)
public Duration plusSeconds(long secondsToAdd)
public Duration plusMillis(long millisToAdd)
public Duration plusNanos(long nanosToAdd)
public Duration minus(Duration duration)
public Duration minus(long amountToSubtract, TemporalUnit unit)
public Duration minusDays(long daysToSubtract)
public Duration minusHours(long hoursToSubtract)
public Duration minusMinutes(long minutesToSubtract)
public Duration minusSeconds(long secondsToSubtract)
public Duration minusMillis(long millisToSubtract)
public Duration minusNanos(long nanosToSubtract)
public Duration multipliedBy(long multiplicand)This duration multiplied by multiplicand, or ArithmeticException when the product is longer than a duration holds.
public Duration dividedBy(long divisor)This duration divided by divisor, the fraction of a nanosecond dropped towards zero.
public Duration negated()This duration with its sign reversed.
public Duration abs()This duration without its sign.
public long toDays()The whole days, of 24 hours.
public long toHours()The whole hours.
public long toMinutes()The whole minutes.
public long toSeconds()The whole seconds; the same as getSeconds().
public long toMillis()The whole milliseconds, or ArithmeticException past what a long holds.
public long toNanos()The whole nanoseconds, or ArithmeticException past what a long holds (about 292 years).
public long toDaysPart()The whole days; the same as toDays().
public int toHoursPart()The hours left over after the whole days.
public int toMinutesPart()The minutes left over after the whole hours.
public int toSecondsPart()The seconds left over after the whole minutes.
public int toMillisPart()The milliseconds within the second.
public int toNanosPart()The nanoseconds within the second; the same as getNano().
public int compareTo(Duration other)Compares this object to the specified object to determine their relative order.
public boolean equals(Object obj)Indicates whether some other object is “equal to” this one.
public int hashCode()Returns a hash code value for the object.
public String toString()Returns a string representation of the object.

Inherited methods

Field details

ZERO

public static final Duration ZERO
A duration of zero.

Method details

ofDays

public static Duration ofDays(long days)
A duration of days standard 24 hour days.

ofHours

public static Duration ofHours(long hours)
A duration of hours hours.

ofMinutes

public static Duration ofMinutes(long minutes)
A duration of minutes minutes.

ofSeconds

public static Duration ofSeconds(long seconds)
A duration of seconds seconds.

ofSeconds

public static Duration ofSeconds(long seconds, long nanoAdjustment)
A duration of seconds seconds adjusted by nanoAdjustment nanoseconds, which may take it across any number of seconds.

ofMillis

public static Duration ofMillis(long millis)
A duration of millis milliseconds.

ofNanos

public static Duration ofNanos(long nanos)
A duration of nanos nanoseconds.

of

public static Duration of(long amount, TemporalUnit unit)
A duration of amount of unit. The unit must have an exact length: a day or anything shorter.

from

public static Duration from(TemporalAmount amount)
The duration amount adds up to. Every unit it is made of must have an exact length.

between

public static Duration between(Temporal startInclusive, Temporal endExclusive)
The duration from one value to another of the same kind, negative when endExclusive comes first.

parse

public static Duration parse(CharSequence text)

get

public long get(TemporalUnit unit)
The amount of unit held: the seconds or the nanoseconds.

getUnits

public List<TemporalUnit> getUnits()
Seconds and nanoseconds, the two units a duration is made of.

addTo

public Temporal addTo(Temporal temporal)
A zero duration hands temporal back untouched, null included: JDK 8, the conformance host, does the same and throws nothing.

subtractFrom

public Temporal subtractFrom(Temporal temporal)
temporal moved back by this amount.

isZero

public boolean isZero()
Whether this is zero long.

isNegative

public boolean isNegative()
Whether this is shorter than zero.

getSeconds

public long getSeconds()
The whole seconds, rounded towards negative infinity.

getNano

public int getNano()
The nanoseconds within the second, 0 to 999,999,999.

withSeconds

public Duration withSeconds(long seconds)
This duration with seconds whole seconds.

withNanos

public Duration withNanos(int nanoOfSecond)
This duration with nanoOfSecond nanoseconds within the second.

plus

public Duration plus(Duration duration)

plus

public Duration plus(long amountToAdd, TemporalUnit unit)
This duration plus amountToAdd of unit, which must have an exact length.

plusDays

public Duration plusDays(long daysToAdd)

plusHours

public Duration plusHours(long hoursToAdd)

plusMinutes

public Duration plusMinutes(long minutesToAdd)

plusSeconds

public Duration plusSeconds(long secondsToAdd)

plusMillis

public Duration plusMillis(long millisToAdd)

plusNanos

public Duration plusNanos(long nanosToAdd)

minus

public Duration minus(Duration duration)

minus

public Duration minus(long amountToSubtract, TemporalUnit unit)

minusDays

public Duration minusDays(long daysToSubtract)

minusHours

public Duration minusHours(long hoursToSubtract)

minusMinutes

public Duration minusMinutes(long minutesToSubtract)

minusSeconds

public Duration minusSeconds(long secondsToSubtract)

minusMillis

public Duration minusMillis(long millisToSubtract)

minusNanos

public Duration minusNanos(long nanosToSubtract)

multipliedBy

public Duration multipliedBy(long multiplicand)
This duration multiplied by multiplicand, or ArithmeticException when the product is longer than a duration holds.

dividedBy

public Duration dividedBy(long divisor)
This duration divided by divisor, the fraction of a nanosecond dropped towards zero. Exact for every duration; only a quotient that is not a duration throws ArithmeticException.

negated

public Duration negated()
This duration with its sign reversed. The most negative duration has no positive counterpart and throws ArithmeticException.

abs

public Duration abs()
This duration without its sign.

toDays

public long toDays()
The whole days, of 24 hours.

toHours

public long toHours()
The whole hours.

toMinutes

public long toMinutes()
The whole minutes.

toSeconds

public long toSeconds()
The whole seconds; the same as getSeconds().

toMillis

public long toMillis()
The whole milliseconds, or ArithmeticException past what a long holds.

toNanos

public long toNanos()
The whole nanoseconds, or ArithmeticException past what a long holds (about 292 years).

toDaysPart

public long toDaysPart()
The whole days; the same as toDays().

toHoursPart

public int toHoursPart()
The hours left over after the whole days.

toMinutesPart

public int toMinutesPart()
The minutes left over after the whole hours.

toSecondsPart

public int toSecondsPart()
The seconds left over after the whole minutes.

toMillisPart

public int toMillisPart()
The milliseconds within the second.

toNanosPart

public int toNanosPart()
The nanoseconds within the second; the same as getNano().

compareTo

public int compareTo(Duration other)
Compares this object to the specified object to determine their relative order.

Parameters

other Duration
the object to compare to this instance.

Returns

a negative integer if this instance is less than another; a positive integer if this instance is greater than another; 0 if this instance has the same order as another.

Throws

ClassCastException
if another cannot be converted into something comparable to this instance.

equals

public boolean equals(Object obj)
Indicates whether some other object is “equal to” this one. The equals method implements an equivalence relation: It is reflexive: for any reference value x, x.equals(x) should return true. It is symmetric: for any reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true. It is transitive: for any reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true. It is consistent: for any reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the object is modified. For any non-null reference value x, x.equals(null) should return false. The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any reference values x and y, this method returns true if and only if x and y refer to the same object (x==y has the value true).

hashCode

public int hashCode()
Returns a hash code value for the object. This method is supported for the benefit of hashtables such as those provided by java.util.Hashtable. The general contract of hashCode is: Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application. If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result. It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables. As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)

toString

public String toString()
Returns a string representation of the object. In general, the toString method returns a string that “textually represents” this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method. The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@’, and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of: getClass().getName() + ‘@’ + Integer.toHexString(hashCode())