📌  相关文章
📜  Java中的 ChronoZonedDateTime toInstant() 方法及示例

📅  最后修改于: 2022-05-13 01:55:36.061000             🧑  作者: Mango

Java中的 ChronoZonedDateTime toInstant() 方法及示例

ChronoZonedDateTime类的toInstant()方法用于将此 ChronoZonedDateTime 转换为 Instant。

句法:

default Instant toInstant()

参数:此方法不接受任何参数。

返回值:此方法返回Instant ,它与此 ChronoZonedDateTime 表示的 Instant 相同。

下面的程序说明了 toInstant() 方法:

方案一:

// Java program to demonstrate
// ChronoZonedDateTime.toInstant() 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("Instant: "
                           + time.toInstant());
    }
}
输出:
ChronoZonedDateTime: 2018-12-06T19:21:12.123+05:30[Asia/Calcutta]
Instant: 2018-12-06T13:51:12.123Z

方案二:

// Java program to demonstrate
// ChronoZonedDateTime.toInstant() 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("Instant: "
                           + time.toInstant());
    }
}
输出:
ChronoZonedDateTime: 1918-10-25T23:12:38.543Z[Europe/Paris]
Instant: 1918-10-25T23:12:38.543Z

参考: https://docs.oracle.com/javase/9/docs/api/ Java/time/chrono/ChronoZonedDateTime.html#toInstant–