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(com.google.common.base.Stopwatch stopwatch) |
boolean |
isPositive() |
boolean |
isShorterThan(Duration x) |
boolean |
isShorterThan(com.google.common.base.Stopwatch stopwatch) |
Duration |
lowerBound(Duration alternateMinimumValue)
returns the larger of this value or the argument
|
static Duration |
max(Duration first,
Duration second) |
Duration |
maximum(Duration alternateMaximumValue)
Deprecated.
since 0.7.0 use
upperBound(Duration) |
static Duration |
millis(java.lang.Number n)
creates new
Duration instance of the given length of time |
static com.google.common.base.Function<java.lang.Number,java.lang.String> |
millisToStringRounded() |
static Duration |
min(Duration first,
Duration second) |
Duration |
minimum(Duration alternateMinimumValue)
Deprecated.
since 0.7.0 use
lowerBound(Duration) |
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)
tries to convert given object to a Duration, parsing strings, treating numbers as millis, etc;
throws IAE if not convertible
|
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 com.google.common.base.Function<java.lang.Number,java.lang.String> |
secondsToStringRounded() |
static Duration |
sinceUtc(long millisSinceEpoch) |
static void |
sleep(Duration duration)
see
Time.sleep(long) |
Duration |
subtract(Duration other) |
Duration |
times(long x) |
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").
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 nanos(java.lang.Number n)
Duration
instance of the given length of timepublic static com.google.common.base.Function<java.lang.Number,java.lang.String> millisToStringRounded()
public static com.google.common.base.Function<java.lang.Number,java.lang.String> secondsToStringRounded()
public static Duration of(java.lang.Object o)
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)
public Duration times(long x)
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 isLongerThan(Duration x)
public boolean isLongerThan(com.google.common.base.Stopwatch stopwatch)
public boolean isShorterThan(Duration x)
public boolean isShorterThan(com.google.common.base.Stopwatch stopwatch)
public Duration lowerBound(Duration alternateMinimumValue)
public Duration upperBound(Duration alternateMaximumValue)
@Deprecated public Duration minimum(Duration alternateMinimumValue)
lowerBound(Duration)
@Deprecated public Duration maximum(Duration alternateMaximumValue)
upperBound(Duration)