Constant for a duration of zero.
Obtains an instance of Duration
representing the duration between two instants.
Obtains an instance of Duration
representing the duration between two instants.
Obtains a Duration
representing the duration between two instants.
This calculates the duration between two temporal objects of the same type.
The difference in seconds is calculated using TemporalUnit)
.
The difference in nanoseconds is calculated using by querying the
NANO_OF_SECOND
field.
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() on the result.
the start instant, inclusive, not null
the end instant, exclusive, not null
a { @code Duration}, not null
ArithmeticException
if the calculation exceeds the capacity of { @code Duration}
DateTimeException
if the seconds between the temporals cannot be obtained
Obtains an instance of Duration
from an amount.
Obtains an instance of Duration
from an 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 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.
the amount to convert, not null
a { @code Duration}, not null
ArithmeticException
if a numeric overflow occurs
DateTimeException
if the amount cannot be converted
Obtains an instance of Duration
from a duration in the specified unit.
Obtains an instance of Duration
from a duration in the specified unit.
The parameters represent the two parts of a phrase like '6 Hours'. For example:
Duration.of(3, SECONDS); Duration.of(465, HOURS);Only a subset of units are accepted by this method. The unit must either have an
exact duration
or
be ChronoUnit#DAYS
which is treated as 24 hours. Other units throw an exception.
the amount of the duration, measured in terms of the unit, positive or negative
the unit that the duration is measured in, must have an exact duration, not null
a { @code Duration}, not null
ArithmeticException
if a numeric overflow occurs
DateTimeException
if the period unit has an estimated duration
Obtains an instance of Duration
from a number of standard 24 hour days.
Obtains an instance of Duration
from a number of standard 24 hour days.
The seconds are calculated based on the standard definition of a day, where each day is 86400 seconds which implies a 24 hour day. The nanosecond in second field is set to zero.
the number of days, positive or negative
a { @code Duration}, not null
ArithmeticException
if the input days exceeds the capacity of { @code Duration}
Obtains an instance of Duration
from a number of standard length hours.
Obtains an instance of Duration
from a number of standard length hours.
The seconds are calculated based on the standard definition of an hour, where each hour is 3600 seconds. The nanosecond in second field is set to zero.
the number of hours, positive or negative
a { @code Duration}, not null
ArithmeticException
if the input hours exceeds the capacity of { @code Duration}
Obtains an instance of Duration
from a number of milliseconds.
Obtains an instance of Duration
from a number of milliseconds.
The seconds and nanoseconds are extracted from the specified milliseconds.
the number of milliseconds, positive or negative
a { @code Duration}, not null
Obtains an instance of Duration
from a number of standard length minutes.
Obtains an instance of Duration
from a number of standard length minutes.
The seconds are calculated based on the standard definition of a minute, where each minute is 60 seconds. The nanosecond in second field is set to zero.
the number of minutes, positive or negative
a { @code Duration}, not null
ArithmeticException
if the input minutes exceeds the capacity of { @code Duration}
Obtains an instance of Duration
from a number of nanoseconds.
Obtains an instance of Duration
from a number of nanoseconds.
The seconds and nanoseconds are extracted from the specified nanoseconds.
the number of nanoseconds, positive or negative
a { @code Duration}, not null
Obtains an instance of Duration
from a number of seconds
and an adjustment in nanoseconds.
Obtains an instance of Duration
from a number of seconds
and an adjustment in nanoseconds.
This method allows an arbitrary number of nanoseconds to be passed in. The factory will alter the values of the second and nanosecond in order to ensure that the stored nanosecond is in the range 0 to 999,999,999. For example, the following will result in the exactly the same duration:
Duration.ofSeconds(3, 1); Duration.ofSeconds(4, -999_999_999); Duration.ofSeconds(2, 1000_000_001);
the number of seconds, positive or negative
the nanosecond adjustment to the number of seconds, positive or negative
a { @code Duration}, not null
ArithmeticException
if the adjustment causes the seconds to exceed the capacity of { @code Duration}
Obtains an instance of Duration
from a number of seconds.
Obtains an instance of Duration
from a number of seconds.
The nanosecond in second field is set to zero.
the number of seconds, positive or negative
a { @code Duration}, not null
Obtains a Duration
from a text string such as PnDTnHnMn.nS
.
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" "P-6H3M" -> parses as "-6 hours and +3 minutes" "-P6H3M" -> parses as "-6 hours and -3 minutes" "-P-6H+3M" -> parses as "+6 hours and -3 minutes"
the text to parse, not null
the parsed duration, not null
DateTimeParseException
if the text cannot be parsed to a duration