Java中的Clock system()方法及其示例

JAVA时间时钟系统(区域ID区域) 方法是时钟类的静态方法,它返回一个时钟,该时钟使用最佳可用系统时钟返回时钟的当前时刻,返回的时钟的ZoneID设置为已传递的ZoneID。这种方法可以使用系统。currentTimeMillis(),或其他更高分辨率的时钟(如果该时钟可用)。

null

在从即时转换为日期或时间时,指定的时区用于给出该时区的日期和时间。此方法返回的时钟是不可变的、线程安全的和可序列化的。

语法:

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的system(ZoneId)方法。时间时钟等级:

项目1: 使用系统(ZoneId)创建时钟时,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()打印区域ID。

// 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

参考: https://docs.oracle.com/javase/8/docs/api/java/time/Clock.html#system-爪哇。时间地带-

© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享