Java TimeUnit toMicros()方法及示例

Java TimeUnit toMicros()方法及示例

TimeUnit类toMicros() 方法用于获取TimeUnit对象所代表的时间,即自1970年1月1日UTC午夜起的微秒数。

语法

public long toMicros(long duration)

参数: 该方法接受一个强制性参数 duration ,它是以毫秒为单位的持续时间。

返回值: 该方法返回 转换后的持续时间 为微秒。

以下程序说明了TimeUnit toMicros()方法的实现。

程序 1 :

// 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));
    }
}

输出

Time 1539585538596 milliSeconds in MicroSeconds = 1539585538596000

程序2

// 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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程