public final class Duration extends Object implements TemporalAdder, TemporalSubtractor, Comparable<Duration>, Serializable
This class models a duration of time and is not tied to any instant. The model is of a directed duration, meaning that the duration may be negative.
A physical duration could be of infinite length.
For practicality, the duration is stored with constraints similar to Instant.
The duration uses nanosecond resolution with a maximum value of the seconds that can
be held in a long. This is greater than the current estimated age of the universe.
The range of a duration requires the storage of a number larger than a long.
To achieve this, the class stores a long representing seconds and an int
representing nanosecond-of-second, which will always be between 0 and 999,999,999.
The duration is measured in "seconds", but these are not necessarily identical to
the scientific "SI second" definition based on atomic clocks.
This difference only impacts durations measured near a leap-second and should not affect
most applications.
See Instant for a discussion as to the meaning of the second and time-scales.
| Modifier and Type | Field and Description |
|---|---|
static Duration |
ZERO
Constant for a duration of zero.
|
| Modifier and Type | Method and Description |
|---|---|
Duration |
abs()
Returns a copy of this duration with a positive length.
|
Temporal |
addTo(Temporal temporal)
Adds this duration to the specified temporal object.
|
static Duration |
between(TemporalAccessor startInclusive,
TemporalAccessor endExclusive)
Obtains an instance of
Duration representing the duration between two instants. |
int |
compareTo(Duration otherDuration)
Compares this duration to the specified
Duration. |
Duration |
dividedBy(long divisor)
Returns a copy of this duration divided by the specified value.
|
boolean |
equals(Object otherDuration)
Checks if this duration is equal to the specified
Duration. |
int |
getNano()
Gets the number of nanoseconds within the second in this duration.
|
long |
getSeconds()
Gets the number of seconds in this duration.
|
int |
hashCode()
A hash code for this duration.
|
boolean |
isGreaterThan(Duration otherDuration)
Checks if this duration is greater than the specified
Duration. |
boolean |
isLessThan(Duration otherDuration)
Checks if this duration is less than the specified
Duration. |
boolean |
isNegative()
Checks if this duration is negative, excluding zero.
|
boolean |
isPositive()
Checks if this duration is positive, excluding zero.
|
boolean |
isZero()
Checks if this duration is zero length.
|
Duration |
minus(Duration duration)
Returns a copy of this duration with the specified duration subtracted.
|
Duration |
minus(long amountToSubtract,
TemporalUnit unit)
Returns a copy of this duration with the specified duration subtracted.
|
Duration |
minusMillis(long millisToSubtract)
Returns a copy of this duration with the specified duration in milliseconds subtracted.
|
Duration |
minusNanos(long nanosToSubtract)
Returns a copy of this duration with the specified duration in nanoseconds subtracted.
|
Duration |
minusSeconds(long secondsToSubtract)
Returns a copy of this duration with the specified duration in seconds subtracted.
|
Duration |
multipliedBy(long multiplicand)
Returns a copy of this duration multiplied by the scalar.
|
Duration |
negated()
Returns a copy of this duration with the length negated.
|
static Duration |
of(long amount,
TemporalUnit unit)
Obtains an instance of
Duration from a duration in the specified unit. |
static Duration |
ofDays(long days)
Obtains an instance of
Duration from a number of standard 24 hour days. |
static Duration |
ofHours(long hours)
Obtains an instance of
Duration from a number of standard length hours. |
static Duration |
ofMillis(long millis)
Obtains an instance of
Duration from a number of milliseconds. |
static Duration |
ofMinutes(long minutes)
Obtains an instance of
Duration from a number of standard length minutes. |
static Duration |
ofNanos(long nanos)
Obtains an instance of
Duration from a number of nanoseconds. |
static Duration |
ofSeconds(long seconds)
Obtains an instance of
Duration from a number of seconds. |
static Duration |
ofSeconds(long seconds,
long nanoAdjustment)
Obtains an instance of
Duration from a number of seconds
and an adjustment in nanoseconds. |
static Duration |
parse(CharSequence text)
Obtains an instance of
Duration by parsing a text string. |
Duration |
plus(Duration duration)
Returns a copy of this duration with the specified duration added.
|
Duration |
plus(long amountToAdd,
TemporalUnit unit)
Returns a copy of this duration with the specified duration added.
|
Duration |
plusMillis(long millisToAdd)
Returns a copy of this duration with the specified duration in milliseconds added.
|
Duration |
plusNanos(long nanosToAdd)
Returns a copy of this duration with the specified duration in nanoseconds added.
|
Duration |
plusSeconds(long secondsToAdd)
Returns a copy of this duration with the specified duration in seconds added.
|
Temporal |
subtractFrom(Temporal temporal)
Subtracts this duration from the specified temporal object.
|
long |
toMillis()
Converts this duration to the total length in milliseconds.
|
long |
toNanos()
Converts this duration to the total length in nanoseconds expressed as a
long. |
String |
toString()
A string representation of this duration using ISO-8601 seconds
based representation, such as
PT12.345S. |
public static Duration ofSeconds(long seconds)
Duration from a number of seconds.
The nanosecond in second field is set to zero.
seconds - the number of seconds, positive or negativeDuration, not nullpublic static Duration ofSeconds(long seconds, long nanoAdjustment)
Duration from a number of seconds
and an adjustment in nanoseconds.
This method allows an arbitrary number of nanoseconds to be passed in. The factory will alter the values of the second and nanosecond in order to ensure that the stored nanosecond is in the range 0 to 999,999,999. For example, the following will result in the exactly the same duration:
Duration.ofSeconds(3, 1); Duration.ofSeconds(4, -999_999_999); Duration.ofSeconds(2, 1000_000_001);
seconds - the number of seconds, positive or negativenanoAdjustment - the nanosecond adjustment to the number of seconds, positive or negativeDuration, not nullArithmeticException - if the adjustment causes the seconds to exceed the capacity of Durationpublic static Duration ofMillis(long millis)
Duration from a number of milliseconds.
The seconds and nanoseconds are extracted from the specified milliseconds.
millis - the number of milliseconds, positive or negativeDuration, not nullpublic static Duration ofNanos(long nanos)
Duration from a number of nanoseconds.
The seconds and nanoseconds are extracted from the specified nanoseconds.
nanos - the number of nanoseconds, positive or negativeDuration, not nullpublic static Duration ofMinutes(long minutes)
Duration from a number of standard length minutes.
The seconds are calculated based on the standard definition of a minute, where each minute is 60 seconds. The nanosecond in second field is set to zero.
minutes - the number of minutes, positive or negativeDuration, not nullArithmeticException - if the input minutes exceeds the capacity of Durationpublic static Duration ofHours(long hours)
Duration from a number of standard length hours.
The seconds are calculated based on the standard definition of an hour, where each hour is 3600 seconds. The nanosecond in second field is set to zero.
hours - the number of hours, positive or negativeDuration, not nullArithmeticException - if the input hours exceeds the capacity of Durationpublic static Duration ofDays(long days)
Duration from a number of standard 24 hour days.
The seconds are calculated based on the standard definition of a day, where each day is 86400 seconds which implies a 24 hour day. The nanosecond in second field is set to zero.
days - the number of days, positive or negativeDuration, not nullArithmeticException - if the input days exceeds the capacity of Durationpublic static Duration of(long amount, TemporalUnit unit)
Duration from a duration in the specified unit.
The parameters represent the two parts of a phrase like '6 Hours'. For example:
Duration.of(3, SECONDS); Duration.of(465, HOURS);Only a subset of units are accepted by this method. The unit must either have an
exact duration or
be ChronoUnit.DAYS which is treated as 24 hours. Other units throw an exception.amount - the amount of the duration, measured in terms of the unit, positive or negativeunit - the unit that the duration is measured in, must have an exact duration, not nullDuration, not nullDateTimeException - if the period unit has an estimated durationArithmeticException - if a numeric overflow occurspublic static Duration between(TemporalAccessor startInclusive, TemporalAccessor endExclusive)
Duration representing the duration between two instants.
A Duration represents a directed distance between two points on the time-line.
As such, this method will return a negative duration if the end is before the start.
To guarantee to obtain a positive duration call abs() on the result of this factory.
startInclusive - the start instant, inclusive, not nullendExclusive - the end instant, exclusive, not nullDuration, not nullArithmeticException - if the calculation exceeds the capacity of Durationpublic static Duration parse(CharSequence text)
Duration by parsing a text string.
This will parse the string produced by toString() which is
the ISO-8601 format PTnS where n is
the number of seconds with optional decimal part.
The number must consist of ASCII numerals.
There must only be a negative sign at the start of the number and it can
only be present if the value is less than zero.
There must be at least one digit before any decimal point.
There must be between 1 and 9 inclusive digits after any decimal point.
The letters (P, T and S) will be accepted in upper or lower case.
The decimal point may be either a dot or a comma.
text - the text to parse, not nullDuration, not nullDateTimeParseException - if the text cannot be parsed to a Durationpublic boolean isZero()
A Duration represents a directed distance between two points on
the time-line and can therefore be positive, zero or negative.
This method checks whether the length is zero.
public boolean isPositive()
A Duration represents a directed distance between two points on
the time-line and can therefore be positive, zero or negative.
This method checks whether the length is greater than zero.
public boolean isNegative()
A Duration represents a directed distance between two points on
the time-line and can therefore be positive, zero or negative.
This method checks whether the length is less than zero.
public long getSeconds()
The length of the duration is stored using two fields - seconds and nanoseconds.
The nanoseconds part is a value from 0 to 999,999,999 that is an adjustment to
the length in seconds.
The total duration is defined by calling this method and getNano().
A Duration represents a directed distance between two points on the time-line.
A negative duration is expressed by the negative sign of the seconds part.
A duration of -1 nanosecond is stored as -1 seconds plus 999,999,999 nanoseconds.
public int getNano()
The length of the duration is stored using two fields - seconds and nanoseconds.
The nanoseconds part is a value from 0 to 999,999,999 that is an adjustment to
the length in seconds.
The total duration is defined by calling this method and getSeconds().
A Duration represents a directed distance between two points on the time-line.
A negative duration is expressed by the negative sign of the seconds part.
A duration of -1 nanosecond is stored as -1 seconds plus 999,999,999 nanoseconds.
public Duration plus(Duration duration)
This instance is immutable and unaffected by this method call.
duration - the duration to add, positive or negative, not nullDuration based on this duration with the specified duration added, not nullArithmeticException - if numeric overflow occurspublic Duration plus(long amountToAdd, TemporalUnit unit)
The duration amount is measured in terms of the specified unit.
Only a subset of units are accepted by this method.
The unit must either have an exact duration or
be ChronoUnit.DAYS which is treated as 24 hours. Other units throw an exception.
This instance is immutable and unaffected by this method call.
amountToAdd - the amount of the period, measured in terms of the unit, positive or negativeunit - the unit that the period is measured in, must have an exact duration, not nullDuration based on this duration with the specified duration added, not nullArithmeticException - if numeric overflow occurspublic Duration plusSeconds(long secondsToAdd)
This instance is immutable and unaffected by this method call.
secondsToAdd - the seconds to add, positive or negativeDuration based on this duration with the specified seconds added, not nullArithmeticException - if numeric overflow occurspublic Duration plusMillis(long millisToAdd)
This instance is immutable and unaffected by this method call.
millisToAdd - the milliseconds to add, positive or negativeDuration based on this duration with the specified milliseconds added, not nullArithmeticException - if numeric overflow occurspublic Duration plusNanos(long nanosToAdd)
This instance is immutable and unaffected by this method call.
nanosToAdd - the nanoseconds to add, positive or negativeDuration based on this duration with the specified nanoseconds added, not nullArithmeticException - if numeric overflow occurspublic Duration minus(Duration duration)
This instance is immutable and unaffected by this method call.
duration - the duration to subtract, positive or negative, not nullDuration based on this duration with the specified duration subtracted, not nullArithmeticException - if numeric overflow occurspublic Duration minus(long amountToSubtract, TemporalUnit unit)
The duration amount is measured in terms of the specified unit.
Only a subset of units are accepted by this method.
The unit must either have an exact duration or
be ChronoUnit.DAYS which is treated as 24 hours. Other units throw an exception.
This instance is immutable and unaffected by this method call.
amountToSubtract - the amount of the period, measured in terms of the unit, positive or negativeunit - the unit that the period is measured in, must have an exact duration, not nullDuration based on this duration with the specified duration subtracted, not nullArithmeticException - if numeric overflow occurspublic Duration minusSeconds(long secondsToSubtract)
This instance is immutable and unaffected by this method call.
secondsToSubtract - the seconds to subtract, positive or negativeDuration based on this duration with the specified seconds subtracted, not nullArithmeticException - if numeric overflow occurspublic Duration minusMillis(long millisToSubtract)
This instance is immutable and unaffected by this method call.
millisToSubtract - the milliseconds to subtract, positive or negativeDuration based on this duration with the specified milliseconds subtracted, not nullArithmeticException - if numeric overflow occurspublic Duration minusNanos(long nanosToSubtract)
This instance is immutable and unaffected by this method call.
nanosToSubtract - the nanoseconds to subtract, positive or negativeDuration based on this duration with the specified nanoseconds subtracted, not nullArithmeticException - if numeric overflow occurspublic Duration multipliedBy(long multiplicand)
This instance is immutable and unaffected by this method call.
multiplicand - the value to multiply the duration by, positive or negativeDuration based on this duration multiplied by the specified scalar, not nullArithmeticException - if numeric overflow occurspublic Duration dividedBy(long divisor)
This instance is immutable and unaffected by this method call.
divisor - the value to divide the duration by, positive or negative, not zeroDuration based on this duration divided by the specified divisor, not nullArithmeticException - if the divisor is zeroArithmeticException - if numeric overflow occurspublic Duration negated()
This method swaps the sign of the total length of this duration.
For example, PT1.3S will be returned as PT-1.3S.
This instance is immutable and unaffected by this method call.
Duration based on this duration with the amount negated, not nullArithmeticException - if numeric overflow occurspublic Duration abs()
This method returns a positive duration by effectively removing the sign from any negative total length.
For example, PT-1.3S will be returned as PT1.3S.
This instance is immutable and unaffected by this method call.
Duration based on this duration with an absolute length, not nullArithmeticException - if numeric overflow occurspublic Temporal addTo(Temporal temporal)
This returns a temporal object of the same observable type as the input with this duration added.
In most cases, it is clearer to reverse the calling pattern by using
Temporal.plus(TemporalAdder).
// these two lines are equivalent, but the second approach is recommended dateTime = thisDuration.addTo(dateTime); dateTime = dateTime.plus(thisDuration);
A Duration can only be added to a Temporal that
represents an instant and can supply ChronoField.INSTANT_SECONDS.
This instance is immutable and unaffected by this method call.
addTo in interface TemporalAddertemporal - the temporal object to adjust, not nullDateTimeException - if unable to addArithmeticException - if numeric overflow occurspublic Temporal subtractFrom(Temporal temporal)
This returns a temporal object of the same observable type as the input with this duration subtracted.
In most cases, it is clearer to reverse the calling pattern by using
Temporal.minus(TemporalSubtractor).
// these two lines are equivalent, but the second approach is recommended dateTime = thisDuration.subtractFrom(dateTime); dateTime = dateTime.minus(thisDuration);
A Duration can only be subtracted from a Temporal that
represents an instant and can supply ChronoField.INSTANT_SECONDS.
This instance is immutable and unaffected by this method call.
subtractFrom in interface TemporalSubtractortemporal - the temporal object to adjust, not nullDateTimeException - if unable to subtractArithmeticException - if numeric overflow occurspublic long toMillis()
If this duration is too large to fit in a long milliseconds, then an
exception is thrown.
If this duration has greater than millisecond precision, then the conversion will drop any excess precision information as though the amount in nanoseconds was subject to integer division by one million.
ArithmeticException - if numeric overflow occurspublic long toNanos()
long.
If this duration is too large to fit in a long nanoseconds, then an
exception is thrown.
ArithmeticException - if numeric overflow occurspublic int compareTo(Duration otherDuration)
Duration.
The comparison is based on the total length of the durations.
It is "consistent with equals", as defined by Comparable.
compareTo in interface Comparable<Duration>otherDuration - the other duration to compare to, not nullpublic boolean isGreaterThan(Duration otherDuration)
Duration.
The comparison is based on the total length of the durations.
otherDuration - the other duration to compare to, not nullpublic boolean isLessThan(Duration otherDuration)
Duration.
The comparison is based on the total length of the durations.
otherDuration - the other duration to compare to, not nullpublic boolean equals(Object otherDuration)
Duration.
The comparison is based on the total length of the durations.
public int hashCode()
Copyright © 2007-2013 ThreeTen.org. All Rights Reserved.