public class Duration extends java.lang.Object implements java.lang.Comparable<Duration>, java.io.Serializable
Modifier and Type | Field and Description |
---|---|
static Duration |
FIVE_MINUTES |
static Duration |
FIVE_SECONDS |
static Duration |
ONE_DAY |
static Duration |
ONE_HOUR |
static Duration |
ONE_MILLISECOND |
static Duration |
ONE_MINUTE |
static Duration |
ONE_SECOND |
static Duration |
PRACTICALLY_FOREVER
longest supported duration, 2^{63}-1 nanoseconds, approx ten billion seconds, or 300 years
|
static Duration |
TEN_SECONDS |
static Duration |
THIRTY_SECONDS |
static Duration |
TWO_MINUTES |
static Duration |
ZERO |
Constructor and Description |
---|
Duration(long value,
java.util.concurrent.TimeUnit unit) |
Modifier and Type | Method and Description |
---|---|
Duration |
add(Duration other) |
int |
compareTo(Duration o) |
CountdownTimer |
countdownTimer()
returns a new started
CountdownTimer with this duration |
static Duration |
days(java.lang.Number n)
creates new
Duration instance of the given length of time |
boolean |
equals(java.lang.Object o) |
Duration |
half() |
int |
hashCode() |
static Duration |
hours(java.lang.Number n)
creates new
Duration instance of the given length of time |
boolean |
isLongerThan(Duration x) |
boolean |
isLongerThan(Stopwatch stopwatch) |
boolean |
isNegative() |
boolean |
isPositive() |
boolean |
isShorterThan(Duration x) |
boolean |
isShorterThan(Stopwatch stopwatch) |
Duration |
lowerBound(Duration alternateMinimumValue)
returns the larger of this value or the argument
|
static Duration |
max(Duration first,
Duration second) |
static Duration |
micros(java.lang.Number n)
creates new
Duration instance of the given length of time |
static Duration |
millis(java.lang.Number n)
creates new
Duration instance of the given length of time |
static <any> |
millisToStringRounded() |
static Duration |
min(Duration first,
Duration second) |
static Duration |
minutes(java.lang.Number n)
creates new
Duration instance of the given length of time |
Duration |
multiply(double d)
as #multiply(long), but approximate due to the division (nano precision)
|
Duration |
multiply(long x) |
long |
nanos()
number of nanoseconds of this duration
|
static Duration |
nanos(java.lang.Number n)
creates new
Duration instance of the given length of time |
static Duration |
of(long value,
java.util.concurrent.TimeUnit unit) |
static Duration |
of(java.lang.Object o)
Converts the given object to a Duration by attempting the following in order:
return null if the object is null
parse the object as a string
treat numbers as milliseconds
obtain the elapsed time of a Stopwatch
invoke the object's toMilliseconds method, if one exists.
If none of these cases work the method throws an IllegalArgumentException . |
static Duration |
parse(java.lang.String textualDescription)
See
Time.parseElapsedTime(String) ;
also accepts "forever" (and for those who prefer things exceedingly accurate, "practically_forever"). |
static Duration |
seconds(java.lang.Number n)
creates new
Duration instance of the given length of time |
static <any> |
secondsToStringRounded() |
static Duration |
sinceUtc(long millisSinceEpoch) |
static void |
sleep(Duration duration)
see
Time.sleep(long) |
Duration |
subtract(Duration other) |
Duration |
times(long x)
Deprecated.
since 0.10.0 use
multiply(long) instead. |
long |
toMilliseconds() |
long |
toMillisecondsRoundingUp()
as
toMilliseconds() but rounding away from zero (so 1 nanosecond gets rounded to 1 millisecond);
see toUnitRoundingUp(TimeUnit) ; provided as a convenience on top of toUnit(TimeUnit, RoundingMode)
as this is a common case (when you want to make sure you wait at least a certain amount of time) |
long |
toNanoseconds() |
long |
toSeconds() |
java.lang.String |
toString() |
java.lang.String |
toStringRounded() |
long |
toUnit(java.util.concurrent.TimeUnit unit)
converts to the given
TimeUnit , using TimeUnit.convert(long, TimeUnit) which rounds _down_
(so 1 nanosecond converted to milliseconds gives 0 milliseconds, and -1 ns gives -1 ms) |
long |
toUnit(java.util.concurrent.TimeUnit unit,
java.math.RoundingMode rounding)
as
toUnit(TimeUnit) but rounding as indicated
(rather than always taking the floor which is TimeUnit's default behaviour) |
long |
toUnitRoundingUp(java.util.concurrent.TimeUnit unit)
as
toUnit(TimeUnit) but rounding away from zero,
so 1 ns converted to ms gives 1 ms, and -1 ns gives 1ms |
static Duration |
untilUtc(long millisSinceEpoch) |
Duration |
upperBound(Duration alternateMaximumValue)
returns the smaller of this value or the argument
|
public static final Duration ZERO
public static final Duration ONE_MILLISECOND
public static final Duration ONE_SECOND
public static final Duration FIVE_SECONDS
public static final Duration TEN_SECONDS
public static final Duration THIRTY_SECONDS
public static final Duration ONE_MINUTE
public static final Duration TWO_MINUTES
public static final Duration FIVE_MINUTES
public static final Duration ONE_HOUR
public static final Duration ONE_DAY
public static final Duration PRACTICALLY_FOREVER
public int compareTo(Duration o)
compareTo
in interface java.lang.Comparable<Duration>
public java.lang.String toString()
toString
in class java.lang.Object
public java.lang.String toStringRounded()
public boolean equals(java.lang.Object o)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public long toUnit(java.util.concurrent.TimeUnit unit)
TimeUnit
, using TimeUnit.convert(long, TimeUnit)
which rounds _down_
(so 1 nanosecond converted to milliseconds gives 0 milliseconds, and -1 ns gives -1 ms)public long toUnit(java.util.concurrent.TimeUnit unit, java.math.RoundingMode rounding)
toUnit(TimeUnit)
but rounding as indicated
(rather than always taking the floor which is TimeUnit's default behaviour)public long toUnitRoundingUp(java.util.concurrent.TimeUnit unit)
toUnit(TimeUnit)
but rounding away from zero,
so 1 ns converted to ms gives 1 ms, and -1 ns gives 1mspublic long toMilliseconds()
public long toMillisecondsRoundingUp()
toMilliseconds()
but rounding away from zero (so 1 nanosecond gets rounded to 1 millisecond);
see toUnitRoundingUp(TimeUnit)
; provided as a convenience on top of toUnit(TimeUnit, RoundingMode)
as this is a common case (when you want to make sure you wait at least a certain amount of time)public long toNanoseconds()
public long toSeconds()
public long nanos()
public static Duration parse(java.lang.String textualDescription)
Time.parseElapsedTime(String)
;
also accepts "forever" (and for those who prefer things exceedingly accurate, "practically_forever").
If null or blank or 'null' is passed in, then null will be returned.
Also see of(Object)
.public static Duration days(java.lang.Number n)
Duration
instance of the given length of timepublic static Duration hours(java.lang.Number n)
Duration
instance of the given length of timepublic static Duration minutes(java.lang.Number n)
Duration
instance of the given length of timepublic static Duration seconds(java.lang.Number n)
Duration
instance of the given length of timepublic static Duration millis(java.lang.Number n)
Duration
instance of the given length of timepublic static Duration micros(java.lang.Number n)
Duration
instance of the given length of timepublic static Duration nanos(java.lang.Number n)
Duration
instance of the given length of timepublic static <any> millisToStringRounded()
public static <any> secondsToStringRounded()
public static Duration of(java.lang.Object o)
parse
the object as a stringStopwatch
toMilliseconds
method, if one exists.IllegalArgumentException
.public static Duration of(long value, java.util.concurrent.TimeUnit unit)
public static Duration untilUtc(long millisSinceEpoch)
public static Duration sinceUtc(long millisSinceEpoch)
public Duration multiply(long x)
@Deprecated public Duration times(long x)
multiply(long)
instead.public Duration multiply(double d)
public Duration half()
public static void sleep(Duration duration)
Time.sleep(long)
public CountdownTimer countdownTimer()
CountdownTimer
with this durationpublic boolean isPositive()
public boolean isNegative()
public boolean isLongerThan(Duration x)
public boolean isLongerThan(Stopwatch stopwatch)
public boolean isShorterThan(Duration x)
public boolean isShorterThan(Stopwatch stopwatch)
public Duration lowerBound(Duration alternateMinimumValue)