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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。