GeezerVison is showing docs.oracle.com — Duration (Java Platform SE 8 )
Java™ Platform Standard Ed. 8
compact1, compact2, compact3
java.time
Class Duration
-
-
Field Summary
Fields
| Modifier and Type |
Field |
Description |
static Duration |
ZERO |
Constant for a duration of zero.
|
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods
| Modifier and Type |
Method |
Description |
Duration |
abs() |
Returns a copy of this duration with a positive length.
|
Temporal |
addTo(Temporal temporal) |
Adds this duration to the specified temporal object.
|
static Duration |
between(Temporal startInclusive,
Temporal endExclusive) |
Obtains a Duration representing the duration between two temporal objects.
|
int |
compareTo(Duration otherDuration) |
Compares this duration to the specified Duration.
|
Duration |
dividedBy(long divisor) |
Returns a copy of this duration divided by the specified value.
|
boolean |
equals(Object otherDuration) |
Checks if this duration is equal to the specified Duration.
|
static Duration |
from(TemporalAmount amount) |
Obtains an instance of Duration from a temporal amount.
|
long |
get(TemporalUnit unit) |
Gets the value of the requested unit.
|
int |
getNano() |
Gets the number of nanoseconds within the second in this duration.
|
long |
getSeconds() |
Gets the number of seconds in this duration.
|
List<TemporalUnit> |
getUnits() |
Gets the set of units supported by this duration.
|
int |
hashCode() |
A hash code for this duration.
|
boolean |
isNegative() |
Checks if this duration is negative, excluding zero.
|
boolean |
isZero() |
Checks if this duration is zero length.
|
Duration |
minus(Duration duration) |
Returns a copy of this duration with the specified duration subtracted.
|
Duration |
minus(long amountToSubtract,
TemporalUnit unit) |
Returns a copy of this duration with the specified duration subtracted.
|
Duration |
minusDays(long daysToSubtract) |
Returns a copy of this duration with the specified duration in standard 24 hour days subtracted.
|
Duration |
minusHours(long hoursToSubtract) |
Returns a copy of this duration with the specified duration in hours subtracted.
|
Duration |
minusMillis(long millisToSubtract) |
Returns a copy of this duration with the specified duration in milliseconds subtracted.
|
Duration |
minusMinutes(long minutesToSubtract) |
Returns a copy of this duration with the specified duration in minutes subtracted.
|
Duration |
minusNanos(long nanosToSubtract) |
Returns a copy of this duration with the specified duration in nanoseconds subtracted.
|
Duration |
minusSeconds(long secondsToSubtract) |
Returns a copy of this duration with the specified duration in seconds subtracted.
|
Duration |
multipliedBy(long multiplicand) |
Returns a copy of this duration multiplied by the scalar.
|
Duration |
negated() |
Returns a copy of this duration with the length negated.
|
static Duration |
of(long amount,
TemporalUnit unit) |
Obtains a Duration representing an amount in the specified unit.
|
static Duration |
ofDays(long days) |
Obtains a Duration representing a number of standard 24 hour days.
|
static Duration |
ofHours(long hours) |
Obtains a Duration representing a number of standard hours.
|
static Duration |
ofMillis(long millis) |
Obtains a Duration representing a number of milliseconds.
|
static Duration |
ofMinutes(long minutes) |
Obtains a Duration representing a number of standard minutes.
|
static Duration |
ofNanos(long nanos) |
Obtains a Duration representing a number of nanoseconds.
|
static Duration |
ofSeconds(long seconds) |
Obtains a Duration representing a number of seconds.
|
static Duration |
ofSeconds(long seconds,
long nanoAdjustment) |
Obtains a Duration representing a number of seconds and an
adjustment in nanoseconds.
|
static Duration |
parse(CharSequence text) |
Obtains a Duration from a text string such as PnDTnHnMn.nS.
|
Duration |
plus(Duration duration) |
Returns a copy of this duration with the specified duration added.
|
Duration |
plus(long amountToAdd,
TemporalUnit unit) |
Returns a copy of this duration with the specified duration added.
|
Duration |
plusDays(long daysToAdd) |
Returns a copy of this duration with the specified duration in standard 24 hour days added.
|
Duration |
plusHours(long hoursToAdd) |
Returns a copy of this duration with the specified duration in hours added.
|
Duration |
plusMillis(long millisToAdd) |
Returns a copy of this duration with the specified duration in milliseconds added.
|
Duration |
plusMinutes(long minutesToAdd) |
Returns a copy of this duration with the specified duration in minutes added.
|
Duration |
plusNanos(long nanosToAdd) |
Returns a copy of this duration with the specified duration in nanoseconds added.
|
Duration |
plusSeconds(long secondsToAdd) |
Returns a copy of this duration with the specified duration in seconds added.
|
Temporal |
subtractFrom(Temporal temporal) |
Subtracts this duration from the specified temporal object.
|
long |
toDays() |
Gets the number of days in this duration.
|
long |
toHours() |
Gets the number of hours in this duration.
|
long |
toMillis() |
Converts this duration to the total length in milliseconds.
|
long |
toMinutes() |
Gets the number of minutes in this duration.
|
long |
toNanos() |
Converts this duration to the total length in nanoseconds expressed as a long.
|
String |
toString() |
A string representation of this duration using ISO-8601 seconds
based representation, such as PT8H6M12.345S.
|
Duration |
withNanos(int nanoOfSecond) |
Returns a copy of this duration with the specified nano-of-second.
|
Duration |
withSeconds(long seconds) |
Returns a copy of this duration with the specified amount of seconds.
|
-
-
Method Detail
-
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
-
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().
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().
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
-
abs
public Duration 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 - 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
-
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 - 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 - if numeric overflow occurs
-
toString
public String 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 i 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 in class Object
- Returns:
- an ISO-8601 representation of this duration, not null
Java™ Platform Standard Ed. 8
Submit a bug or feature For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples. Copyright © 1993, 2026, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.
|