Java Instant atZone()方法在Java中的应用实例

Java Instant atZone()方法在Java中的应用实例

Instant类的 atZone(ZoneId zone) 方法是用来将这个瞬间与ZoneId作为参数的时区结合起来,创建一个ZonedDateTime对象。这个方法以ZoneId为参数,将时区与这个即时时间结合起来,操作后返回一个ZonedDateTime对象。如果该时刻太大,不能放入一个分区日期时间,那么这个方法将抛出一个异常。这个方法与 ZonedDateTime.ofInstant(this, zone) 相同

语法

public ZonedDateTime atZone(ZoneId zone)

参数: 该方法接受一个参数 zone ,它是要结合到这个即时对象的zone。它不应该是空的。

返回值 :该方法返回一个ZonedDateTime,它是Instant的当前区域和作为参数传递的区域的组合。

异常: 如果瞬时太大,不能装入一个分区的日期时间,这个方法会抛出 DateTimeException

下面的程序说明了Instant.atZone()方法。

程序1 :

// Java program to demonstrate
// Instant.atZone() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create an instance object
        Instant instant
            = Instant.parse("2018-10-20T16:55:30.00Z");
  
        // print Instant Value
        System.out.println("Instant: "
                           + instant);
  
        // create ZoneId object
        ZoneId zone = ZoneId.of("Europe/Paris");
  
        // apply atZone method of Instant class
        ZonedDateTime result = instant.atZone(zone);
  
        // print results
        System.out.println("ZonedDateTime: "
                           + result);
    }
}

输出:

Instant: 2018-10-20T16:55:30Z
ZonedDateTime: 2018-10-20T18:55:30+02:00[Europe/Paris]

程序2

// Java program to demonstrate
// Instant.atZone() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create an instance object
        Instant instant
            = Instant.parse("2018-11-18T06:55:30.00Z");
  
        // print Instant Value
        System.out.println("Instant: "
                           + instant);
  
        // create ZoneId object
        ZoneId zone = ZoneId.of("Asia/Aden");
  
        // apply atZone method
        ZonedDateTime result
            = instant.atZone(zone);
  
        // print results
        System.out.println("ZonedDateTime: "
                           + result);
    }
}

输出:

Instant: 2018-11-18T06:55:30Z
ZonedDateTime: 2018-11-18T09:55:30+03:00[Asia/Aden]

程序3

// Java program to demonstrate
// Instant.atZone() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create an instance object
        Instant instant
            = Instant.now();
  
        // print Instant Value
        System.out.println("Instant: "
                           + instant);
  
        // create ZoneId object
        ZoneId zone = ZoneId.of("Pacific/Midway");
  
        // apply atZone method
        ZonedDateTime result
            = instant.atZone(zone);
  
        // print results
        System.out.println("ZonedDateTime: "
                           + result);
    }
}

输出:

Instant: 2018-11-22T08:11:48.029Z
ZonedDateTime: 2018-11-21T21:11:48.029-11:00[Pacific/Midway]

**参考资料: ** https://docs.oracle.com/javase/10/docs/api/java/time/Instant.html#atZone(java.time.ZoneId)

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程