public enum java.util.concurrent
TimeUnit


Show All Login
Java SE 6
  
Extends: Enum
Details
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.
since1.5

Enum constants
final public static TimeUnit NANOSECONDS
final public static TimeUnit MICROSECONDS
final public static TimeUnit MILLISECONDS
final public static TimeUnit SECONDS
final public static TimeUnit MINUTES
final public static TimeUnit HOURS
final public static TimeUnit DAYS

Methods
public long convert(long sourceDuration, TimeUnit sourceUnit) Details
Convert 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)
sourceDurationthe time duration in the given sourceUnit
sourceUnitthe unit of the sourceDuration argument
returnthe converted duration in this unit, or Long.MIN_VALUE if conversion would negatively overflow, or Long.MAX_VALUE if it would positively overflow.

public void sleep(long timeout) throws InterruptedException Details
Performs a Thread.sleep using this unit. This is a convenience method that converts time arguments into the form required by the Thread.sleep method.
timeoutthe minimum time to sleep. If less than or equal to zero, do not sleep at all.
ThrowsInterruptedException: if interrupted while sleeping.
See also sleep
public void timedJoin(Thread thread, long timeout) throws InterruptedException Details
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.
threadthe thread to wait for
timeoutthe maximum time to wait. If less than or equal to zero, do not wait at all.
ThrowsInterruptedException: if interrupted while waiting.
See also join(long, int)
public void timedWait(Object obj, long timeout) throws InterruptedException Details
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 synchronized Object poll(long timeout, TimeUnit unit) throws InterruptedException {
    while (empty) {
      unit.timedWait(this, timeout);
      ...
    }
  }
objthe object to wait on
timeoutthe maximum time to wait. If less than or equal to zero, do not wait at all.
ThrowsInterruptedException: if interrupted while waiting.
See also wait(long, int)
public long toDays(long duration) Details
Equivalent to DAYS.convert(duration, this).
durationthe duration
returnthe converted duration
since1.6
See also convert
public long toHours(long duration) Details
Equivalent to HOURS.convert(duration, this).
durationthe duration
returnthe converted duration, or Long.MIN_VALUE if conversion would negatively overflow, or Long.MAX_VALUE if it would positively overflow.
since1.6
See also convert
public long toMicros(long duration) Details
Equivalent to MICROSECONDS.convert(duration, this).
durationthe duration
returnthe converted duration, or Long.MIN_VALUE if conversion would negatively overflow, or Long.MAX_VALUE if it would positively overflow.
See also convert
public long toMillis(long duration) Details
Equivalent to MILLISECONDS.convert(duration, this).
durationthe duration
returnthe converted duration, or Long.MIN_VALUE if conversion would negatively overflow, or Long.MAX_VALUE if it would positively overflow.
See also convert
public long toMinutes(long duration) Details
Equivalent to MINUTES.convert(duration, this).
durationthe duration
returnthe converted duration, or Long.MIN_VALUE if conversion would negatively overflow, or Long.MAX_VALUE if it would positively overflow.
since1.6
See also convert
public long toNanos(long duration) Details
Equivalent to NANOSECONDS.convert(duration, this).
durationthe duration
returnthe converted duration, or Long.MIN_VALUE if conversion would negatively overflow, or Long.MAX_VALUE if it would positively overflow.
See also convert
public long toSeconds(long duration) Details
Equivalent to SECONDS.convert(duration, this).
durationthe duration
returnthe converted duration, or Long.MIN_VALUE if conversion would negatively overflow, or Long.MAX_VALUE if it would positively overflow.
See also convert
public static TimeUnit valueOf(String name)
public static TimeUnit[] values()