當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。