Java Instant getEpochSecond()方法及示例

Java Instant 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()方法。

程序1 :

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

程序2

// 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://docs.oracle.com/javase/10/docs/api/java/time/Instant.html#getEpochSecond()

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程