TimeUnit
represents time durations at a given unit of
granularity and provides utility methods to convert across units,
and to perform timing and delay operations in these units. A
TimeUnit
does not maintain time information, but only
helps organize and use time representations that may be maintained
separately across various contexts. A nanosecond is defined as one
thousandth of a microsecond, a microsecond as one thousandth of a
millisecond, a millisecond as one thousandth of a second, a minute
as sixty seconds, an hour as sixty minutes, and a day as twenty four
hours.
A TimeUnit
is mainly used to inform time-based methods
how a given timing parameter should be interpreted. For example,
the following code will timeout in 50 milliseconds if the lock
is not available:
Lock lock = ...;
if (lock.tryLock(50L, TimeUnit.MILLISECONDS)) ...
while this code will timeout in 50 seconds:
Lock lock = ...;
if (lock.tryLock(50L, TimeUnit.SECONDS)) ...
Note however, that there is no guarantee that a particular timeout
implementation will be able to notice the passage of time at the
same granularity as the given TimeUnit
.
Modifier and Type | Field and Description |
---|---|
private static final long | |
public static final TimeUnit | DAYS
Time unit representing twenty four hours. |
private static final long | |
public static final TimeUnit | HOURS
Time unit representing sixty minutes. |
private final long | |
private final long | |
private final long | |
private final long | |
private static final long | |
private final long | |
public static final TimeUnit | MICROSECONDS
Time unit representing one thousandth of a millisecond. |
private static final long | |
private final int | |
public static final TimeUnit | MILLISECONDS
Time unit representing one thousandth of a second. |
private static final long | |
public static final TimeUnit | MINUTES
Time unit representing sixty seconds. |
private static final long | |
public static final TimeUnit | NANOSECONDS
Time unit representing one thousandth of a microsecond. |
private final long | |
private static final long | |
public static final TimeUnit | SECONDS
Time unit representing one second. |
private final int |
Access | Constructor and Description |
---|---|
private |
Modifier and Type | Method and Description |
---|---|
public long | Returns: the converted duration in this unit, orLong.MIN_VALUE if conversion would negatively overflow,
or Long.MAX_VALUE if it would positively overflow.the time duration in the given sourceDuration, TimeUnit sourceUnit the unit of the sourceUnit)sourceDuration argumentConverts the given time duration in the given unit to this unit. |
public long | |
private static long | cvt(long
duration d, long result unit scale dst, long source unit scale src)General conversion utility. |
private int | Returns: the number of nanosecondsthe duration d, long the number of milliseconds m)Utility to compute the excess-nanosecond argument to wait, sleep, join. |
public static TimeUnit | Returns: the converted equivalent TimeUnitthe ChronoUnit to convert chronoUnit)Converts a |
public void | sleep(long
the minimum time to sleep. If less than
or equal to zero, do not sleep at all. timeout)Performs a |
public void | timedJoin(Thread
the thread to wait for thread, long the maximum time to wait. If less than
or equal to zero, do not wait at all. timeout)Performs a timed |
public void | timedWait(Object
the object to wait on obj, long the maximum time to wait. If less than
or equal to zero, do not wait at all. timeout)Performs a timed |
public ChronoUnit | Returns: the converted equivalent ChronoUnitConverts this |
public long | Returns: the converted durationthe duration duration)Equivalent to
|
public long | Returns: the converted duration, orLong.MIN_VALUE if conversion would negatively overflow,
or Long.MAX_VALUE if it would positively overflow.the duration duration)Equivalent to
|
public long | Returns: the converted duration, orLong.MIN_VALUE if conversion would negatively overflow,
or Long.MAX_VALUE if it would positively overflow.the duration duration)Equivalent to
|
public long | Returns: the converted duration, orLong.MIN_VALUE if conversion would negatively overflow,
or Long.MAX_VALUE if it would positively overflow.the duration duration)Equivalent to
|
public long | Returns: the converted duration, orLong.MIN_VALUE if conversion would negatively overflow,
or Long.MAX_VALUE if it would positively overflow.the duration duration)Equivalent to
|
public long | Returns: the converted duration, orLong.MIN_VALUE if conversion would negatively overflow,
or Long.MAX_VALUE if it would positively overflow.the duration duration)Equivalent to
|
public long | Returns: the converted duration, orLong.MIN_VALUE if conversion would negatively overflow,
or Long.MAX_VALUE if it would positively overflow.the duration duration)Equivalent to
|
public static TimeUnit | |
public static TimeUnit[] |
DAY_SCALE | back to summary |
---|---|
private static final long DAY_SCALE |
DAYS | back to summary |
---|---|
public static final TimeUnit DAYS Time unit representing twenty four hours.
|
HOUR_SCALE | back to summary |
---|---|
private static final long HOUR_SCALE |
HOURS | back to summary |
---|---|
public static final TimeUnit HOURS Time unit representing sixty minutes.
|
maxMicros | back to summary |
---|---|
private final long maxMicros |
maxMillis | back to summary |
---|---|
private final long maxMillis |
maxNanos | back to summary |
---|---|
private final long maxNanos |
maxSecs | back to summary |
---|---|
private final long maxSecs |
MICRO_SCALE | back to summary |
---|---|
private static final long MICRO_SCALE |
microRatio | back to summary |
---|---|
private final long microRatio |
MICROSECONDS | back to summary |
---|---|
public static final TimeUnit MICROSECONDS Time unit representing one thousandth of a millisecond. |
MILLI_SCALE | back to summary |
---|---|
private static final long MILLI_SCALE |
milliRatio | back to summary |
---|---|
private final int milliRatio |
MILLISECONDS | back to summary |
---|---|
public static final TimeUnit MILLISECONDS Time unit representing one thousandth of a second. |
MINUTE_SCALE | back to summary |
---|---|
private static final long MINUTE_SCALE |
MINUTES | back to summary |
---|---|
public static final TimeUnit MINUTES Time unit representing sixty seconds.
|
NANO_SCALE | back to summary |
---|---|
private static final long NANO_SCALE |
NANOSECONDS | back to summary |
---|---|
public static final TimeUnit NANOSECONDS Time unit representing one thousandth of a microsecond. |
scale | back to summary |
---|---|
private final long scale |
SECOND_SCALE | back to summary |
---|---|
private static final long SECOND_SCALE |
SECONDS | back to summary |
---|---|
public static final TimeUnit SECONDS Time unit representing one second. |
secRatio | back to summary |
---|---|
private final int secRatio |
TimeUnit | back to summary |
---|---|
private TimeUnit(long s) |
convert | back to summary |
---|---|
public long convert(long sourceDuration, TimeUnit sourceUnit) Converts the given time duration in the given unit to this unit.
Conversions from finer to coarser granularities truncate, so
lose precision. For example, converting For example, to convert 10 minutes to milliseconds, use:
|
convert | back to summary |
---|---|
public long convert(Duration duration) Converts the given time duration to this unit. For any TimeUnit API Note This method differs from
|
cvt | back to summary |
---|---|
private static long cvt(long d, long dst, long src) General conversion utility.
|
excessNanos | back to summary |
---|---|
private int excessNanos(long d, long m) Utility to compute the excess-nanosecond argument to wait, sleep, join.
|
of | back to summary |
---|---|
public static TimeUnit of(ChronoUnit chronoUnit) Converts a
|
sleep | back to summary |
---|---|
public void sleep(long timeout) throws InterruptedException Performs a
|
timedJoin | back to summary |
---|---|
public void timedJoin(Thread thread, long timeout) throws InterruptedException Performs a timed
|
timedWait | back to summary |
---|---|
public void timedWait(Object obj, long timeout) throws InterruptedException Performs a timed For example, you could implement a blocking
|
toChronoUnit | back to summary |
---|---|
public ChronoUnit toChronoUnit() Converts this
|
toDays | back to summary |
---|---|
public long toDays(long duration) Equivalent to
|
toHours | back to summary |
---|---|
public long toHours(long duration) Equivalent to
|
toMicros | back to summary |
---|---|
public long toMicros(long duration) Equivalent to
|
toMillis | back to summary |
---|---|
public long toMillis(long duration) Equivalent to
|
toMinutes | back to summary |
---|---|
public long toMinutes(long duration) Equivalent to
|
toNanos | back to summary |
---|---|
public long toNanos(long duration) Equivalent to
|
toSeconds | back to summary |
---|---|
public long toSeconds(long duration) Equivalent to
|
valueOf | back to summary |
---|---|
public static TimeUnit valueOf(String name) |
values | back to summary |
---|---|
public static TimeUnit[] values() |