当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java TimeUnit toMicros()用法及代码示例


自1970年1月1日午夜UTC以来,使用TimeUnit类的toMicros()方法获取TimeUnit对象表示的时间,以MicroSeconds的数量表示。

用法:

public long toMicros(long duration)

参数:此方法接受强制参数持续时间,该持续时间以毫秒为单位。


返回值:此方法将转换后的持续时间作为MicroSeconds返回。

下面的程序演示了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


相关用法


注:本文由纯净天空筛选整理自RishabhPrabhu大神的英文原创作品 TimeUnit toMicros() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。