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
- 如果休眠時中斷。