Top Description Inners Fields Constructors Methods
java.time

public final Class Duration

extends Object
implements TemporalAmount, Comparable<Duration>, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, java.lang.Comparable, java.time.temporal.TemporalAmount
Annotations
@ValueBased
Static Imports
java.time.LocalTime.MINUTES_PER_HOUR, .LocalTime.NANOS_PER_MILLI, .LocalTime.NANOS_PER_SECOND, .LocalTime.SECONDS_PER_DAY, .LocalTime.SECONDS_PER_HOUR, .LocalTime.SECONDS_PER_MINUTE, java.time.temporal.ChronoField.NANO_OF_SECOND, .ChronoUnit.DAYS, .ChronoUnit.NANOS, .ChronoUnit.SECONDS

A time-based amount of time, such as '34.5 seconds'.

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.

Since
1.8

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
Duration.DurationUnits

Private class to delay initialization of this list until needed.

private static class
Duration.Lazy

The pattern for parsing.

Field Summary

Modifier and TypeField 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.

Constructor Summary

AccessConstructor 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 Duration using seconds and nanoseconds.

Method Summary

Modifier and TypeMethod and Description
public Duration

Returns:

a Duration based on this duration with an absolute length, not null
abs
()

Returns a copy of this duration with a positive length.

public Temporal

Returns:

an object of the same type with the adjustment made, not null
addTo
(Temporal
the temporal object to adjust, not null
temporal
)

Implements java.time.temporal.TemporalAmount.addTo.

Adds this duration to the specified temporal object.
public static Duration

Returns:

a Duration, not null
between
(Temporal
the start instant, inclusive, not null
startInclusive
,
Temporal
the end instant, exclusive, not null
endExclusive
)

Obtains a Duration representing the duration between two temporal objects.

private static boolean
charMatch(CharSequence text, int start, int end, char c)

public int

Returns:

the comparator value, that is less than zero if this duration is less than otherDuration, zero if they are equal, greater than zero if this duration is greater than otherDuration
compareTo
(Duration
the other duration to compare to, not null
otherDuration
)

Implements java.lang.Comparable.compareTo.

Compares this duration to the specified Duration.
private static Duration
create(boolean negate, long daysAsSecs, long hoursAsSecs, long minsAsSecs, long secs, int nanos)

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 Duration using seconds and nanoseconds.

private static Duration

Returns:

a Duration, not null
create
(BigDecimal
the number of seconds, up to scale 9, positive or negative
seconds
)

Creates an instance of Duration from a number of seconds.

public Duration

Returns:

