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()