* #### ZERO
public static final [Duration](../../java/time/Duration.html "class in java.time") ZERO
Constant for a duration of zero.
Method Detail
* #### ofDays
public static [Duration](../../java/time/Duration.html "class in java.time") ofDays(long days)
Obtains a `Duration` representing 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.
Parameters:
`days` \- the number of days, positive or negative
Returns:
a `Duration`, not null
Throws:
`[ArithmeticException](../../java/lang/ArithmeticException.html "class in java.lang")` \- if the input days exceeds the capacity of `Duration`
* #### ofHours
public static [Duration](../../java/time/Duration.html "class in java.time") ofHours(long hours)
Obtains a `Duration` representing a number of standard 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.
Parameters:
`hours` \- the number of hours, positive or negative
Returns:
a `Duration`, not null
Throws:
`[ArithmeticException](../../java/lang/ArithmeticException.html "class in java.lang")` \- if the input hours exceeds the capacity of `Duration`
* #### ofMinutes
public static [Duration](../../java/time/Duration.html "class in java.time") ofMinutes(long minutes)
Obtains a `Duration` representing a number of standard 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.
Parameters:
`minutes` \- the number of minutes, positive or negative
Returns:
a `Duration`, not null
Throws:
`[ArithmeticException](../../java/lang/ArithmeticException.html "class in java.lang")` \- if the input minutes exceeds the capacity of `Duration`
* #### ofSeconds
public static [Duration](../../java/time/Duration.html "class in java.time") ofSeconds(long seconds)
Obtains a `Duration` representing a number of seconds.
The nanosecond in second field is set to zero.
Parameters:
`seconds` \- the number of seconds, positive or negative
Returns:
a `Duration`, not null
* #### ofSeconds
public static [Duration](../../java/time/Duration.html "class in java.time") ofSeconds(long seconds,
long nanoAdjustment)
Obtains a `Duration` representing 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);
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](../../java/lang/ArithmeticException.html "class in java.lang")` \- if the adjustment causes the seconds to exceed the capacity of `Duration`
* #### ofMillis
public static [Duration](../../java/time/Duration.html "class in java.time") ofMillis(long millis)
Obtains a `Duration` representing a number of milliseconds.
The seconds and nanoseconds are extracted from the specified milliseconds.
Parameters:
`millis` \- the number of milliseconds, positive or negative
Returns:
a `Duration`, not null
* #### ofNanos
public static [Duration](../../java/time/Duration.html "class in java.time") ofNanos(long nanos)
Obtains a `Duration` representing a number of nanoseconds.
The seconds and nanoseconds are extracted from the specified nanoseconds.
Parameters:
`nanos` \- the number of nanoseconds, positive or negative
Returns:
a `Duration`, not null
* #### of
public static [Duration](../../java/time/Duration.html "class in java.time") of(long amount,
[TemporalUnit](../../java/time/temporal/TemporalUnit.html "interface in java.time.temporal") unit)
Obtains a `Duration` representing an amount 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](../../java/time/temporal/TemporalUnit.html#isDurationEstimated--) or be [ChronoUnit.DAYS](../../java/time/temporal/ChronoUnit.html#DAYS) which is treated as 24 hours. Other units throw an exception.
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](../../java/time/DateTimeException.html "class in java.time")` \- if the period unit has an estimated duration
`[ArithmeticException](../../java/lang/ArithmeticException.html "class in java.lang")` \- if a numeric overflow occurs
* #### from
public static [Duration](../../java/time/Duration.html "class in java.time") from([TemporalAmount](../../java/time/temporal/TemporalAmount.html "interface in java.time.temporal") amount)
Obtains an instance of `Duration` from a temporal amount.
This 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](../../java/time/temporal/TemporalUnit.html#getDuration--) 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](../../java/time/temporal/TemporalUnit.html#isDurationEstimated--) or be [ChronoUnit.DAYS](../../java/time/temporal/ChronoUnit.html#DAYS) which is treated as 24 hours. If any other units are found then an exception is thrown.
Parameters:
`amount` \- the temporal amount to convert, not null
Returns:
the equivalent duration, not null
Throws:
`[DateTimeException](../../java/time/DateTimeException.html "class in java.time")` \- if unable to convert to a `Duration`
`[ArithmeticException](../../java/lang/ArithmeticException.html "class in java.lang")` \- if numeric overflow occurs
* #### parse
public static [Duration](../../java/time/Duration.html "class in java.time") parse([CharSequence](../../java/lang/CharSequence.html "interface in java.lang") text)
Obtains a `Duration` from a text string such as `PnDTnHnMn.nS`.
This 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 period 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 3600 seconds)
"P2D" -- parses as "2 days" (where a day is 24 hours or 86400 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"
Parameters:
`text` \- the text to parse, not null
Returns:
the parsed duration, not null
Throws:
`[DateTimeParseException](../../java/time/format/DateTimeParseException.html "class in java.time.format")` \- if the text cannot be parsed to a duration
* #### between
public static [Duration](../../java/time/Duration.html "class in java.time") between([Temporal](../../java/time/temporal/Temporal.html "interface in java.time.temporal") startInclusive,
[Temporal](../../java/time/temporal/Temporal.html "interface in java.time.temporal") endExclusive)
Obtains a `Duration` representing the duration between two temporal objects.
This 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](../../java/time/temporal/ChronoUnit.html#SECONDS) unit. For full accuracy, either the [NANOS](../../java/time/temporal/ChronoUnit.html#NANOS) unit or the[NANO\_OF\_SECOND](../../java/time/temporal/ChronoField.html#NANO%5FOF%5FSECOND) field should be supported.
The result of this method can be a negative period if the end is before the start. To guarantee to obtain a positive duration call [abs()](../../java/time/Duration.html#abs--) on the result.
Parameters:
`startInclusive` \- the start instant, inclusive, not null
`endExclusive` \- the end instant, exclusive, not null
Returns:
a `Duration`, not null
Throws:
`[DateTimeException](../../java/time/DateTimeException.html "class in java.time")` \- if the seconds between the temporals cannot be obtained
`[ArithmeticException](../../java/lang/ArithmeticException.html "class in java.lang")` \- if the calculation exceeds the capacity of `Duration`
* #### get
public long get([TemporalUnit](../../java/time/temporal/TemporalUnit.html "interface in java.time.temporal") unit)
Gets the value of the requested unit.
This returns a value for each of the two supported units,[SECONDS](../../java/time/temporal/ChronoUnit.html#SECONDS) and [NANOS](../../java/time/temporal/ChronoUnit.html#NANOS). All other units throw an exception.
Specified by:
`[get](../../java/time/temporal/TemporalAmount.html#get-java.time.temporal.TemporalUnit-)` in interface `[TemporalAmount](../../java/time/temporal/TemporalAmount.html "interface in java.time.temporal")`
Parameters:
`unit` \- the `TemporalUnit` for which to return the value
Returns:
the long value of the unit
Throws:
`[DateTimeException](../../java/time/DateTimeException.html "class in java.time")` \- if the unit is not supported
`[UnsupportedTemporalTypeException](../../java/time/temporal/UnsupportedTemporalTypeException.html "class in java.time.temporal")` \- if the unit is not supported
* #### getUnits
public [List](../../java/util/List.html "interface in java.util")<[TemporalUnit](../../java/time/temporal/TemporalUnit.html "interface in java.time.temporal")> getUnits()
Gets the set of units supported by this duration.
The supported units are [SECONDS](../../java/time/temporal/ChronoUnit.html#SECONDS), and [NANOS](../../java/time/temporal/ChronoUnit.html#NANOS). They are returned in the order seconds, nanos.
This set can be used in conjunction with [get(TemporalUnit)](../../java/time/Duration.html#get-java.time.temporal.TemporalUnit-) to access the entire state of the duration.
Specified by:
`[getUnits](../../java/time/temporal/TemporalAmount.html#getUnits--)` in interface `[TemporalAmount](../../java/time/temporal/TemporalAmount.html "interface in java.time.temporal")`
Returns:
a list containing the seconds and nanos units, not null
* #### isZero
public boolean isZero()
Checks if this duration is zero length.
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.
Returns:
true if this duration has a total length equal to zero
* #### isNegative
public boolean isNegative()
Checks if this duration is negative, excluding zero.
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.
Returns:
true if this duration has a total length less than zero
* #### getSeconds
public long getSeconds()
Gets the number of seconds in this duration.
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()](../../java/time/Duration.html#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.
Returns:
the whole seconds part of the length of the duration, positive or negative
* #### getNano
public int getNano()
Gets the number of nanoseconds within the second in this duration.
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()](../../java/time/Duration.html#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.
Returns:
the nanoseconds within the second part of the length of the duration, from 0 to 999,999,999
* #### withSeconds
public [Duration](../../java/time/Duration.html "class in java.time") withSeconds(long seconds)
Returns a copy of this duration with the specified amount of seconds.
This 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.
Parameters:
`seconds` \- the seconds to represent, may be negative
Returns:
a `Duration` based on this period with the requested seconds, not null
* #### withNanos
public [Duration](../../java/time/Duration.html "class in java.time") withNanos(int nanoOfSecond)
Returns a copy of this duration with the specified nano-of-second.
This 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.
Parameters:
`nanoOfSecond` \- the nano-of-second to represent, from 0 to 999,999,999
Returns:
a `Duration` based on this period with the requested nano-of-second, not null
Throws:
`[DateTimeException](../../java/time/DateTimeException.html "class in java.time")` \- if the nano-of-second is invalid
* #### plus
public [Duration](../../java/time/Duration.html "class in java.time") plus([Duration](../../java/time/Duration.html "class in java.time") duration)
Returns a copy of this duration with the specified duration added.
This instance is immutable and unaffected by this method call.
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](../../java/lang/ArithmeticException.html "class in java.lang")` \- if numeric overflow occurs
* #### plus
public [Duration](../../java/time/Duration.html "class in java.time") plus(long amountToAdd,
[TemporalUnit](../../java/time/temporal/TemporalUnit.html "interface in java.time.temporal") unit)
Returns a copy of this duration with the specified duration added.
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](../../java/time/temporal/TemporalUnit.html#isDurationEstimated--) or be [ChronoUnit.DAYS](../../java/time/temporal/ChronoUnit.html#DAYS) which is treated as 24 hours. Other units throw an exception.
This instance is immutable and unaffected by this method call.
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](../../java/time/temporal/UnsupportedTemporalTypeException.html "class in java.time.temporal")` \- if the unit is not supported
`[ArithmeticException](../../java/lang/ArithmeticException.html "class in java.lang")` \- if numeric overflow occurs
* #### plusDays
public [Duration](../../java/time/Duration.html "class in java.time") plusDays(long daysToAdd)
Returns a copy of this duration with the specified duration in standard 24 hour days added.
The number of days is multiplied by 86400 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.
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](../../java/lang/ArithmeticException.html "class in java.lang")` \- if numeric overflow occurs
* #### plusHours
public [Duration](../../java/time/Duration.html "class in java.time") plusHours(long hoursToAdd)
Returns a copy of this duration with the specified duration in hours added.
This instance is immutable and unaffected by this method call.
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](../../java/lang/ArithmeticException.html "class in java.lang")` \- if numeric overflow occurs
* #### plusMinutes
public [Duration](../../java/time/Duration.html "class in java.time") plusMinutes(long minutesToAdd)
Returns a copy of this duration with the specified duration in minutes added.
This instance is immutable and unaffected by this method call.
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](../../java/lang/ArithmeticException.html "class in java.lang")` \- if numeric overflow occurs
* #### plusSeconds
public [Duration](../../java/time/Duration.html "class in java.time") plusSeconds(long secondsToAdd)
Returns a copy of this duration with the specified duration in seconds added.
This instance is immutable and unaffected by this method call.
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](../../java/lang/ArithmeticException.html "class in java.lang")` \- if numeric overflow occurs
* #### plusMillis
public [Duration](../../java/time/Duration.html "class in java.time") plusMillis(long millisToAdd)
Returns a copy of this duration with the specified duration in milliseconds added.
This instance is immutable and unaffected by this method call.
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](../../java/lang/ArithmeticException.html "class in java.lang")` \- if numeric overflow occurs
* #### plusNanos
public [Duration](../../java/time/Duration.html "class in java.time") plusNanos(long nanosToAdd)
Returns a copy of this duration with the specified duration in nanoseconds added.
This instance is immutable and unaffected by this method call.
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](../../java/lang/ArithmeticException.html "class in java.lang")` \- if numeric overflow occurs
* #### minus
public [Duration](../../java/time/Duration.html "class in java.time") minus([Duration](../../java/time/Duration.html "class in java.time") duration)
Returns a copy of this duration with the specified duration subtracted.
This instance is immutable and unaffected by this method call.
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](../../java/lang/ArithmeticException.html "class in java.lang")` \- if numeric overflow occurs
* #### minus
public [Duration](../../java/time/Duration.html "class in java.time") minus(long amountToSubtract,
[TemporalUnit](../../java/time/temporal/TemporalUnit.html "interface in java.time.temporal") unit)
Returns a copy of this duration with the specified duration subtracted.
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](../../java/time/temporal/TemporalUnit.html#isDurationEstimated--) or be [ChronoUnit.DAYS](../../java/time/temporal/ChronoUnit.html#DAYS) which is treated as 24 hours. Other units throw an exception.
This instance is immutable and unaffected by this method call.
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](../../java/lang/ArithmeticException.html "class in java.lang")` \- if numeric overflow occurs
* #### minusDays
public [Duration](../../java/time/Duration.html "class in java.time") minusDays(long daysToSubtract)
Returns a copy of this duration with the specified duration in standard 24 hour days subtracted.
The number of days is multiplied by 86400 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.
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](../../java/lang/ArithmeticException.html "class in java.lang")` \- if numeric overflow occurs
* #### minusHours
public [Duration](../../java/time/Duration.html "class in java.time") minusHours(long hoursToSubtract)
Returns a copy of this duration with the specified duration in hours subtracted.
The number of hours is multiplied by 3600 to obtain the number of seconds to subtract.
This instance is immutable and unaffected by this method call.
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](../../java/lang/ArithmeticException.html "class in java.lang")` \- if numeric overflow occurs
* #### minusMinutes
public [Duration](../../java/time/Duration.html "class in java.time") minusMinutes(long minutesToSubtract)
Returns a copy of this duration with the specified duration in minutes subtracted.
The number of hours is multiplied by 60 to obtain the number of seconds to subtract.
This instance is immutable and unaffected by this method call.
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](../../java/lang/ArithmeticException.html "class in java.lang")` \- if numeric overflow occurs
* #### minusSeconds
public [Duration](../../java/time/Duration.html "class in java.time") minusSeconds(long secondsToSubtract)
Returns a copy of this duration with the specified duration in seconds subtracted.
This instance is immutable and unaffected by this method call.
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](../../java/lang/ArithmeticException.html "class in java.lang")` \- if numeric overflow occurs
* #### minusMillis
public [Duration](../../java/time/Duration.html "class in java.time") minusMillis(long millisToSubtract)
Returns a copy of this duration with the specified duration in milliseconds subtracted.
This instance is immutable and unaffected by this method call.
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](../../java/lang/ArithmeticException.html "class in java.lang")` \- if numeric overflow occurs
* #### minusNanos
public [Duration](../../java/time/Duration.html "class in java.time") minusNanos(long nanosToSubtract)
Returns a copy of this duration with the specified duration in nanoseconds subtracted.
This instance is immutable and unaffected by this method call.
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](../../java/lang/ArithmeticException.html "class in java.lang")` \- if numeric overflow occurs
* #### multipliedBy
public [Duration](../../java/time/Duration.html "class in java.time") multipliedBy(long multiplicand)
Returns a copy of this duration multiplied by the scalar.
This instance is immutable and unaffected by this method call.
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](../../java/lang/ArithmeticException.html "class in java.lang")` \- if numeric overflow occurs
* #### dividedBy
public [Duration](../../java/time/Duration.html "class in java.time") dividedBy(long divisor)
Returns a copy of this duration divided by the specified value.
This instance is immutable and unaffected by this method call.
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](../../java/lang/ArithmeticException.html "class in java.lang")` \- if the divisor is zero or if numeric overflow occurs
* #### dividedBy
public long dividedBy([Duration](../../java/time/Duration.html "class in java.time") divisor)
Returns number of whole times a specified Duration occurs within this Duration.
This instance is immutable and unaffected by this method call.
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](../../java/lang/ArithmeticException.html "class in java.lang")` \- if the divisor is zero, or if numeric overflow occurs
Since:
9
* #### negated
public [Duration](../../java/time/Duration.html "class in java.time") negated()
Returns a copy of this duration with the length 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.
Returns:
a `Duration` based on this duration with the amount negated, not null
Throws:
`[ArithmeticException](../../java/lang/ArithmeticException.html "class in java.lang")` \- if numeric overflow occurs
* #### abs
public [Duration](../../java/time/Duration.html "class in java.time") abs()
Returns a copy of this duration with a positive length.
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.
Returns:
a `Duration` based on this duration with an absolute length, not null
Throws:
`[ArithmeticException](../../java/lang/ArithmeticException.html "class in java.lang")` \- if numeric overflow occurs
* #### addTo
public [Temporal](../../java/time/temporal/Temporal.html "interface in java.time.temporal") addTo([Temporal](../../java/time/temporal/Temporal.html "interface in java.time.temporal") temporal)
Adds this duration to the specified temporal object.
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(TemporalAmount)](../../java/time/temporal/Temporal.html#plus-java.time.temporal.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.
Specified by:
`[addTo](../../java/time/temporal/TemporalAmount.html#addTo-java.time.temporal.Temporal-)` in interface `[TemporalAmount](../../java/time/temporal/TemporalAmount.html "interface in java.time.temporal")`
Parameters:
`temporal` \- the temporal object to adjust, not null
Returns:
an object of the same type with the adjustment made, not null
Throws:
`[DateTimeException](../../java/time/DateTimeException.html "class in java.time")` \- if unable to add
`[ArithmeticException](../../java/lang/ArithmeticException.html "class in java.lang")` \- if numeric overflow occurs
* #### subtractFrom
public [Temporal](../../java/time/temporal/Temporal.html "interface in java.time.temporal") subtractFrom([Temporal](../../java/time/temporal/Temporal.html "interface in java.time.temporal") temporal)
Subtracts this duration from the specified temporal object.
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(TemporalAmount)](../../java/time/temporal/Temporal.html#minus-java.time.temporal.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.
Specified by:
`[subtractFrom](../../java/time/temporal/TemporalAmount.html#subtractFrom-java.time.temporal.Temporal-)` in interface `[TemporalAmount](../../java/time/temporal/TemporalAmount.html "interface in java.time.temporal")`
Parameters:
`temporal` \- the temporal object to adjust, not null
Returns:
an object of the same type with the adjustment made, not null
Throws:
`[DateTimeException](../../java/time/DateTimeException.html "class in java.time")` \- if unable to subtract
`[ArithmeticException](../../java/lang/ArithmeticException.html "class in java.lang")` \- if numeric overflow occurs
* #### toDays
public long toDays()
Gets the number of days in this duration.
This returns the total number of days in the duration by dividing the number of seconds by 86400\. This is based on the standard definition of a day as 24 hours.
This instance is immutable and unaffected by this method call.
Returns:
the number of days in the duration, may be negative
* #### toHours
public long toHours()
Gets the number of hours in this duration.
This returns the total number of hours in the duration by dividing the number of seconds by 3600.
This instance is immutable and unaffected by this method call.
Returns:
the number of hours in the duration, may be negative
* #### toMinutes
public long toMinutes()
Gets the number of minutes in this duration.
This 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.
Returns:
the number of minutes in the duration, may be negative
* #### toSeconds
public long toSeconds()
Gets the number of seconds in this duration.
This returns the total number of whole seconds in the duration.
This instance is immutable and unaffected by this method call.
Returns:
the whole seconds part of the length of the duration, positive or negative
Since:
9
* #### toMillis
public long toMillis()
Converts this duration to the total length in milliseconds.
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.
Returns:
the total length of the duration in milliseconds
Throws:
`[ArithmeticException](../../java/lang/ArithmeticException.html "class in java.lang")` \- if numeric overflow occurs
* #### toNanos
public long toNanos()
Converts this duration to the total length in nanoseconds expressed as a `long`.
If this duration is too large to fit in a `long` nanoseconds, then an exception is thrown.
Returns:
the total length of the duration in nanoseconds
Throws:
`[ArithmeticException](../../java/lang/ArithmeticException.html "class in java.lang")` \- if numeric overflow occurs
* #### toDaysPart
public long toDaysPart()
Extracts the number of days in the duration.
This returns the total number of days in the duration by dividing the number of seconds by 86400\. This is based on the standard definition of a day as 24 hours.
This instance is immutable and unaffected by this method call.
Returns:
the number of days in the duration, may be negative
Since:
9
* #### toHoursPart
public int toHoursPart()
Extracts the number of hours part in the duration.
This returns the number of remaining hours when dividing [toHours()](../../java/time/Duration.html#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.
Returns:
the number of hours part in the duration, may be negative
Since:
9
* #### toMinutesPart
public int toMinutesPart()
Extracts the number of minutes part in the duration.
This returns the number of remaining minutes when dividing [toMinutes()](../../java/time/Duration.html#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.
Returns:
the number of minutes parts in the duration, may be negative
Since:
9
* #### toSecondsPart
public int toSecondsPart()
Extracts the number of seconds part in the duration.
This returns the remaining seconds when dividing [toSeconds()](../../java/time/Duration.html#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.
Returns:
the number of seconds parts in the duration, may be negative
Since:
9
* #### toMillisPart
public int toMillisPart()
Extracts the number of milliseconds part of the duration.
This 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()](../../java/time/Duration.html#getNano--) and [getSeconds()](../../java/time/Duration.html#getSeconds--).
This instance is immutable and unaffected by this method call.
Returns:
the number of milliseconds part of the duration.
Since:
9
* #### toNanosPart
public int toNanosPart()
Get the nanoseconds part within seconds of the duration.
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()](../../java/time/Duration.html#getNano--) and [getSeconds()](../../java/time/Duration.html#getSeconds--).
This instance is immutable and unaffected by this method call.
Returns:
the nanoseconds within the second part of the length of the duration, from 0 to 999,999,999
Since:
9
* #### truncatedTo
public [Duration](../../java/time/Duration.html "class in java.time") truncatedTo([TemporalUnit](../../java/time/temporal/TemporalUnit.html "interface in java.time.temporal") unit)
Returns a copy of this `Duration` truncated to the specified unit.
Truncating 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](../../java/time/temporal/ChronoUnit.html#MINUTES) unit will round down to the nearest minute, setting the seconds and nanoseconds to zero.
The unit must have a [duration](../../java/time/temporal/TemporalUnit.html#getDuration--) that divides into the length of a standard day without remainder. This includes all supplied time units on [ChronoUnit](../../java/time/temporal/ChronoUnit.html "enum in java.time.temporal") and[DAYS](../../java/time/temporal/ChronoUnit.html#DAYS). Other ChronoUnits throw an exception.
This instance is immutable and unaffected by this method call.
Parameters:
`unit` \- the unit to truncate to, not null
Returns:
a `Duration` based on this duration with the time truncated, not null
Throws:
`[DateTimeException](../../java/time/DateTimeException.html "class in java.time")` \- if the unit is invalid for truncation
`[UnsupportedTemporalTypeException](../../java/time/temporal/UnsupportedTemporalTypeException.html "class in java.time.temporal")` \- if the unit is not supported
Since:
9
* #### compareTo
public int compareTo([Duration](../../java/time/Duration.html "class in java.time") otherDuration)
Compares this duration to the specified `Duration`.
The comparison is based on the total length of the durations. It is "consistent with equals", as defined by [Comparable](../../java/lang/Comparable.html "interface in java.lang").
Specified by:
`[compareTo](../../java/lang/Comparable.html#compareTo-T-)` in interface `[Comparable](../../java/lang/Comparable.html "interface in java.lang")<[Duration](../../java/time/Duration.html "class in java.time")>`
Parameters:
`otherDuration` \- the other duration to compare to, not null
Returns:
the comparator value, negative if less, positive if greater
* #### equals
public boolean equals([Object](../../java/lang/Object.html "class in java.lang") otherDuration)
Checks if this duration is equal to the specified `Duration`.
The comparison is based on the total length of the durations.
Overrides:
`[equals](../../java/lang/Object.html#equals-java.lang.Object-)` in class `[Object](../../java/lang/Object.html "class in java.lang")`
Parameters:
`otherDuration` \- the other duration, null returns false
Returns:
true if the other duration is equal to this one
See Also:
[Object.hashCode()](../../java/lang/Object.html#hashCode--), [HashMap](../../java/util/HashMap.html "class in java.util")
* #### hashCode
public int hashCode()
A hash code for this duration.
Overrides:
`[hashCode](../../java/lang/Object.html#hashCode--)` in class `[Object](../../java/lang/Object.html "class in java.lang")`
Returns:
a suitable hash code
See Also:
[Object.equals(java.lang.Object)](../../java/lang/Object.html#equals-java.lang.Object-), [System.identityHashCode(java.lang.Object)](../../java/lang/System.html#identityHashCode-java.lang.Object-)
* #### toString
public [String](../../java/lang/String.html "class in java.lang") toString()
A string representation of this duration using ISO-8601 seconds based representation, such as `PT8H6M12.345S`.
The 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 * 3600 seconds) -- "PT10H"
"2 days" (2 * 86400 seconds) -- "PT48H"
Note that multiples of 24 hours are not output as days to avoid confusion with `Period`.
Overrides:
`[toString](../../java/lang/Object.html#toString--)` in class `[Object](../../java/lang/Object.html "class in java.lang")`
Returns:
an ISO-8601 representation of this duration, not null