public class Duration extends Object implements Comparable<Duration>
A time-based amount, such as '01:59:59.987654321 (H:M:S.ns)'.
This class models a quantity or amount of time in terms of:
longThis class is immutable and thread-safe.
| Modifier and Type | Field and Description |
|---|---|
static Duration |
ZERO
Constant for a duration of zero.
|
| Modifier and Type | Method and Description |
|---|---|
int |
compareTo(Duration otherDuration)
Compares this duration to the specified
Duration based on the total length of
the durations, as defined by Comparable. |
Duration |
dividedBy(long divisor)
Returns a copy of this duration divided by the specified value.
|
boolean |
equals(Object object) |
long |
getHours()
Returns the number of hours in this duration.
|
int |
getMinutes()
Returns the number of minutes within the hour in this duration.
|
int |
getNanoseconds()
Returns the number of nanoseconds within the second in this duration.
|
int |
getSeconds()
Returns the number of seconds within the minute in this duration.
|
int |
hashCode() |
boolean |
isZero()
Checks if this duration is zero length.
|
static Duration |
of(long amount,
TimeUnit timeUnit)
Obtains a
Duration representing an amount in the specified time unit. |
Duration |
plus(Duration duration)
Returns a copy of this duration with the specified duration added.
|
Duration |
plus(long amount,
TimeUnit timeUnit)
Returns a copy of this duration with the specified duration added.
|
static Duration |
sum(Duration duration1,
Duration duration2)
Returns the sum of two durations.
|
double |
toSeconds()
Converts this duration to the total length in seconds and fractional nanoseconds, with
a fixed scale of 9, so nanoseconds precision is preserved.
|
String |
toString()
Returns a string representation of this
Duration in default format. |
String |
toString(DurationFormat format)
Returns a string representation of this
Duration with a specific format. |
String |
toString(DurationFormat format,
boolean printLegend)
Returns a string representation of this
Duration with a specific format. |
double |
toTimeUnit(TimeUnit timeUnit)
Converts this duration to the total length in a given time unit, with default scale.
|
double |
toTimeUnit(TimeUnit timeUnit,
int scale)
Converts this duration to the total length in a given time unit, with a custom scale.
|
public static final Duration ZERO
public static Duration of(long amount, TimeUnit timeUnit)
Duration representing an amount in the specified time unit.
For example, calling Duration.of(65, SECONDS) produces an object in which the
getters behave according to the example below:
duration.getHours() //returns: 0 duration.getMinutes() //returns: 1 duration.getSeconds() //returns: 5 duration.getNanoseconds() //returns: 0
amount - the amount of the duration, measured in terms of the time unit argumenttimeUnit - the unit that the amount argument is measured in, not nullDuration, not nullNullPointerException - if the specified time unit is nullpublic long getHours()
The hours field is a value from 0 to the maximum value that can be held in a
long.
public int getMinutes()
The minutes field is a value from 0 to 59, which is an adjustment to the length in
hours, retrieved by calling getHours().
public int getSeconds()
The seconds field is a value from 0 to 59, which is an adjustment to the length in minutes, and hours, each part separately retrieved with their respective getter methods.
public int getNanoseconds()
The nanoseconds field is a value from 0 to 999,999,999, which is an adjustment to the length in seconds, minutes, and hours, each part separately retrieved with their respective getter methods.
public boolean isZero()
public String toString()
Duration in default format.public String toString(DurationFormat format)
Duration with a specific format.
Note: This is equivalent to calling:
DurationFormatter.format(duration, format)
format - the DurationFormat to be appliedpublic String toString(DurationFormat format, boolean printLegend)
Duration with a specific format.format - the DurationFormat to be appliedprintLegend - a flag indicating whether or not to include a legend in the
generated stringpublic double toTimeUnit(TimeUnit timeUnit)
Note: The number of decimal places applied is determined by
ConfigurationHolder.getConfiguration().getScale() and may be changed by calling
Performetrics.setScale(int).
timeUnit - the target time unit, not nullNullPointerException - if the specified time unit is nullpublic double toTimeUnit(TimeUnit timeUnit, int scale)
timeUnit - the target time unit, not nullscale - a positive number indicates the number of decimal places to maintain;
if negative, the default scale will be appliedNullPointerException - if the specified time unit is nullpublic double toSeconds()
Note: This is equivalent to calling: toTimeUnit(TimeUnit.SECONDS, 9)
public Duration plus(Duration duration)
This instance is immutable and unaffected by this method call.
duration - the duration to add, not nullDuration based on this duration with the specified duration added,
not nullNullPointerException - if the specified duration is nullArithmeticException - if numeric overflow occurspublic Duration plus(long amount, TimeUnit timeUnit)
This instance is immutable and unaffected by this method call.
amount - the amount to add, measured in terms of the timeUnit argumenttimeUnit - the unit that the amount to add is measured in, not nullDuration based on this duration with the specified duration added,
not nullNullPointerException - if the specified time unit is nullArithmeticException - if numeric overflow occurspublic static Duration sum(Duration duration1, Duration duration2)
This instances are immutable and unaffected by this method call.
duration1 - the first duration to add, not nullduration2 - the second duration to add, not nullDuration resulting by adding two durations, not nullNullPointerException - if the specified duration is nullArithmeticException - if numeric overflow occurspublic Duration dividedBy(long divisor)
This instance is immutable and unaffected by this method call.
divisor - the value to divide the duration by, positive or negative, not zeroArithmeticException - if the divisor is zero or if numeric overflow occurspublic int compareTo(Duration otherDuration)
Duration based on the total length of
the durations, as defined by Comparable.compareTo in interface Comparable<Duration>otherDuration - the other duration to compare to; not nullCopyright © 2021. All rights reserved.