public class Duration extends Object
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:
long| Modifier and Type | Class and Description |
|---|---|
static class |
Duration.FormatStyle
Enumerates different time format styles, each one with particular behaviors.
|
| Modifier and Type | Method and Description |
|---|---|
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() |
static Duration |
of(long amount,
TimeUnit timeUnit)
Obtains a
Duration representing an amount in the specified time unit. |
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 style. |
String |
toString(Duration.FormatStyle style)
Returns a string representation of this
Duration with custom format style. |
String |
toString(Duration.FormatStyle style,
boolean printLegend)
Returns a string representation of this
Duration with custom format style. |
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 with:
[hours = 0, minutes = 1, seconds = 5, nanoseconds = 0]
amount - the amount of the duration, measured in term of the timeUnit argument;
it must be a positive integertimeUnit - the unit that the amount argument is measured in; cannot be nullDuration, not null.IllegalArgumentException - if the specified amount is negativeNullPointerException - if the specified timeUnit 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 String toString()
Duration in default format style.public String toString(Duration.FormatStyle style)
Duration with custom format style.
Note: This is equivalent to calling: toString(style, true)
style - the Duration.FormatStyle to be appliedpublic String toString(Duration.FormatStyle style, boolean printLegend)
Duration with custom format style.style - the Duration.FormatStyle to be appliedprintLegend - a flag indicating whether or not to include the 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 unitpublic double toTimeUnit(TimeUnit timeUnit, int scale)
timeUnit - the target time unitscale - a positive number indicates the number of decimal places to maintain;
if negative, the default scale will be appliedpublic double toSeconds()
Note: This is equivalent to calling: toTimeUnit(TimeUnit.SECONDS, 9)
Copyright © 2020. All rights reserved.