Java中的 TimeUnit 类与示例
TimeUnit是一个可用的枚举 Java.util.concurrent包。 顾名思义,TimeUnit 处理时间单位。 TimeUnit 以给定的粒度单位提供时间表示。它提供了跨时间单位转换时间的可用方法。 TimeUnit 有助于了解应如何解释给定时间,即应考虑哪个时间单位。可以使用 TimeUnit 计算出持续时间之间的微小差异,例如微秒和纳秒。它用于执行定时和延迟操作。
它支持纳秒、微秒、毫秒、秒、分钟、小时和天单位。对于这些单位,TimeUnit 指定了相应的枚举常量:
- 纳秒:千分之一微秒
- 微秒:千分之一毫秒
- 毫秒:千分之一秒
- 秒数:一秒
- 分钟:六十秒
- 时间:六十分钟
- 天数:二十四小时
示例 1:
Java
// Java program to demonstrate TimeUnit Class
import java.util.concurrent.TimeUnit;
public class GFG {
public static void main(String args[])
{
long hours = 96;
// Convert given time (hours)in days
long days = TimeUnit.DAYS.convert(hours, TimeUnit.HOURS);
// Convert days in minutes
long minutes = TimeUnit.MINUTES.convert(days, TimeUnit.DAYS);
System.out.println(hours + " Hours = " + days
+ " Days = " + +minutes
+ " Minutes");
// Convert given time (minutes) to microseconds
long micros = TimeUnit.MINUTES.toMicros(minutes);
System.out.println(minutes + " Minutes = " + micros
+ " Microseconds");
// Convert given time (microseconds) to seconds
long seconds = TimeUnit.MICROSECONDS.toSeconds(micros);
System.out.println(micros + " Microseconds = "
+ seconds + " Seconds");
// Create TimeUnit object of type Minutes
TimeUnit time = TimeUnit.valueOf("MINUTES");
System.out.println("TimeUnit object type: " + time);
}
}
Java
// Java program to demonstrate TimeUnit Class
import java.util.concurrent.TimeUnit;
public class GFG implements Runnable {
public void run()
{
// Get array of TimeUnit enum constants using
// values()
for (TimeUnit unit : TimeUnit.values()) {
try {
// pause for 1 second
TimeUnit.SECONDS.sleep(1);
}
catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(
unit + " : "
+ unit.convert(24, TimeUnit.HOURS));
}
}
public static void main(String args[])
{
GFG obj1 = new GFG();
System.out.println("TimeUnit Example");
// Create and start Thread
Thread t1 = new Thread(obj1);
t1.start();
System.out.println("Now, thread will run for 5 seconds");
try {
// Specify Thread join time, here, 5 seconds
TimeUnit.SECONDS.timedJoin(t1, 5);
}
catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread Execution Paused");
System.out.println("Resuming Thread Execution...");
}
}
96 Hours = 4 Days = 5760 Minutes
5760 Minutes = 345600000000 Microseconds
345600000000 Microseconds = 345600 Seconds
TimeUnit object type: MINUTES
示例 2:
Java
// Java program to demonstrate TimeUnit Class
import java.util.concurrent.TimeUnit;
public class GFG implements Runnable {
public void run()
{
// Get array of TimeUnit enum constants using
// values()
for (TimeUnit unit : TimeUnit.values()) {
try {
// pause for 1 second
TimeUnit.SECONDS.sleep(1);
}
catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(
unit + " : "
+ unit.convert(24, TimeUnit.HOURS));
}
}
public static void main(String args[])
{
GFG obj1 = new GFG();
System.out.println("TimeUnit Example");
// Create and start Thread
Thread t1 = new Thread(obj1);
t1.start();
System.out.println("Now, thread will run for 5 seconds");
try {
// Specify Thread join time, here, 5 seconds
TimeUnit.SECONDS.timedJoin(t1, 5);
}
catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread Execution Paused");
System.out.println("Resuming Thread Execution...");
}
}
TimeUnit Example
Now, thread will run for 5 seconds
NANOSECONDS : 86400000000000
MICROSECONDS : 86400000000
MILLISECONDS : 86400000
SECONDS : 86400
Thread Execution Paused
Resuming Thread Execution...
MINUTES : 1440
HOURS : 24
DAYS : 1
TimeUnit 中可用的实用方法: Methods Description long convert() Converts the time duration inputted with its unit to the required unit. long toDays() Converts the time duration to Days. long toHours() Converts the time duration to Hours. long toMicros() Converts the time duration to Microseconds. long toMillis() Converts the time duration to Milliseconds. long toMinutes() Converts the time duration to Minutes. long toNanos() Converts the time duration to Nanoseconds. long toSeconds() Converts the time duration to Seconds. static TimeUnit valueOf() Returns the enum constant of the type with the specified name. static TimeUnit [] values() Returns an array containing the enum constants. void sleep() Performs a Thread.sleep using this time unit. Pause for given TimeUnit. void timedWait() Performs a timed Object.wait using this time unit. Wait for the given time unit to execute. void timedJoin() Performs a timed Thread.join using this time unit. Thread is provided to do work for a given time duration only.