Java中的即时 getEpochSecond() 方法和示例
Instant 类的getEpochSecond()方法用于返回Java纪元 1970-01-01T00:00:00Z 的秒数。表示此实例的时间与“1970-01-01T00:00:00Z”表示的时间之间的时间差,由该方法返回。纪元秒计数是秒的简单递增计数,其中第二个 0 是 1970-01-01T00:00:00Z。
句法:
public long getEpochSecond()
返回:此方法返回从 1970-01-01T00:00:00Z 开始的秒数。
下面的程序说明了 Instant.getEpochSecond() 方法:
方案一:
// Java program to demonstrate
// Instant.getEpochSecond() method
import java.time.*;
public class GFG {
public static void main(String[] args)
{
// create instance object
Instant instant
= Instant.parse("2018-10-20T16:55:30.00Z");
// print Instant Value
System.out.println("Instant: " + instant);
// get epochValue using getEpochSecond
long epochValue = instant.getEpochSecond();
// print results
System.out.println("Java epoch Value: "
+ epochValue);
}
}
输出:
Instant: 2018-10-20T16:55:30Z
Java epoch Value: 1540054530
方案二:
// Java program to demonstrate
// Instant.getEpochSecond() method
import java.time.*;
public class GFG {
public static void main(String[] args)
{
// create current instance object
Instant instant
= Instant.now();
// print Instant Value
System.out.println("Current Instant: "
+ instant);
// get epochValue using getEpochSecond
long epochValue = instant.getEpochSecond();
// print results
System.out.println("Java epoch Value: "
+ epochValue);
}
}
输出:
Current Instant: 2018-11-22T08:26:19.502Z
Java epoch Value: 1542875179
参考:https: Java/time/Instant.html#getEpochSecond()