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


Java Instant toEpochMilli()用法及代码示例


Instant类的toEpochMilli()方法用于将此瞬间转换为从1970-01-01T00:00:00Z的纪元到long值的毫秒数。此方法返回该long值。

用法:

public long toEpochMilli()

返回值:此方法返回自1970-01-01T00:00:00Z的纪元以来的毫秒数。


异常:如果发生数字溢出,则此方法将引发ArithmeticException。

以下示例程序旨在说明Instant.toEpochMilli()方法:

示例1:

// Java program to demonstrate 
// Instant.toEpochMilli() method 
  
import java.time.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create a Instant object 
        Instant instant 
            = Instant.parse("2018-12-30T19:34:50.63Z"); 
  
        // get milisecond value using toEpochMilli() 
        long value = instant.toEpochMilli(); 
  
        // print result 
        System.out.println("Milisecond value: "
                           + value); 
    } 
}
输出:
Milisecond value: 1546198490630

示例2:

// Java program to demonstrate 
// Instant.toEpochMilli() method 
  
import java.time.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create a Instant object 
        Instant instant = Instant.now(); 
  
        // current Instant 
        System.out.println("Current Instant: "
                           + instant); 
  
        // get milisecond value using toEpochMilli() 
        long value = instant.toEpochMilli(); 
  
        // print result 
        System.out.println("Milisecond value: "
                           + value); 
    } 
}
输出:
Current Instant: 2018-11-27T04:46:36.368Z
Milisecond value: 1543293996368

参考:https://docs.oracle.com/javase/10/docs/api/java/time/Instant.html#toEpochMilli()



相关用法


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