a Duration based on this duration divided by the specified divisor, not null
dividedBy
(long
the 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

Returns:

number of whole times, rounded toward zero, a specified Duration occurs within this Duration, may be negative
dividedBy
(Duration
the value to divide the duration by, positive or negative, not null
divisor
)

Returns number of whole times a specified Duration occurs within this Duration.

public boolean

Returns:

true if the other duration is equal to this one
equals
(Object
the other duration, null returns false
other
)

Overrides java.lang.Object.equals.

Checks if this duration is equal to the specified Duration.
public static Duration

Returns:

the equivalent duration, not null
from
(TemporalAmount
the temporal amount to convert, not null
amount
)

Obtains an instance of Duration from a temporal amount.

public long

Returns:

the long value of the unit
get
(TemporalUnit
the TemporalUnit for which to return the value
unit
)

Implements java.time.temporal.TemporalAmount.get.

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,999
getNano
()

Gets 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 negative
getSeconds
()

Gets the number of seconds in this duration.

public List<TemporalUnit>

Returns:

a list containing the seconds and nanos units, not null
getUnits
()

Implements java.time.temporal.TemporalAmount.getUnits.

Gets the set of units supported by this duration.
public int

Returns:

a suitable hash code
hashCode
()

Overrides java.lang.Object.hashCode.

A hash code for this duration.
public boolean

Returns:

true if this duration has a total length less than zero
isNegative
()

Checks if this duration is negative, excluding zero.

public boolean

Returns:

true if this duration has a total length greater than zero
isPositive
()

Checks if this duration is positive, excluding zero.

public boolean

Returns:

true if this duration has a total length equal to zero
isZero
()

Checks if this duration is zero length.

public Duration

Returns:

a Duration based on this duration with the specified duration subtracted, not null
minus
(Duration
the duration to subtract, positive or negative, not null
duration
)

Returns a copy of this duration with the specified duration subtracted.

public Duration

Returns:

a Duration based on this duration with the specified duration subtracted, not null
minus
(long
the 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:

a Duration based on this duration with the specified days subtracted, not null
minusDays
(long
the 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:

a Duration based on this duration with the specified hours subtracted, not null
minusHours
(long
the hours to subtract, positive or negative
hoursToSubtract
)

Returns a copy of this duration with the specified duration in hours subtracted.

public Duration

Returns:

a Duration based on this duration with the specified milliseconds subtracted, not null
minusMillis
(long
the milliseconds to subtract, positive or negative
millisToSubtract
)

Returns a copy of this duration with the specified duration in milliseconds subtracted.

public Duration

Returns:

a Duration based on this duration with the specified minutes subtracted, not null
minusMinutes
(long
the minutes to subtract, positive or negative
minutesToSubtract
)

Returns a copy of this duration with the specified duration in minutes subtracted.

public Duration

Returns:

a Duration based on this duration with the specified nanoseconds subtracted, not null
minusNanos
(long
the nanoseconds to subtract, positive or negative
nanosToSubtract
)

Returns a copy of this duration with the specified duration in nanoseconds subtracted.

public Duration

Returns:

a Duration based on this duration with the specified seconds subtracted, not null
minusSeconds
(long
the seconds to subtract, positive or negative
secondsToSubtract
)

Returns a copy of this duration with the specified duration in seconds subtracted.

public Duration

Returns:

a Duration based on this duration multiplied by the specified scalar, not null
multipliedBy
(long
the value to multiply the duration by, positive or negative
multiplicand
)

Returns a copy of this duration multiplied by the scalar.

public Duration

Returns:

a Duration based on this duration with the amount negated, not null
negated
()

Returns a copy of this duration with the length negated.

public static Duration

Returns:

a Duration, not null
of
(long
the 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 Duration representing an amount in the specified unit.

public static Duration

Returns:

a Duration, not null
ofDays
(long
the number of days, positive or negative
days
)

Obtains a Duration representing a number of standard 24 hour days.

public static Duration

Returns:

a Duration, not null
ofHours
(long
the number of hours, positive or negative
hours
)

Obtains a Duration representing a number of standard hours.

public static Duration

Returns:

a Duration, not null
ofMillis
(long
the number of milliseconds, positive or negative
millis
)

Obtains a Duration representing a number of milliseconds.

public static Duration

Returns:

a Duration, not null
ofMinutes
(long
the number of minutes, positive or negative
minutes
)

Obtains a Duration representing a number of standard minutes.

public static Duration

Returns:

a Duration, not null
ofNanos
(long
the number of nanoseconds, positive or negative
nanos
)

Obtains a Duration representing a number of nanoseconds.

public static Duration

Returns:

a Duration, not null
ofSeconds
(long
the number of seconds, positive or negative
seconds
)

Obtains a Duration representing a number of seconds.

public static Duration

Returns:

a Duration, not null
ofSeconds
(long
the number of seconds, positive or negative
seconds
,
long
the nanosecond adjustment to the number of seconds, positive or negative
nanoAdjustment
)

Obtains a Duration representing a number of seconds and an adjustment in nanoseconds.

public static Duration

Returns:

the parsed duration, not null
parse
(CharSequence
the text to parse, not null
text
)

Obtains a Duration from a text string such as PnDTnHnMn.nS.

private static int
parseFraction(CharSequence text, int start, int end, int negate)

private static long
parseNumber(CharSequence text, int start, int end, int multiplier, String errorText)

public Duration

Returns:

a Duration based on this duration with the specified duration added, not null
plus
(Duration
the duration to add, positive or negative, not null
duration
)

Returns a copy of this duration with the specified duration added.

public Duration

Returns:

a Duration based on this duration with the specified duration added, not null
plus
(long
the 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:

a Duration based on this duration with the specified seconds added, not null
plus
(long
the 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:

a Duration based on this duration with the specified days added, not null
plusDays
(long
the 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:

a Duration based on this duration with the specified hours added, not null
plusHours
(long
the hours to add, positive or negative
hoursToAdd
)

Returns a copy of this duration with the specified duration in hours added.

public Duration

Returns:

a Duration based on this duration with the specified milliseconds added, not null
plusMillis
(long
the milliseconds to add, positive or negative
millisToAdd
)

Returns a copy of this duration with the specified duration in milliseconds added.

public Duration

Returns:

a Duration based on this duration with the specified minutes added, not null
plusMinutes
(long
the minutes to add, positive or negative
minutesToAdd
)

Returns a copy of this duration with the specified duration in minutes added.

public Duration

Returns:

a Duration based on this duration with the specified nanoseconds added, not null
plusNanos
(long
the nanoseconds to add, positive or negative
nanosToAdd
)

Returns a copy of this duration with the specified duration in nanoseconds added.

public Duration

Returns:

a Duration based on this duration with the specified seconds added, not null
plusSeconds
(long
the 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
readObject(ObjectInputStream
the stream to read
s
)

Defend against malicious streams.

public Temporal

Returns:

an object of the same type with the adjustment made, not null
subtractFrom
(Temporal
the temporal object to adjust, not null
temporal
)

Implements java.time.temporal.TemporalAmount.subtractFrom.

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 null
toBigDecimalSeconds
()

Converts this duration to the total length in seconds and fractional nanoseconds expressed as a BigDecimal.

public long

Returns:

the number of days in the duration, may be negative
toDays
()

Gets the number of days in this duration.

public long

Returns:

the number of days in the duration, may be negative
toDaysPart
()

Extracts the number of days in the duration.

public long

Returns:

the number of hours in the duration, may be negative
toHours
()

Gets the number of hours in this duration.

public int

Returns:

the number of hours part in the duration, may be negative
toHoursPart
()

Extracts the number of hours part in the duration.

public long

Returns:

the total length of the duration in milliseconds
toMillis
()

Converts this duration to the total length in milliseconds.

public int

Returns:

the number of milliseconds part of the duration.
toMillisPart
()

Extracts the number of milliseconds part of the duration.

public long

Returns:

the number of minutes in the duration, may be negative
toMinutes
()

Gets the number of minutes in this duration.

public int

Returns:

the number of minutes parts in the duration, may be negative
toMinutesPart
()

Extracts the number of minutes part in the duration.

public long

Returns:

the total length of the duration in nanoseconds
toNanos
()

Converts this duration to the total length in nanoseconds expressed as a long.

public int

Returns:

the nanoseconds within the second part of the length of the duration, from 0 to 999,999,999
toNanosPart
()

Get the nanoseconds part within seconds of the duration.

public long

Returns:

the whole seconds part of the length of the duration, positive or negative
toSeconds
()

Gets the number of seconds in this duration.

public int

Returns:

the number of seconds parts in the duration, may be negative
toSecondsPart
()

Extracts the number of seconds part in the duration.

public String

Returns:

an ISO-8601 representation of this duration, not null
toString
()

Overrides java.lang.Object.toString.

A string representation of this duration using ISO-8601 seconds based representation, such as PT8H6M12.345S.
public Duration

Returns:

a Duration based on this duration with the time truncated, not null
truncatedTo
(TemporalUnit
the unit to truncate to, not null
unit
)

Returns a copy of this Duration truncated to the specified unit.

public Duration

Returns:

a Duration based on this duration with the requested nano-of-second, not null
withNanos
(int
the 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:

a Duration based on this duration with the requested seconds, not null
withSeconds
(long
the 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 of Ser, not null
writeReplace
()

Writes the object using a dedicated serialized form.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait