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


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


Instant类的getEpochSecond()方法用于从1970-01-01T00:00:00Z的Java纪元返回秒数。这意味着此实例返回的时间(以秒为单位)与“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()



相关用法


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