1. Overview
1.概述
In this tutorial, we’re going to look into the Java Clock class from the java.time package. We’ll explain what the Clock class is and how we can use it.
在本教程中,我们将研究java.time包中的JavaClock类。我们将解释什么是Clock类以及我们如何使用它。
2. The Clock Class
2.时钟类
Clock was added in Java 8 and provides access to an instant in time using the best available system clock, and to be used as a time provider which can be effectively stubbed for testing purposes.
Clock是在Java 8中添加的,它提供了对使用最佳可用系统时钟的瞬间时间的访问,并被用作一个时间提供者,可以有效地存根于测试目的。
The current date and time depend on the time zone and, for globalized applications, a time provider is necessary to ensure that the date and time are created with the correct time zone.
当前的日期和时间取决于时区,对于全球化的应用程序,需要一个时间提供者来确保以正确的时区创建日期和时间。
This class helps us to test that our code changes work with different time zones or – when using a fixed clock – that time doesn’t affect our code.
这个类可以帮助我们测试我们的代码变化在不同的时区是否有效,或者–当使用一个固定的时钟时–时间不会影响我们的代码。
The Clock class is abstract, so we cannot create an instance of it. The following factory methods can be used:
Clock类是抽象的,所以我们不能创建它的实例。可以使用以下工厂方法。
- offset(Clock, Duration) – returns a clock that is offset by the specified Duration. The main use case for this is to simulate running in the future or the past
- systemUTC() – returns a clock representing the UTC time zone
- fixed(Instant, ZoneId) – always returns the same Instant. The leading use case for this is in testing, where the fixed clock ensures that tests are not dependent on the current clock
We’re going to look into most of the methods available in the Clock class.
我们要研究一下Clock类中的大部分方法。
2.1. instant()
2.1. instant()
This method returns an instant representing the current instant defined by the clock:
该方法返回一个代表时钟定义的当前瞬时。
Clock clock = Clock.systemDefaultZone();
Instant instant = clock.instant();
System.out.println(instant);
will produce:
将产生。
2018-04-07T03:59:35.555Z
2.2. systemUTC()
2.2. systemUTC()
This method returns a Clock object representing the current instant in the UTC zone:
该方法返回一个Clock对象,代表UTC区的当前时刻。
Clock clock = Clock.systemUTC();
System.out.println("UTC time :: " + clock.instant());
will produce:
将产生。
UTC time :: 2018-04-04T17:40:12.353Z
2.3. system()
2.3. system()
This static method returns the Clock object for the timezone identified by the given time zone id:
该静态方法返回由给定时区标识的时区的Clock对象。
Clock clock = Clock.system(ZoneId.of("Asia/Calcutta"));
System.out.println(clock.instant());
will produce:
将产生。
2018-04-04T18:00:31.376Z
2.4. systemDefaultZone()
2.4.systemDefaultZone()
This static method returns a Clock object representing the current instant and using the default time zone of the system it’s running on:
这个静态方法返回一个Clock对象,代表当前时刻,并使用它所运行的系统的默认时区。
Clock clock = Clock.systemDefaultZone();
System.out.println(clock);
The above lines produce the following result (assuming our default time zone is “Asia/Calcutta”):
以上几行产生的结果如下(假设我们的默认时区是 “亚洲/卡尔卡塔”)。
SystemClock[Asia/Calcutta]
We can achieve the same behavior by passing ZoneId.systemDefault():
我们可以通过传递ZoneId.systemDefault()来实现同样的行为。
Clock clock = Clock.system(ZoneId.systemDefault());
2.5. millis()
2.5. millis()
This method returns the current instant of the clock in milliseconds. It’s provided to allow the use of the clock in high-performance use cases where the creation of an object would be unacceptable. This method can be used in places where we’d otherwise have used System.currentTimeInMillis():
该方法以毫秒为单位返回时钟的当前时刻。提供该方法的目的是允许在创建对象无法接受的高性能用例中使用时钟。这个方法可以用在我们本来要使用System.currentTimeInMillis()的地方。
Clock clock = Clock.systemDefaultZone();
System.out.println(clock.millis());
will produce:
将产生。
1523104441258
2.6. offset()
2.6.offset()
This static method returns an instant from the specified base clock with the specified duration added.
这个静态方法从指定的基础时钟返回一个添加了指定持续时间的瞬间。
If the duration is negative, then the resulting clock instant will be earlier than the given base clock.
如果持续时间为负数,那么产生的时钟瞬间将早于给定的基准时钟。
Using offset, we can get instants in the past and future of the given base clock. If we pass a zero duration then we’ll get the same clock as given base clock:
使用offset,我们可以得到给定基础时钟的过去和未来的时刻。如果我们传递一个零持续时间,那么我们将得到与给定基础时钟相同的时钟。
Clock baseClock = Clock.systemDefaultZone();
// result clock will be later than baseClock
Clock clock = Clock.offset(baseClock, Duration.ofHours(72));
System.out.println(clock5.instant());
// result clock will be same as baseClock
clock = Clock.offset(baseClock, Duration.ZERO);
System.out.println(clock.instant());
// result clock will be earlier than baseClock
clock = Clock.offset(baseClock, Duration.ofHours(-72));
System.out.println(clock.instant());
will produce:
将产生。
2018-04-10T13:24:07.347Z
2018-04-07T13:24:07.348Z
2018-04-04T13:24:07.348Z
2.7. tick()
2.7 tick()
This static method returns instants from the specified clock rounded to the nearest occurrence of the specified duration. The specified clock duration must be positive:
该静态方法返回指定时钟的实例四舍五入到最近的指定持续时间。指定的时钟持续时间必须是正数。
Clock clockDefaultZone = Clock.systemDefaultZone();
Clock clocktick = Clock.tick(clockDefaultZone, Duration.ofSeconds(30));
System.out.println("Clock Default Zone: " + clockDefaultZone.instant());
System.out.println("Clock tick: " + clocktick.instant());
will produce:
将产生。
Clock Default Zone: 2018-04-07T16:42:05.473Z
Clock tick: 2018-04-07T16:42:00Z
2.8. tickSeconds()
2.8.tickSeconds()
This static method returns the current instant ticking in whole seconds for the given time zone. This clock will always have the nano-of-second field set to zero:
这个静态方法返回给定时区的当前即时滴答声,单位为整秒。这个时钟的nano-of-second字段将始终设置为零。
ZoneId zoneId = ZoneId.of("Asia/Calcutta");
Clock clock = Clock.tickSeconds(zoneId);
System.out.println(clock.instant());
will produce:
将产生。
2018-04-07T17:40:23Z
The same can be achieved by using tick():
同样可以通过使用tick()来实现。
Clock clock = Clock.tick(Clock.system(ZoneId.of("Asia/Calcutta")), Duration.ofSeconds(1));
2.9. tickMinutes()
2.9. tickMinutes()
This static method returns the clock instant ticking in whole minutes for the specified timezone. This clock will always have the nano-of-second and second-of-minute fields set to zero:
这个静态方法返回指定时区的时钟瞬间的整点时间。这个时钟的纳秒和分秒字段将始终设置为零。
ZoneId zoneId = ZoneId.of("Asia/Calcutta");
Clock clock = Clock.tickMinutes(zoneId);
System.out.println(clock.instant());
will produce:
将产生。
2018-04-07T17:26:00Z
The same can be achieved by using tick():
同样可以通过使用tick()来实现。
Clock clock = Clock.tick(Clock.system(ZoneId.of("Asia/Calcutta")), Duration.ofMinutes(1));
2.10. withZone()
2.10.withZone()
This method returns a copy of this clock with a different time zone.
该方法返回一个具有不同时区的该时钟的副本。
If we have a clock instance for a specific time zone, we can make a copy of that clock for different time zone:
如果我们有一个特定时区的时钟实例,我们可以为不同的时区制作一个该时钟的副本。
ZoneId zoneSingapore = ZoneId.of("Asia/Singapore");
Clock clockSingapore = Clock.system(zoneSingapore);
System.out.println(clockSingapore.instant());
ZoneId zoneCalcutta = ZoneId.of("Asia/Calcutta");
Clock clockCalcutta = clockSingapore.withZone(zoneCalcutta);
System.out.println(clockCalcutta.instant());
will produce:
将产生。
2018-04-07T17:55:43.035Z
2018-04-07T17:55:43.035Z
2.11. getZone()
2.11.getZone()
This method returns the time zone of the given Clock.
该方法返回给定Clock的时区。
Clock clock = Clock.systemDefaultZone();
ZoneId zone = clock.getZone();
System.out.println(zone.getId());
will produce:
将产生。
Asia/Calcutta
2.12. fixed()
2.12. 固定()
This method returns a clock that always returns the same instant. The main use case for this method is in testing, where the fixed clock ensures that tests are not dependent on the current clock.
该方法返回一个时钟,总是返回同一时刻。这个方法的主要用例是在测试中,固定的时钟确保测试不依赖于当前的时钟。
Clock fixedClock = Clock.fixed(Instant.parse("2018-04-29T10:15:30.00Z"),
ZoneId.of("Asia/Calcutta"));
System.out.println(fixedClock);
will produce:
将产生。
FixedClock[2018-04-29T10:15:30Z,Asia/Calcutta]
3. Conclusion
3.结论
In this article, we dove into the Java Clock class and the different ways we can use it with the available methods.
在这篇文章中,我们深入研究了Java Clock类,以及我们可以通过可用的方法使用它的不同方式。
As always, the code examples are available over on GitHub.
像往常一样,代码示例可在GitHub上获得。