Top Description Fields Constructors Methods
java.util.concurrent

public final Enum TimeUnit

extends Enum<TimeUnit>
Class Inheritance
Imports
java.time.Duration, java.time.temporal.ChronoUnit, java.util.Objects

A 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.
Author
Doug Lea
Since
1.5

Field Summary

Modifier and TypeField 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

Constructor Summary

AccessConstructor and Description
private
TimeUnit(long s)

Method Summary

Modifier and TypeMethod and Description
public long

Returns:

the converted duration in this unit, or Long.MIN_VALUE if conversion would negatively overflow, or Long.MAX_VALUE if it would positively overflow.
convert
(long
the time duration in the given sourceUnit
sourceDuration
,
TimeUnit
the unit of the sourceDuration argument
sourceUnit
)

Converts the given time duration in the given unit to this unit.

public long

Returns:

the converted duration in this unit, or Long.MIN_VALUE if conversion would negatively overflow, or Long.MAX_VALUE if it would positively overflow.
convert
(Duration
the time duration
duration
)

Converts the given time duration to this unit.

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 nanoseconds
excessNanos
(long
the 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 TimeUnit
of
(ChronoUnit
the ChronoUnit to convert
chronoUnit
)

Converts a ChronoUnit to the equivalent TimeUnit.

public void
sleep(long
the minimum time to sleep. If less than or equal to zero, do not sleep at all.
timeout
)

Performs a Thread.sleep using this time unit.

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 Thread.join using this time unit.

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 Object.wait using this time unit.

public ChronoUnit

Returns:

the converted equivalent ChronoUnit
toChronoUnit
()

Converts this TimeUnit to the equivalent ChronoUnit.

public long

Returns:

the converted duration
toDays
(long
the duration
duration
)

Equivalent to DAYS.convert(duration, this).

public long

Returns:

the converted duration, or Long.MIN_VALUE if conversion would negatively overflow, or Long.MAX_VALUE if it would positively overflow.
toHours
(long
the duration
duration
)

Equivalent to HOURS.convert(duration, this).

public long

Returns:

the converted duration, or Long.MIN_VALUE if conversion would negatively overflow, or Long.MAX_VALUE if it would positively overflow.
toMicros
(long
the duration
duration
)

Equivalent to MICROSECONDS.convert(duration, this).

public long

Returns:

the converted duration, or Long.MIN_VALUE if conversion would negatively overflow, or Long.MAX_VALUE if it would positively overflow.
toMillis
(long
the duration
duration
)

Equivalent to MILLISECONDS.convert(duration, this).

public long

Returns:

the converted duration, or Long.MIN_VALUE if conversion would negatively overflow, or Long.MAX_VALUE if it would positively overflow.
toMinutes
(long
the duration
duration
)

Equivalent to MINUTES.convert(duration, this).

public long

Returns:

the converted duration, or Long.MIN_VALUE if conversion would negatively overflow, or Long.MAX_VALUE if it would positively overflow.
toNanos
(long
the duration
duration
)

Equivalent to NANOSECONDS.convert(duration, this).

public long

Returns:

the converted duration, or Long.MIN_VALUE if conversion would negatively overflow, or Long.MAX_VALUE if it would positively overflow.
toSeconds
(long
the duration
duration
)

Equivalent to SECONDS.convert(duration, this).

public static TimeUnit
public static TimeUnit[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

DAY_SCALEback to summary
private static final long DAY_SCALE
DAYSback to summary
public static final TimeUnit DAYS

Time unit representing twenty four hours.

Since
1.6
HOUR_SCALEback to summary
private static final long HOUR_SCALE
HOURSback to summary
public static final TimeUnit HOURS

Time unit representing sixty minutes.

Since
1.6
maxMicrosback to summary
private final long maxMicros
maxMillisback to summary
private final long maxMillis
maxNanosback to summary
private final long maxNanos
maxSecsback to summary
private final long maxSecs
MICRO_SCALEback to summary
private static final long MICRO_SCALE
microRatioback to summary
private final long microRatio
MICROSECONDSback to summary
public static final TimeUnit MICROSECONDS

Time unit representing one thousandth of a millisecond.

MILLI_SCALEback to summary
private static final long MILLI_SCALE
milliRatioback to summary
private final int milliRatio
MILLISECONDSback to summary
public static final TimeUnit MILLISECONDS

Time unit representing one thousandth of a second.

MINUTE_SCALEback to summary
private static final long MINUTE_SCALE
MINUTESback to summary
public static final TimeUnit MINUTES

Time unit representing sixty seconds.

Since
1.6
NANO_SCALEback to summary
private static final long NANO_SCALE
NANOSECONDSback to summary
public static final TimeUnit NANOSECONDS

Time unit representing one thousandth of a microsecond.

scaleback to summary
private final long scale
SECOND_SCALEback to summary
private static final long SECOND_SCALE
SECONDSback to summary
public static final TimeUnit SECONDS

Time unit representing one second.

secRatioback to summary
private final int secRatio

Constructor Detail

TimeUnitback to summary
private TimeUnit(long s)

Method Detail

convertback 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 999 milliseconds to seconds results in 0. Conversions from coarser to finer granularities with arguments that would numerically overflow saturate to Long.MIN_VALUE if negative or Long.MAX_VALUE if positive.

For example, to convert 10 minutes to milliseconds, use: TimeUnit.MILLISECONDS.convert(10L, TimeUnit.MINUTES)

Parameters
sourceDuration:long

the time duration in the given sourceUnit

sourceUnit:TimeUnit

the unit of the sourceDuration argument

Returns:long

the converted duration in this unit, or Long.MIN_VALUE if conversion would negatively overflow, or Long.MAX_VALUE if it would positively overflow.

convertback to summary
public long convert(Duration duration)

Converts the given time duration to this unit.

For any TimeUnit unit, unit.convert(Duration.ofNanos(n)) is equivalent to unit.convert(n, NANOSECONDS), and unit.convert(Duration.of(n, unit.toChronoUnit())) is equivalent to n (in the absence of overflow).

API Note

This method differs from Duration#toNanos() in that it does not throw ArithmeticException on numeric overflow.

Parameters
duration:Duration

the time duration

Returns:long

the converted duration in this unit, or Long.MIN_VALUE if conversion would negatively overflow, or Long.MAX_VALUE if it would positively overflow.

Exceptions
NullPointerException:
if duration is null
Since
11
See Also
Duration#of(long, TemporalUnit)
cvtback to summary
private static long cvt(long d, long dst, long src)

General conversion utility.

Parameters
d:long

duration

dst:long

result unit scale

src:long

source unit scale

excessNanosback to summary
private int excessNanos(long d, long m)

Utility to compute the excess-nanosecond argument to wait, sleep, join.

Parameters
d:long

the duration

m:long

the number of milliseconds

Returns:int

the number of nanoseconds

ofback to summary
public static TimeUnit of(ChronoUnit chronoUnit)

Converts a ChronoUnit to the equivalent TimeUnit.

Parameters
chronoUnit:ChronoUnit

the ChronoUnit to convert

Returns:TimeUnit

the converted equivalent TimeUnit

Exceptions
IllegalArgumentException:
if chronoUnit has no equivalent TimeUnit
NullPointerException:
if chronoUnit is null
Since
9
sleepback to summary
public void sleep(long timeout) throws InterruptedException

Performs a Thread.sleep using this time unit. This is a convenience method that converts time arguments into the form required by the Thread.sleep method.

Parameters
timeout:long

the minimum time to sleep. If less than or equal to zero, do not sleep at all.

Exceptions
InterruptedException:
if interrupted while sleeping
timedJoinback to summary
public void timedJoin(Thread thread, long timeout) throws InterruptedException

Performs a timed Thread.join using this time unit. This is a convenience method that converts time arguments into the form required by the Thread.join method.

Parameters
thread:Thread

the thread to wait for

timeout:long

the maximum time to wait. If less than or equal to zero, do not wait at all.

Exceptions
InterruptedException:
if interrupted while waiting
timedWaitback to summary
public void timedWait(Object obj, long timeout) throws InterruptedException

Performs a timed Object.wait using this time unit. This is a convenience method that converts timeout arguments into the form required by the Object.wait method.

For example, you could implement a blocking poll method (see BlockingQueue.poll) using:

 public E poll(long timeout, TimeUnit unit)
    throws InterruptedException {
  synchronized (lock) {
    while (isEmpty()) {
      unit.timedWait(lock, timeout);
      ...
    }
  }
}
Parameters
obj:Object

the object to wait on

timeout:long

the maximum time to wait. If less than or equal to zero, do not wait at all.

Exceptions
InterruptedException:
if interrupted while waiting
toChronoUnitback to summary
public ChronoUnit toChronoUnit()

Converts this TimeUnit to the equivalent ChronoUnit.

Returns:ChronoUnit

the converted equivalent ChronoUnit

Since
9
toDaysback to summary
public long toDays(long duration)

Equivalent to DAYS.convert(duration, this).

Parameters
duration:long

the duration

Returns:long

the converted duration

Since
1.6
toHoursback to summary
public long toHours(long duration)

Equivalent to HOURS.convert(duration, this).

Parameters
duration:long

the duration

Returns:long

the converted duration, or Long.MIN_VALUE if conversion would negatively overflow, or Long.MAX_VALUE if it would positively overflow.

Since
1.6
toMicrosback to summary
public long toMicros(long duration)

Equivalent to MICROSECONDS.convert(duration, this).

Parameters
duration:long

the duration

Returns:long

the converted duration, or Long.MIN_VALUE if conversion would negatively overflow, or Long.MAX_VALUE if it would positively overflow.

toMillisback to summary
public long toMillis(long duration)

Equivalent to MILLISECONDS.convert(duration, this).

Parameters
duration:long

the duration

Returns:long

the converted duration, or Long.MIN_VALUE if conversion would negatively overflow, or Long.MAX_VALUE if it would positively overflow.

toMinutesback to summary
public long toMinutes(long duration)

Equivalent to MINUTES.convert(duration, this).

Parameters
duration:long

the duration

Returns:long

the converted duration, or Long.MIN_VALUE if conversion would negatively overflow, or Long.MAX_VALUE if it would positively overflow.

Since
1.6
toNanosback to summary
public long toNanos(long duration)

Equivalent to NANOSECONDS.convert(duration, this).

Parameters
duration:long

the duration

Returns:long

the converted duration, or Long.MIN_VALUE if conversion would negatively overflow, or Long.MAX_VALUE if it would positively overflow.

toSecondsback to summary
public long toSeconds(long duration)

Equivalent to SECONDS.convert(duration, this).

Parameters
duration:long

the duration

Returns:long

the converted duration, or Long.MIN_VALUE if conversion would negatively overflow, or Long.MAX_VALUE if it would positively overflow.

valueOfback to summary
public static TimeUnit valueOf(String name)
valuesback to summary
public static TimeUnit[] values()