Java中的Clock withZone()方法
可以使用withZone()JavaClockClass中的方法获取时钟对象的时钟副本。在时钟对象上使用此方法可获得时钟副本。该withZone()方法需要一个参数,即更改时区所需的区域。同样,它返回带有所需时区的时钟对象的时钟副本。
演示此的程序如下所示-
示例
import java.time.*;
public class Demo {
public static void main(String[] args) {
Clock c1 = Clock.systemDefaultZone();
ZoneId zone = ZoneId.of("Australia/Melbourne");
Clock c2 = c1.withZone(zone);
System.out.println("The Zone is: " + c2.getZone());
}
}输出结果
The Zone is: Australia/Melbourne
现在让我们了解上面的程序。
使用方法获得时钟对象c1的时钟副本,即c2withZone()。然后,该getzone()方法用于打印区域详细信息。演示这的代码片段如下-
Clock c1 = Clock.systemDefaultZone();
ZoneId zone = ZoneId.of("Australia/Melbourne");
Clock c2 = c1.withZone(zone);
System.out.println("The Zone is: " + c2.getZone());