java.time.Clock.system(ZoneId zone)方法是Clock类的静态方法,该方法返回一个时钟,该时钟使用最佳系统时钟将返回的当前时刻作为当前时钟,并将返回时钟的ZoneID设置为传递的ZoneID。如果可以使用此方法,则可以使用System.currentTimeMillis()或其他更高分辨率的时钟。
从即时转换为日期或时间时,将使用指定的时区来给出该时区的日期和时间。从此方法返回的时钟是不可变的,线程安全的和可序列化的。
用法:
public static Clock system(ZoneId zone)
参数:此方法采用强制性参数区域,该参数区域是在将即时转换为日期时间时使用的时区
返回:该方法返回给定ZoneId的Clock对象
例:
Code:
// create a Zone Id for Europe/Paris
ZoneId zoneId = ZoneId.of("Europe/Paris");
// base Clock with default zone
Clock realClock=Clock.system(zoneId);
System.out.println(clock.instant());
Output::
2018-08-21T10:25:52.361Z
Explanation::
when you call system(ZoneId) for Clock then the system(ZoneId)
method will return a Class Object for the given ZoneId.you can get
date and time of clock by using instant of class.
以下示例程序旨在说明java.time.Clock类的system(ZoneId)方法:
程序1:当使用system(ZoneId)创建Clock时,其中ZoneId为“Europe/Paris”并打印时钟的日期和时间。
// Java program to demonstrate
// system(ZoneId) method of Clock class
import java.time.*;
// create class
public class systemMethodDemo {
// Main method
public static void main(String[] args)
{
// create a Zone Id for Europe/Paris
ZoneId zoneId = ZoneId.of("Europe/Paris");
// create Clock with system(zoneId) method
Clock clock = Clock.system(zoneId);
// get instant of class
Instant instant = clock.instant();
// get ZonedDateTime object from instantObj to get date time
ZonedDateTime time = instant.atZone(clock.getZone());
// print details of time
System.out.println("Instant for class is " + time.toString());
}
}
输出:
Instant for class is 2018-08-22T13:53:35.779+02:00[Europe/Paris]
程序2:使用system()创建带有区域“US/Arizona”的时钟,并使用getZone()打印zoneId。
// Java program to demonstrate
// system(ZoneId) method of Clock class
import java.time.*;
// create class
public class systemMethodDemo {
// Main method
public static void main(String[] args)
{
// create a Zone Id for US/Arizona
ZoneId zoneId = ZoneId.of("US/Arizona");
// create Clock with system(zoneId) method
Clock clock = Clock.system(zoneId);
// print details of ZoneId of new Clock
System.out.println("ZoneID of class is "
+ clock.getZone());
}
}
输出:
ZoneID of class is US/Arizona