Java 中 Clock system() 方法
Java 中 Clock 类的 system() 方法可用于获取所需的 zoneId 的时钟当前实例。此方法需要一个参数,即 zoneId 或时区,并且它返回该时区的时钟当前实例。
演示此方法的程序如下 −
示例
import java.time.*;
public class Main {
public static void main(String[] args) {
ZoneId zone = ZoneId.of("Australia/Melbourne");
Clock c = Clock.system(zone);
ZonedDateTime zdt = c.instant().atZone(c.getZone());
System.out.println("The Date Time for the given zone is: " + zdt.toString());
}
}输出
The Date Time for the given zone is: 2019-02-06T23:03:16.395+11:00[Australia/Melbourne]
现在,让我们来理解上述程序。
可以使用 system() 方法获取所需的 zoneId 的时钟当前实例。使用 toString() 方法显示即时时间。一个演示此方法的代码片段如下 −
ZoneId zone = ZoneId.of("Australia/Melbourne");
Clock c = Clock.system(zone);
ZonedDateTime zdt = c.instant().atZone(c.getZone());
System.out.println("The Date Time for the given zone is: " + zdt.toString());
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程语言
C++
C#
MongoDB
MySQL
Javascript
PHP