TimeUnit 表示给定单元粒度的时间段,它提供在这些单元中进行跨单元转换和执行计时及延迟操作的实用工具方法。TimeUnit 不维护时间信息,但是有助于组织和使用可能跨各种上下文单独维护的时间表示形式。毫微秒定义为千分之一微秒,微秒为千分之一毫秒,毫秒为千分之一秒,一分钟为六十秒,一小时为六十分钟,一天为二十四小时。
方法详细信息
values
public static final TimeUnit[] values()
Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows:
for(TimeUnit c : TimeUnit.values())
System.out.println(c);
返回:
an array containing the constants of this enum type, in the order they are declared
valueOf
public static TimeUnit valueOf(String name)
返回带有指定名称的该类型的枚举常量。 字符串必须与用于声明该类型的枚举常量的 标识符
完全匹配。(不允许有多余 的空格。)
参数:
指定要返回的枚举常量的名称。 -
返回:
返回带有指定名称的枚举常量
抛出:
如果该枚举类型没有带有指定名称的常量, - 则抛出 IllegalArgumentException
convert
public long convert(long sourceDuration,
TimeUnit sourceUnit)
将给定单元的时间段转换到此单元。从较细粒度到较粗粒度的舍位转换,这样会失去精确性。例如,将
999 毫秒转换为秒的结果为
0。使用参数从较粗粒度到较细粒度转换,如果参数为负,则在数字上溢出至
Long.MIN_VALUE,如果为正,则为
Long.MAX_VALUE。
例如,要将 10 分钟转换为毫秒,请使用:TimeUnit.MILLISECONDS.convert(10L, TimeUnit.MINUTES)
参数:
sourceDuration - 给定
sourceUnit 中的时间段
sourceUnit -
sourceDuration 参数的单元
返回:
此单元中的转换时间段;如果转换将负溢出,则返回
Long.MIN_VALUE;如果转换将正溢出,则返回
Long.MAX_VALUE。
toNanos
public long toNanos(long duration)
等效于
NANOSECONDS.convert(duration, this)。
参数:
duration - 时间段
返回:
转换时间段,如果转换将负溢出,则返回
Long.MIN_VALUE;如果转换将正溢出,则返回
Long.MAX_VALUE。
另请参见:
convert(long, java.util.concurrent.TimeUnit)
toMicros
public long toMicros(long duration)
等效于
MICROSECONDS.convert(duration, this)。
参数:
duration - 时间段
返回:
转换时间段,如果转换将负溢出,则返回
Long.MIN_VALUE;如果转换将正溢出,则返回
Long.MAX_VALUE。
另请参见:
convert(long, java.util.concurrent.TimeUnit)
toMillis
public long toMillis(long duration)
等效于
MILLISECONDS.convert(duration, this)。
参数:
duration - 时间段
返回:
转换时间段,如果转换将负溢出,则返回
Long.MIN_VALUE;如果转换将正溢出,则返回
Long.MAX_VALUE。
另请参见:
convert(long, java.util.concurrent.TimeUnit)
toSeconds
public long toSeconds(long duration)
等效于
SECONDS.convert(duration, this)。
参数:
duration - 时间段
返回:
转换时间段;如果转换将负溢出,则返回
Long.MIN_VALUE;如果转换将正溢出,则返回
Long.MAX_VALUE。
另请参见:
convert(long, java.util.concurrent.TimeUnit)
toMinutes
public long toMinutes(long duration)
等效于
MINUTES.convert(duration, this)。
参数:
duration - 时间段
返回:
转换时间段;如果转换将负溢出,则返回
Long.MIN_VALUE;如果转换将正溢出,则返回
Long.MAX_VALUE。
从以下版本开始:
1.6
另请参见:
convert(long, java.util.concurrent.TimeUnit)
toHours
public long toHours(long duration)
等效于
HOURS.convert(duration, this)。
参数:
duration - 时间段
返回:
转换时间段;如果转换将负溢出,则返回
Long.MIN_VALUE;如果转换将正溢出,则返回
Long.MAX_VALUE。
从以下版本开始:
1.6
另请参见:
convert(long, java.util.concurrent.TimeUnit)
toDays
public long toDays(long duration)
等效于
DAYS.convert(duration, this)。
参数:
duration - 时间段
返回:
转换时间段
从以下版本开始:
1.6
另请参见:
convert(long, java.util.concurrent.TimeUnit)
timedWait
public void timedWait(Object obj,
long timeout)
throws InterruptedException
使用此时间单元执行计时的
Object.wait。这是将超时参数转换为
Object.wait 方法所需格式的便捷方法。
例如,可以使用以下代码实现阻塞 poll 方法(参见 BlockingQueue.poll):
public synchronized Object poll(long timeout, TimeUnit unit) throws InterruptedException {
while (empty) {
unit.timedWait(this, timeout);
...
}
}
参数:
obj - 要等待的对象
timeout - 要等待的最长时间。如果小于等于 0,则根本不会等待。
抛出:
InterruptedException - 如果等待时中断。
另请参见:
Object.wait(long, int)
timedJoin
public void timedJoin(Thread thread,
long timeout)
throws InterruptedException
使用此时间单元执行计时的
Thread.join。这是将时间参数转换为
Thread.join 方法所需格式的便捷方法。
参数:
thread - 要等待的线程
timeout - 要等待的最长时间。如果小于等于 0,则根本不会等待。
抛出:
InterruptedException - 如果等待时中断。
另请参见:
Thread.join(long, int)
sleep
public void sleep(long timeout)
throws InterruptedException
使用此单元执行
Thread.sleep.这是将时间参数转换为
Thread.sleep 方法所需格式的便捷方法。
参数:
timeout - 休眠的最短时间。如果小于等于 0,则根本不会休眠。
抛出:
InterruptedException - 如果休眠时中断。