This class models a quantity or amount of time in terms of seconds and nanoseconds.
It can be accessed using other duration-based units, such as minutes and hours.
In addition, the DAYS
unit can be used and is treated as
exactly equal to 24 hours, thus ignoring daylight savings effects.
See Period
for the date-based equivalent to this class.
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 model is of a directed duration, meaning that the duration may be negative.
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.
This is a value-based
class; programmers should treat instances that are
equal as interchangeable and should not
use instances for synchronization, or unpredictable behavior may
occur. For example, in a future release, synchronization may fail.
The equals
method should be used for comparisons.
Implementation Specification
This class is immutable and thread-safe.
Modifier and Type | Class and Description |
---|---|
private static class | Duration.
Private class to delay initialization of this list until needed. |
private static class | Duration.
The pattern for parsing. |
Modifier and Type | Field and Description |
---|---|
private static final BigInteger | BI_NANOS_PER_SECOND
Constant for nanos per second. |
private final int | nanos
The number of nanoseconds in the duration, expressed as a fraction of the number of seconds. |
private final long | seconds
The number of seconds in the duration. |
private static final long | serialVersionUID
Serialization version. |
public static final Duration | ZERO
Constant for a duration of zero. |
Access | Constructor and Description |
---|---|
private | Duration(long
the length of the duration in seconds, positive or negative seconds, int the nanoseconds within the second, from 0 to 999,999,999 nanos)Constructs an instance of |
Modifier and Type | Method and Description |
---|---|
public Duration | Returns: aDuration based on this duration with an absolute length, not nullReturns a copy of this duration with a positive length. |
public Temporal | Returns: an object of the same type with the adjustment made, not nullthe temporal object to adjust, not null temporal)Implements java. Adds this duration to the specified temporal object. |
public static Duration | |
private static boolean | |
public int | Returns: the comparator value, that is less than zero if this duration is less thanotherDuration ,
zero if they are equal, greater than zero if this duration is greater than otherDuration the other duration to compare to, not null otherDuration)Implements java. Compares this duration to the specified |
private static Duration | |
private static Duration | create(long
the length of the duration in seconds, positive or negative seconds, int the nanosecond adjustment within the second, from 0 to 999,999,999 nanoAdjustment)Obtains an instance of |
private static Duration | Returns: aDuration , not nullthe number of seconds, up to scale 9, positive or negative seconds)Creates an instance of |
public Duration | Returns: aDuration based on this duration divided by the specified divisor, not nullthe value to divide the duration by, positive or negative, not zero divisor)Returns a copy of this duration divided by the specified value. |
public long | |
public boolean | Returns: true if the other duration is equal to this onethe other duration, null returns false other)Overrides java. Checks if this duration is equal to the specified |
public static Duration | Returns: the equivalent duration, not nullthe temporal amount to convert, not null amount)Obtains an instance of |
public long | Returns: the long value of the unitthe unit)TemporalUnit for which to return the valueImplements java. Gets the value of the requested unit. |
public int | Returns: the nanoseconds within the second part of the length of the duration, from 0 to 999,999,999Gets the number of nanoseconds within the second in this duration. |
public long | Returns: the whole seconds part of the length of the duration, positive or negativeGets the number of seconds in this duration. |
public List | Returns: a list containing the seconds and nanos units, not nullImplements java. Gets the set of units supported by this duration. |
public int | Returns: a suitable hash codeOverrides java. A hash code for this duration. |
public boolean | Returns: true if this duration has a total length less than zeroChecks if this duration is negative, excluding zero. |
public boolean | Returns: true if this duration has a total length greater than zeroChecks if this duration is positive, excluding zero. |
public boolean | Returns: true if this duration has a total length equal to zeroChecks if this duration is zero length. |
public Duration | |
public Duration | Returns: aDuration based on this duration with the specified duration subtracted, not nullthe amount to subtract, measured in terms of the unit, positive or negative amountToSubtract, TemporalUnit the unit that the amount is measured in, must have an exact duration, not null unit)Returns a copy of this duration with the specified duration subtracted. |
public Duration | Returns: aDuration based on this duration with the specified days subtracted, not nullthe days to subtract, positive or negative daysToSubtract)Returns a copy of this duration with the specified duration in standard 24 hour days subtracted. |
public Duration | Returns: aDuration based on this duration with the specified hours subtracted, not nullthe hours to subtract, positive or negative hoursToSubtract)Returns a copy of this duration with the specified duration in hours subtracted. |
public Duration | Returns: aDuration based on this duration with the specified milliseconds subtracted, not nullthe milliseconds to subtract, positive or negative millisToSubtract)Returns a copy of this duration with the specified duration in milliseconds subtracted. |
public Duration | Returns: aDuration based on this duration with the specified minutes subtracted, not nullthe minutes to subtract, positive or negative minutesToSubtract)Returns a copy of this duration with the specified duration in minutes subtracted. |
public Duration | Returns: aDuration based on this duration with the specified nanoseconds subtracted, not nullthe nanoseconds to subtract, positive or negative nanosToSubtract)Returns a copy of this duration with the specified duration in nanoseconds subtracted. |
public Duration | Returns: aDuration based on this duration with the specified seconds subtracted, not nullthe seconds to subtract, positive or negative secondsToSubtract)Returns a copy of this duration with the specified duration in seconds subtracted. |
public Duration | Returns: aDuration based on this duration multiplied by the specified scalar, not nullthe value to multiply the duration by, positive or negative multiplicand)Returns a copy of this duration multiplied by the scalar. |
public Duration | Returns: aDuration based on this duration with the amount negated, not nullReturns a copy of this duration with the length negated. |
public static Duration | Returns: aDuration , not nullthe amount of the duration, measured in terms of the unit, positive or negative amount, TemporalUnit the unit that the duration is measured in, must have an exact duration, not null unit)Obtains a |
public static Duration | Returns: aDuration , not nullthe number of days, positive or negative days)Obtains a |
public static Duration | Returns: aDuration , not nullthe number of hours, positive or negative hours)Obtains a |
public static Duration | Returns: aDuration , not nullthe number of milliseconds, positive or negative millis)Obtains a |
public static Duration | Returns: aDuration , not nullthe number of minutes, positive or negative minutes)Obtains a |
public static Duration | Returns: aDuration , not nullthe number of nanoseconds, positive or negative nanos)Obtains a |
public static Duration | Returns: aDuration , not nullthe number of seconds, positive or negative seconds)Obtains a |
public static Duration | Returns: aDuration , not nullthe number of seconds, positive or negative seconds, long the nanosecond adjustment to the number of seconds, positive or negative nanoAdjustment)Obtains a |
public static Duration | Returns: the parsed duration, not nullthe text to parse, not null text)Obtains a |
private static int | |
private static long | |
public Duration | |
public Duration | Returns: aDuration based on this duration with the specified duration added, not nullthe amount to add, measured in terms of the unit, positive or negative amountToAdd, TemporalUnit the unit that the amount is measured in, must have an exact duration, not null unit)Returns a copy of this duration with the specified duration added. |
private Duration | Returns: aDuration based on this duration with the specified seconds added, not nullthe seconds to add, positive or negative secondsToAdd, long the nanos to add, positive or negative nanosToAdd)Returns a copy of this duration with the specified duration added. |
public Duration | Returns: aDuration based on this duration with the specified days added, not nullthe days to add, positive or negative daysToAdd)Returns a copy of this duration with the specified duration in standard 24 hour days added. |
public Duration | Returns: aDuration based on this duration with the specified hours added, not nullthe hours to add, positive or negative hoursToAdd)Returns a copy of this duration with the specified duration in hours added. |
public Duration | Returns: aDuration based on this duration with the specified milliseconds added, not nullthe milliseconds to add, positive or negative millisToAdd)Returns a copy of this duration with the specified duration in milliseconds added. |
public Duration | Returns: aDuration based on this duration with the specified minutes added, not nullthe minutes to add, positive or negative minutesToAdd)Returns a copy of this duration with the specified duration in minutes added. |
public Duration | Returns: aDuration based on this duration with the specified nanoseconds added, not nullthe nanoseconds to add, positive or negative nanosToAdd)Returns a copy of this duration with the specified duration in nanoseconds added. |
public Duration | Returns: aDuration based on this duration with the specified seconds added, not nullthe seconds to add, positive or negative secondsToAdd)Returns a copy of this duration with the specified duration in seconds added. |
pack-priv static Duration | |
private void | |
public Temporal | Returns: an object of the same type with the adjustment made, not nullthe temporal object to adjust, not null temporal)Implements java. Subtracts this duration from the specified temporal object. |
private BigDecimal | Returns: the total length of the duration in seconds, with a scale of 9, not nullConverts this duration to the total length in seconds and
fractional nanoseconds expressed as a |
public long | Returns: the number of days in the duration, may be negativeGets the number of days in this duration. |
public long | Returns: the number of days in the duration, may be negativeExtracts the number of days in the duration. |
public long | Returns: the number of hours in the duration, may be negativeGets the number of hours in this duration. |
public int | Returns: the number of hours part in the duration, may be negativeExtracts the number of hours part in the duration. |
public long | Returns: the total length of the duration in millisecondsConverts this duration to the total length in milliseconds. |
public int | Returns: the number of milliseconds part of the duration.Extracts the number of milliseconds part of the duration. |
public long | Returns: the number of minutes in the duration, may be negativeGets the number of minutes in this duration. |
public int | Returns: the number of minutes parts in the duration, may be negativeExtracts the number of minutes part in the duration. |
public long | Returns: the total length of the duration in nanosecondsConverts this duration to the total length in nanoseconds expressed as a |
public int | Returns: the nanoseconds within the second part of the length of the duration, from 0 to 999,999,999Get the nanoseconds part within seconds of the duration. |
public long | Returns: the whole seconds part of the length of the duration, positive or negativeGets the number of seconds in this duration. |
public int | Returns: the number of seconds parts in the duration, may be negativeExtracts the number of seconds part in the duration. |
public String | Returns: an ISO-8601 representation of this duration, not nullOverrides java. A string representation of this duration using ISO-8601 seconds
based representation, such as |
public Duration | Returns: aDuration based on this duration with the time truncated, not nullthe unit to truncate to, not null unit)Returns a copy of this |
public Duration | Returns: aDuration based on this duration with the requested nano-of-second, not nullthe nano-of-second to represent, from 0 to 999,999,999 nanoOfSecond)Returns a copy of this duration with the specified nano-of-second. |
public Duration | Returns: aDuration based on this duration with the requested seconds, not nullthe seconds to represent, may be negative seconds)Returns a copy of this duration with the specified amount of seconds. |
pack-priv void | |
private Object | Returns: the instance ofSer , not nullWrites the object using a dedicated serialized form. |