- Description
- Field Summary
- Method Summary
- Field Details
- Method Details
- ofDays(long)
- ofHours(long)
- ofMinutes(long)
- ofSeconds(long)
- ofSeconds(long, long)
- ofMillis(long)
- ofNanos(long)
- of(long, TemporalUnit)
- from(TemporalAmount)
- parse(CharSequence)
- between(Temporal, Temporal)
- get(TemporalUnit)
- getUnits()
- isPositive()
- isZero()
- isNegative()
- getSeconds()
- getNano()
- withSeconds(long)
- withNanos(int)
- plus(Duration)
- plus(long, TemporalUnit)
- plusDays(long)
- plusHours(long)
- plusMinutes(long)
- plusSeconds(long)
- plusMillis(long)
- plusNanos(long)
- minus(Duration)
- minus(long, TemporalUnit)
- minusDays(long)
- minusHours(long)
- minusMinutes(long)
- minusSeconds(long)
- minusMillis(long)
- minusNanos(long)
- multipliedBy(long)
- dividedBy(long)
- dividedBy(Duration)
- negated()
- abs()
- addTo(Temporal)
- subtractFrom(Temporal)
- toDays()
- toHours()
- toMinutes()
- toSeconds()
- toMillis()
- toNanos()
- toDaysPart()
- toHoursPart()
- toMinutesPart()
- toSecondsPart()
- toMillisPart()
- toNanosPart()
- truncatedTo(TemporalUnit)
- compareTo(Duration)
- equals(Object)
- hashCode()
- toString()
Class Duration
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.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final DurationThe maximum supported Duration, which is Long.MAX_VALUE seconds and 999,999,999 nanoseconds.static final DurationThe minimum supported Duration, which is Long.MIN_VALUE seconds.static final DurationConstant for a duration of zero. -
Method Summary
All MethodsStatic MethodsInstance MethodsConcrete MethodsModifier and TypeMethodDescriptionabs()Returns a copy of this duration with a positive length.Adds this duration to the specified temporal object.static DurationObtains a Duration representing the duration between two temporal objects.intCompares this duration to the specified Duration.dividedBy(long divisor)Returns a copy of this duration divided by the specified value.longReturns number of whole times a specified Duration occurs within this Duration.booleanChecks if this duration is equal to the specified Duration.static Durationfrom(TemporalAmount amount)Obtains an instance of Duration from a temporal amount.longget(TemporalUnit unit)Gets the value of the requested unit.intgetNano()Gets the number of nanoseconds within the second in this duration.longGets the number of seconds in this duration.getUnits()Gets the set of units supported by this duration.inthashCode()A hash code for this duration.booleanChecks if this duration is negative, excluding zero.booleanChecks if this duration is positive, excluding zero.booleanisZero()Checks if this duration is zero length.minus(long amountToSubtract, TemporalUnit unit)Returns a copy of this duration with the specified duration subtracted.Returns a copy of this duration with the specified duration subtracted.minusDays(long daysToSubtract)Returns a copy of this duration with the specified duration in standard 24 hour days subtracted.minusHours(long hoursToSubtract)Returns a copy of this duration with the specified duration in hours subtracted.minusMillis(long millisToSubtract)Returns a copy of this duration with the specified duration in milliseconds subtracted.minusMinutes(long minutesToSubtract)Returns a copy of this duration with the specified duration in minutes subtracted.minusNanos(long nanosToSubtract)Returns a copy of this duration with the specified duration in nanoseconds subtracted.minusSeconds(long secondsToSubtract)Returns a copy of this duration with the specified duration in seconds subtracted.multipliedBy(long multiplicand)Returns a copy of this duration multiplied by the scalar.negated()Returns a copy of this duration with the length negated.static Durationof(long amount, TemporalUnit unit)Obtains a Duration representing an amount in the specified unit.static DurationofDays(long days)Obtains a Duration representing a number of standard 24 hour days.static DurationofHours(long hours)Obtains a Duration representing a number of standard hours.static DurationofMillis(long millis)Obtains a Duration representing a number of milliseconds.static DurationofMinutes(long minutes)Obtains a Duration representing a number of standard minutes.static DurationofNanos(long nanos)Obtains a Duration representing a number of nanoseconds.static DurationofSeconds(long seconds)Obtains a Duration representing a number of seconds.static DurationofSeconds(long seconds, long nanoAdjustment)Obtains a Duration representing a number of seconds and an adjustment in nanoseconds.static Durationparse(CharSequence text)Obtains a Duration from a text string such as PnDTnHnMn.nS.plus(long amountToAdd, TemporalUnit unit)Returns a copy of this duration with the specified duration added.Returns a copy of this duration with the specified duration added.plusDays(long daysToAdd)Returns a copy of this duration with the specified duration in standard 24 hour days added.plusHours(long hoursToAdd)Returns a copy of this duration with the specified duration in hours added.plusMillis(long millisToAdd)Returns a copy of this duration with the specified duration in milliseconds added.plusMinutes(long minutesToAdd)Returns a copy of this duration with the specified duration in minutes added.plusNanos(long nanosToAdd)Returns a copy of this duration with the specified duration in nanoseconds added.plusSeconds(long secondsToAdd)Returns a copy of this duration with the specified duration in seconds added.subtractFrom(Temporal temporal)Subtracts this duration from the specified temporal object.longtoDays()Gets the number of days in this duration.longExtracts the number of days in the duration.longtoHours()Gets the number of hours in this duration.intExtracts the number of hours part in the duration.longtoMillis()Converts this duration to the total length in milliseconds.intExtracts the number of milliseconds part of the duration.longGets the number of minutes in this duration.intExtracts the number of minutes part in the duration.longtoNanos()Converts this duration to the total length in nanoseconds expressed as a long.intGet the nanoseconds part within seconds of the duration.longGets the number of seconds in this duration.intExtracts the number of seconds part in the duration.toString()A string representation of this duration using ISO-8601 seconds based representation, such as PT8H6M12.345S.truncatedTo(TemporalUnit unit)Returns a copy of this Duration truncated to the specified unit.withNanos(int nanoOfSecond)Returns a copy of this duration with the specified nano-of-second.withSeconds(long seconds)Returns a copy of this duration with the specified amount of seconds.Methods declared in class Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitModifier and TypeMethodDescriptionprotected Objectclone()Creates and returns a copy of this object.protected voidfinalize()Deprecated, for removal: This API element is subject to removal in a future version.Finalization is deprecated and subject to removal in a future release.final Class<?>getClass()Returns the runtime class of this Object.final voidnotify()Wakes up a single thread that is waiting on this object's monitor.final voidWakes up all threads that are waiting on this object's monitor.final voidwait()Causes the current thread to wait until it is awakened, typically by being notified or interrupted.final voidwait(long timeoutMillis)Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.final voidwait(long timeoutMillis, int nanos)Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.
-
Field Details
-
ZERO
Constant for a duration of zero. -
MIN
The minimum supported Duration, which is Long.MIN_VALUE seconds.API Note: This constant represents the smallest possible instance of Duration. Since Duration is directed, the smallest possible duration is negative. The constant is intended to be used as a sentinel value or in tests. Care should be taken when performing arithmetic on MIN as there is a high risk that ArithmeticException or DateTimeException will be thrown. Since: 26 -
MAX
The maximum supported Duration, which is Long.MAX_VALUE seconds and 999,999,999 nanoseconds.API Note: This constant represents the largest possible instance of Duration. The constant is intended to be used as a sentinel value or in tests. Care should be taken when performing arithmetic on MAX as there is a high risk that ArithmeticException or DateTimeException will be thrown. Since: 26
-
-
Method Details
-
ofDays
Obtains a Duration representing a number of standard 24 hour days.Parameters: days - the number of days, positive or negative Returns: a Duration, not null Throws: ArithmeticException - if the input days exceeds the capacity of DurationThe seconds are calculated based on the standard definition of a day, where each day is 86,400 seconds which implies a 24 hour day. The nanosecond in second field is set to zero.
-
ofHours
Obtains a Duration representing a number of standard hours.Parameters: hours - the number of hours, positive or negative Returns: a Duration, not null Throws: ArithmeticException - if the input hours exceeds the capacity of DurationThe seconds are calculated based on the standard definition of an hour, where each hour is 3,600 seconds. The nanosecond in second field is set to zero.
-
ofMinutes
Obtains a Duration representing a number of standard minutes.Parameters: minutes - the number of minutes, positive or negative Returns: a Duration, not null Throws: ArithmeticException - if the input minutes exceeds the capacity of DurationThe 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.
-
ofSeconds
Obtains a Duration representing a number of seconds.Parameters: seconds - the number of seconds, positive or negative Returns: a Duration, not nullThe nanosecond in second field is set to zero.
-
ofSeconds
Obtains a Duration representing a number of seconds and an adjustment in nanoseconds.Parameters: seconds - the number of seconds, positive or negative nanoAdjustment - the nanosecond adjustment to the number of seconds, positive or negative Returns: a Duration, not null Throws: ArithmeticException - if the adjustment causes the seconds to exceed the capacity of DurationThis 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 exactly the same duration: Duration.ofSeconds(3, 1); Duration.ofSeconds(4, -999_999_999); Duration.ofSeconds(2, 1000_000_001);
-
ofMillis
Obtains a Duration representing a number of milliseconds.Parameters: millis - the number of milliseconds, positive or negative Returns: a Duration, not nullThe seconds and nanoseconds are extracted from the specified milliseconds.
-
ofNanos
Obtains a Duration representing a number of nanoseconds.Parameters: nanos - the number of nanoseconds, positive or negative Returns: a Duration, not nullThe seconds and nanoseconds are extracted from the specified nanoseconds.
-
of
Obtains a Duration representing an amount in the specified unit.Parameters: amount - the amount of the duration, measured in terms of the unit, positive or negative unit - the unit that the duration is measured in, must have an exact duration, not null Returns: a Duration, not null Throws: DateTimeException - if the unit has an estimated duration ArithmeticException - if a numeric overflow occursThe 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.
-
from
Obtains an instance of Duration from a temporal amount.Parameters: amount - the temporal amount to convert, not null Returns: the equivalent duration, not null Throws: DateTimeException - if unable to convert to a Duration ArithmeticException - if numeric overflow occursThis obtains a duration based on the specified amount. A TemporalAmount represents an amount of time, which may be date-based or time-based, which this factory extracts to a duration.
The conversion loops around the set of units from the amount and uses the duration of the unit to calculate the total Duration. 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. If any other units are found then an exception is thrown.
-
parse
Obtains a Duration from a text string such as PnDTnHnMn.nS.Parameters: text - the text to parse, not null Returns: the parsed duration, not null Throws: DateTimeParseException - if the text cannot be parsed to a durationThis will parse a textual representation of a duration, including the string produced by toString(). The formats accepted are based on the ISO-8601 duration format PnDTnHnMn.nS with days considered to be exactly 24 hours.
The string starts with an optional sign, denoted by the ASCII negative or positive symbol. If negative, the whole duration is negated. The ASCII letter "P" is next in upper or lower case. There are then four sections, each consisting of a number and a suffix. The sections have suffixes in ASCII of "D", "H", "M" and "S" for days, hours, minutes and seconds, accepted in upper or lower case. The suffixes must occur in order. The ASCII letter "T" must occur before the first occurrence, if any, of an hour, minute or second section. At least one of the four sections must be present, and if "T" is present there must be at least one section after the "T". The number part of each section must consist of one or more ASCII digits. The number may be prefixed by the ASCII negative or positive symbol. The number of days, hours and minutes must parse to a long. The number of seconds must parse to a long with optional fraction. The decimal point may be either a dot or a comma. The fractional part may have from zero to 9 digits.
The leading plus/minus sign, and negative values for other units are not part of the ISO-8601 standard.
Examples: "PT20.345S" -- parses as "20.345 seconds" "PT15M" -- parses as "15 minutes" (where a minute is 60 seconds) "PT10H" -- parses as "10 hours" (where an hour is 3,600 seconds) "P2D" -- parses as "2 days" (where a day is 24 hours or 86,400 seconds) "P2DT3H4M" -- parses as "2 days, 3 hours and 4 minutes" "PT-6H3M" -- parses as "-6 hours and +3 minutes" "-PT6H3M" -- parses as "-6 hours and -3 minutes" "-PT-6H+3M" -- parses as "+6 hours and -3 minutes"
-
between
Obtains a Duration representing the duration between two temporal objects.Parameters: startInclusive - the start instant, inclusive, not null endExclusive - the end instant, exclusive, not null Returns: a Duration, not null Throws: DateTimeException - if the seconds between the temporals cannot be obtained ArithmeticException - if the calculation exceeds the capacity of DurationThis calculates the duration between two temporal objects. If the objects are of different types, then the duration is calculated based on the type of the first object. For example, if the first argument is a LocalTime then the second argument is converted to a LocalTime.
The specified temporal objects must support the SECONDS unit. For full accuracy, either the NANOS unit or the NANO_OF_SECOND field should be supported.
The result of this method can be a negative duration if the end is before the start. To guarantee a positive duration, call abs() on the result.
-
get
Gets the value of the requested unit.Specified by: get in interface TemporalAmount Parameters: unit - the TemporalUnit for which to return the value Returns: the long value of the unit Throws: DateTimeException - if the unit is not supported UnsupportedTemporalTypeException - if the unit is not supportedThis returns a value for each of the two supported units, SECONDS and NANOS. All other units throw an exception.
-
getUnits
Gets the set of units supported by this duration.Specified by: getUnits in interface TemporalAmount Returns: a list containing the seconds and nanos units, not nullThe supported units are SECONDS, and NANOS. They are returned in the order seconds, nanos.
This set can be used in conjunction with get(TemporalUnit) to access the entire state of the duration.
-
isPositive
public boolean isPositive()Checks if this duration is positive, excluding zero.Returns: true if this duration has a total length greater than zero Since: 18A 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.
-
isZero
public boolean isZero()Checks if this duration is zero length.Returns: true if this duration has a total length equal to zeroA 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.
-
isNegative
public boolean isNegative()Checks if this duration is negative, excluding zero.Returns: true if this duration has a total length less than zeroA 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.
-
getSeconds
public long getSeconds()Gets the number of seconds in this duration.Returns: the whole seconds part of the length of the duration, positive or negativeThe 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.
-
getNano
public int getNano()Gets the number of nanoseconds within the second in this duration.Returns: the nanoseconds within the second part of the length of the duration, from 0 to 999,999,999The 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.
-
withSeconds
Returns a copy of this duration with the specified amount of seconds.Parameters: seconds - the seconds to represent, may be negative Returns: a Duration based on this duration with the requested seconds, not nullThis returns a duration with the specified seconds, retaining the nano-of-second part of this duration.
This instance is immutable and unaffected by this method call.
-
withNanos
Returns a copy of this duration with the specified nano-of-second.Parameters: nanoOfSecond - the nano-of-second to represent, from 0 to 999,999,999 Returns: a Duration based on this duration with the requested nano-of-second, not null Throws: DateTimeException - if the nano-of-second is invalidThis returns a duration with the specified nano-of-second, retaining the seconds part of this duration.
This instance is immutable and unaffected by this method call.
-
plus
Returns a copy of this duration with the specified duration added.Parameters: duration - the duration to add, positive or negative, not null Returns: a Duration based on this duration with the specified duration added, not null Throws: ArithmeticException - if numeric overflow occursThis instance is immutable and unaffected by this method call.
-
plus
Returns a copy of this duration with the specified duration added.Parameters: amountToAdd - the amount to add, measured in terms of the unit, positive or negative unit - the unit that the amount is measured in, must have an exact duration, not null Returns: a Duration based on this duration with the specified duration added, not null Throws: UnsupportedTemporalTypeException - if the unit is not supported ArithmeticException - if numeric overflow occursThe 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.
-
plusDays
Returns a copy of this duration with the specified duration in standard 24 hour days added.Parameters: daysToAdd - the days to add, positive or negative Returns: a Duration based on this duration with the specified days added, not null Throws: ArithmeticException - if numeric overflow occursThe number of days is multiplied by 86,400 to obtain the number of seconds to add. This is based on the standard definition of a day as 24 hours.
This instance is immutable and unaffected by this method call.
-
plusHours
Returns a copy of this duration with the specified duration in hours added.Parameters: hoursToAdd - the hours to add, positive or negative Returns: a Duration based on this duration with the specified hours added, not null Throws: ArithmeticException - if numeric overflow occursThis instance is immutable and unaffected by this method call.
-
plusMinutes
Returns a copy of this duration with the specified duration in minutes added.Parameters: minutesToAdd - the minutes to add, positive or negative Returns: a Duration based on this duration with the specified minutes added, not null Throws: ArithmeticException - if numeric overflow occursThis instance is immutable and unaffected by this method call.
-
plusSeconds
Returns a copy of this duration with the specified duration in seconds added.Parameters: secondsToAdd - the seconds to add, positive or negative Returns: a Duration based on this duration with the specified seconds added, not null Throws: ArithmeticException - if numeric overflow occursThis instance is immutable and unaffected by this method call.
-
plusMillis
Returns a copy of this duration with the specified duration in milliseconds added.Parameters: millisToAdd - the milliseconds to add, positive or negative Returns: a Duration based on this duration with the specified milliseconds added, not null Throws: ArithmeticException - if numeric overflow occursThis instance is immutable and unaffected by this method call.
-
plusNanos
Returns a copy of this duration with the specified duration in nanoseconds added.Parameters: nanosToAdd - the nanoseconds to add, positive or negative Returns: a Duration based on this duration with the specified nanoseconds added, not null Throws: ArithmeticException - if numeric overflow occursThis instance is immutable and unaffected by this method call.
-
minus
Returns a copy of this duration with the specified duration subtracted.Parameters: duration - the duration to subtract, positive or negative, not null Returns: a Duration based on this duration with the specified duration subtracted, not null Throws: ArithmeticException - if numeric overflow occursThis instance is immutable and unaffected by this method call.
-
minus
Returns a copy of this duration with the specified duration subtracted.Parameters: amountToSubtract - the amount to subtract, measured in terms of the unit, positive or negative unit - the unit that the amount is measured in, must have an exact duration, not null Returns: a Duration based on this duration with the specified duration subtracted, not null Throws: ArithmeticException - if numeric overflow occursThe 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.
-
minusDays
Returns a copy of this duration with the specified duration in standard 24 hour days subtracted.Parameters: daysToSubtract - the days to subtract, positive or negative Returns: a Duration based on this duration with the specified days subtracted, not null Throws: ArithmeticException - if numeric overflow occursThe number of days is multiplied by 86,400 to obtain the number of seconds to subtract. This is based on the standard definition of a day as 24 hours.
This instance is immutable and unaffected by this method call.
-
minusHours
Returns a copy of this duration with the specified duration in hours subtracted.Parameters: hoursToSubtract - the hours to subtract, positive or negative Returns: a Duration based on this duration with the specified hours subtracted, not null Throws: ArithmeticException - if numeric overflow occursThe number of hours is multiplied by 3,600 to obtain the number of seconds to subtract.
This instance is immutable and unaffected by this method call.
-
minusMinutes
Returns a copy of this duration with the specified duration in minutes subtracted.Parameters: minutesToSubtract - the minutes to subtract, positive or negative Returns: a Duration based on this duration with the specified minutes subtracted, not null Throws: ArithmeticException - if numeric overflow occursThe number of minutes is multiplied by 60 to obtain the number of seconds to subtract.
This instance is immutable and unaffected by this method call.
-
minusSeconds
Returns a copy of this duration with the specified duration in seconds subtracted.Parameters: secondsToSubtract - the seconds to subtract, positive or negative Returns: a Duration based on this duration with the specified seconds subtracted, not null Throws: ArithmeticException - if numeric overflow occursThis instance is immutable and unaffected by this method call.
-
minusMillis
Returns a copy of this duration with the specified duration in milliseconds subtracted.Parameters: millisToSubtract - the milliseconds to subtract, positive or negative Returns: a Duration based on this duration with the specified milliseconds subtracted, not null Throws: ArithmeticException - if numeric overflow occursThis instance is immutable and unaffected by this method call.
-
minusNanos
Returns a copy of this duration with the specified duration in nanoseconds subtracted.Parameters: nanosToSubtract - the nanoseconds to subtract, positive or negative Returns: a Duration based on this duration with the specified nanoseconds subtracted, not null Throws: ArithmeticException - if numeric overflow occursThis instance is immutable and unaffected by this method call.
-
multipliedBy
Returns a copy of this duration multiplied by the scalar.Parameters: multiplicand - the value to multiply the duration by, positive or negative Returns: a Duration based on this duration multiplied by the specified scalar, not null Throws: ArithmeticException - if numeric overflow occursThis instance is immutable and unaffected by this method call.
-
dividedBy
Returns a copy of this duration divided by the specified value.Parameters: divisor - the value to divide the duration by, positive or negative, not zero Returns: a Duration based on this duration divided by the specified divisor, not null Throws: ArithmeticException - if the divisor is zero or if numeric overflow occursThis instance is immutable and unaffected by this method call.
-
dividedBy
Returns number of whole times a specified Duration occurs within this Duration.Parameters: divisor - the value to divide the duration by, positive or negative, not null Returns: number of whole times, rounded toward zero, a specified Duration occurs within this Duration, may be negative Throws: ArithmeticException - if the divisor is zero, or if numeric overflow occurs Since: 9This instance is immutable and unaffected by this method call.
-
negated
Returns a copy of this duration with the length negated.Returns: a Duration based on this duration with the amount negated, not null Throws: ArithmeticException - if numeric overflow occursThis 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.
-
abs
Returns a copy of this duration with a positive length.Returns: a Duration based on this duration with an absolute length, not null Throws: ArithmeticException - if numeric overflow occursThis 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.
-
addTo
Adds this duration to the specified temporal object.Specified by: addTo in interface TemporalAmount Parameters: temporal - the temporal object to adjust, not null Returns: an object of the same type with the adjustment made, not null Throws: DateTimeException - if unable to add ArithmeticException - if numeric overflow occursThis 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(TemporalAmount). // these two lines are equivalent, but the second approach is recommended dateTime = thisDuration.addTo(dateTime); dateTime = dateTime.plus(thisDuration);
The calculation will add the seconds, then nanos. Only non-zero amounts will be added.
This instance is immutable and unaffected by this method call.
-
subtractFrom
Subtracts this duration from the specified temporal object.Specified by: subtractFrom in interface TemporalAmount Parameters: temporal - the temporal object to adjust, not null Returns: an object of the same type with the adjustment made, not null Throws: DateTimeException - if unable to subtract ArithmeticException - if numeric overflow occursThis 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(TemporalAmount). // these two lines are equivalent, but the second approach is recommended dateTime = thisDuration.subtractFrom(dateTime); dateTime = dateTime.minus(thisDuration);
The calculation will subtract the seconds, then nanos. Only non-zero amounts will be added.
This instance is immutable and unaffected by this method call.
-
toDays
public long toDays()Gets the number of days in this duration.Returns: the number of days in the duration, may be negativeThis returns the total number of days in the duration by dividing the number of seconds by 86,400. This is based on the standard definition of a day as 24 hours.
This instance is immutable and unaffected by this method call.
-
toHours
public long toHours()Gets the number of hours in this duration.Returns: the number of hours in the duration, may be negativeThis returns the total number of hours in the duration by dividing the number of seconds by 3,600.
This instance is immutable and unaffected by this method call.
-
toMinutes
public long toMinutes()Gets the number of minutes in this duration.Returns: the number of minutes in the duration, may be negativeThis returns the total number of minutes in the duration by dividing the number of seconds by 60.
This instance is immutable and unaffected by this method call.
-
toSeconds
public long toSeconds()Gets the number of seconds in this duration.Returns: the whole seconds part of the length of the duration, positive or negative Since: 9This returns the total number of whole seconds in the duration.
This instance is immutable and unaffected by this method call.
-
toMillis
public long toMillis()Converts this duration to the total length in milliseconds.Returns: the total length of the duration in milliseconds Throws: ArithmeticException - if numeric overflow occursIf 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.
-
toNanos
public long toNanos()Converts this duration to the total length in nanoseconds expressed as a long.Returns: the total length of the duration in nanoseconds Throws: ArithmeticException - if numeric overflow occursIf this duration is too large to fit in a long nanoseconds, then an exception is thrown.
-
toDaysPart
public long toDaysPart()Extracts the number of days in the duration.API Note: This method behaves exactly the same way as toDays(). Returns: the number of days in the duration, may be negative Since: 9This returns the total number of days in the duration by dividing the number of seconds by 86,400. This is based on the standard definition of a day as 24 hours.
This instance is immutable and unaffected by this method call.
-
toHoursPart
public int toHoursPart()Extracts the number of hours part in the duration.Returns: the number of hours part in the duration, may be negative Since: 9This returns the number of remaining hours when dividing toHours() by hours in a day. This is based on the standard definition of a day as 24 hours.
This instance is immutable and unaffected by this method call.
-
toMinutesPart
public int toMinutesPart()Extracts the number of minutes part in the duration.Returns: the number of minutes parts in the duration, may be negative Since: 9This returns the number of remaining minutes when dividing toMinutes() by minutes in an hour. This is based on the standard definition of an hour as 60 minutes.
This instance is immutable and unaffected by this method call.
-
toSecondsPart
public int toSecondsPart()Extracts the number of seconds part in the duration.Returns: the number of seconds parts in the duration, may be negative Since: 9This returns the remaining seconds when dividing toSeconds() by seconds in a minute. This is based on the standard definition of a minute as 60 seconds.
This instance is immutable and unaffected by this method call.
-
toMillisPart
public int toMillisPart()Extracts the number of milliseconds part of the duration.Returns: the number of milliseconds part of the duration. Since: 9This returns the milliseconds part by dividing the number of nanoseconds by 1,000,000. 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 getNano() and getSeconds().
This instance is immutable and unaffected by this method call.
-
toNanosPart
public int toNanosPart()Get the nanoseconds part within seconds of the duration.Returns: the nanoseconds within the second part of the length of the duration, from 0 to 999,999,999 Since: 9The 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 getNano() and getSeconds().
This instance is immutable and unaffected by this method call.
-
truncatedTo
Returns a copy of this Duration truncated to the specified unit.Parameters: unit - the unit to truncate to, not null Returns: a Duration based on this duration with the time truncated, not null Throws: DateTimeException - if the unit is invalid for truncation UnsupportedTemporalTypeException - if the unit is not supported Since: 9Truncating the duration returns a copy of the original with conceptual fields smaller than the specified unit set to zero. For example, truncating with the MINUTES unit will round down towards zero to the nearest minute, setting the seconds and nanoseconds to zero.
The unit must have a duration that divides into the length of a standard day without remainder. This includes all time-based units on ChronoUnit and DAYS. Other ChronoUnits throw an exception.
This instance is immutable and unaffected by this method call.
-
compareTo
Compares this duration to the specified Duration.Specified by: compareTo in interface Comparable<Duration> Parameters: otherDuration - the other duration to compare to, not null 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 otherDurationThe comparison is based on the total length of the durations. It is "consistent with equals", as defined by Comparable.
-
equals
-
hashCode
-
toString
A string representation of this duration using ISO-8601 seconds based representation, such as PT8H6M12.345S.Overrides: toString in class Object Returns: an ISO-8601 representation of this duration, not nullThe format of the returned string will be PTnHnMnS, where n is the relevant hours, minutes or seconds part of the duration. Any fractional seconds are placed after a decimal point in the seconds section. If a section has a zero value, it is omitted. The hours, minutes and seconds will all have the same sign.
Examples: "20.345 seconds" -- "PT20.345S" "15 minutes" (15 * 60 seconds) -- "PT15M" "10 hours" (10 * 3,600 seconds) -- "PT10H" "2 days" (2 * 86,400 seconds) -- "PT48H" Note that multiples of 24 hours are not output as days to avoid confusion with Period.
-
Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples. Other versions.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2026, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.