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


Java ChronoZonedDateTime toEpochSecond()用法及代碼示例


ChronoZonedDateTime接口的toEpochSecond()方法用於將此ChronoZonedDateTime轉換為自1970-01-01T00:00:00Z以來的秒數。該方法將此ChronoZonedDateTime與作為參數傳遞的偏移量相結合,以計算epoch-second值,該值是從1970-01-01T00:00:00Z開始經過的秒數。時期之後的時間軸上的瞬間為正,早期為負。

用法:

default long toEpochSecond()

參數:此方法不接受任何參數。


返回值:此方法返回long,它是自1970-01-01T00:00:00Z的紀元以來的秒數,可能為負數。

以下示例程序旨在說明toEpochSecond()方法:

示例1:

// Java program to demonstrate 
// ChronoZonedDateTime.toEpochSecond() method 
  
import java.time.*; 
import java.time.chrono.*; 
import java.time.chrono.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create ChronoZonedDateTime object 
        ChronoZonedDateTime time 
            = ZonedDateTime 
                  .parse( 
                      "2018-12-06T19:21:12.123+05:30[Asia/Calcutta]"); 
  
        // print ChronoZonedDateTime 
        System.out.println("ChronoZonedDateTime: "
                           + time); 
  
        // print result 
        System.out.println("Epoch Second: "
                           + time.toEpochSecond()); 
    } 
}
輸出:
ChronoZonedDateTime: 2018-12-06T19:21:12.123+05:30[Asia/Calcutta]
Epoch Second: 1544104272

示例2:

// Java program to demonstrate 
// ChronoZonedDateTime.toEpochSecond() method 
  
import java.time.*; 
import java.time.chrono.*; 
import java.time.chrono.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create ChronoZonedDateTime object 
        ChronoZonedDateTime time 
            = ZonedDateTime.parse( 
                "1918-10-25T23:12:38.543+02:00[Europe/Paris]"); 
  
        // print ChronoZonedDateTime 
        System.out.println("ChronoZonedDateTime: "
                           + time); 
  
        // print result 
        System.out.println("Epoch Second: "
                           + time.toEpochSecond()); 
    } 
}
輸出:
ChronoZonedDateTime: 1918-10-25T23:12:38.543Z[Europe/Paris]
Epoch Second: -1615250842

參考: https://docs.oracle.com/javase/9/docs/api/java/time/chrono/ChronoZonedDateTime.html#toEpochSecond–



相關用法


注:本文由純淨天空篩選整理自ShubhamMaurya3大神的英文原創作品 ChronoZonedDateTime toEpochSecond() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。