Java中的 TimeUnit toMicros() 方法及示例
TimeUnit 类的toMicros()方法用于获取从 1970 年 1 月 1 日午夜 UTC 以来 TimeUnit 对象表示的时间,以 MicroSeconds 的数量表示。
句法:
public long toMicros(long duration)
参数:此方法接受一个强制参数duration ,它是以毫秒为单位的持续时间。
返回值:此方法将转换后的持续时间返回为 MicroSeconds。
下面的程序说明了 TimeUnit toMicros() 方法的实现:
方案一:
Java
// Java program to demonstrate
// toMicros() method of TimeUnit Class
import java.util.concurrent.*;
import java.util.Date;
class GFG {
public static void main(String args[])
{
// Get current time in milliseconds
long timeInMilliSec = new Date().getTime();
// Create a TimeUnit object
TimeUnit time = TimeUnit.MILLISECONDS;
// Convert milliseconds to MicroSeconds
// using toMicros() method
System.out.println("Time " + timeInMilliSec
+ " milliSeconds in MicroSeconds = "
+ time.toMicros(timeInMilliSec));
}
}
Java
// Java program to demonstrate
// toMicros() method of TimeUnit Class
import java.util.concurrent.*;
import java.util.Calendar;
class GFG {
public static void main(String args[])
{
// Get current time in milliseconds
long timeInMilliSec = Calendar
.getInstance()
.getTimeInMillis();
// Create a TimeUnit object
TimeUnit time = TimeUnit.MILLISECONDS;
// Convert milliseconds to MicroSeconds
// using toMicros() method
System.out.println("Time " + timeInMilliSec
+ " milliSeconds in MicroSeconds = "
+ time.toMicros(timeInMilliSec));
}
}
输出:
Time 1539585538596 milliSeconds in MicroSeconds = 1539585538596000
方案二:
Java
// Java program to demonstrate
// toMicros() method of TimeUnit Class
import java.util.concurrent.*;
import java.util.Calendar;
class GFG {
public static void main(String args[])
{
// Get current time in milliseconds
long timeInMilliSec = Calendar
.getInstance()
.getTimeInMillis();
// Create a TimeUnit object
TimeUnit time = TimeUnit.MILLISECONDS;
// Convert milliseconds to MicroSeconds
// using toMicros() method
System.out.println("Time " + timeInMilliSec
+ " milliSeconds in MicroSeconds = "
+ time.toMicros(timeInMilliSec));
}
}
输出:
Time 1539585540908 milliSeconds in MicroSeconds = 1539585540908000