1. Introduction
1.导言
Date and time information must be processed accurately in Java, and this involves managing time zones. ZoneOffset.UTC and ZoneId.of(“UTC”) are two standard methods that we can use for displaying Coordinated Universal Time (UTC). While both look like UTC, they have some differences.
日期和时间信息必须在 Java 中准确处理,这涉及时区管理。ZoneOffset.UTC 和 ZoneId.of(“UTC”) 是我们可用于显示 协调世界时 (UTC) 的两种标准方法。虽然这两种方法看起来都像 UTC,但它们还是有一些区别。
In this tutorial, we’ll take an overview of both methods, key differences, and use cases.
在本教程中,我们将概述这两种方法、主要区别和使用案例。
2. The ZoneOffset.UTC
2.ZoneOffset.UTC<em
The java.time package is introduced from Java 8 and offers classes such as ZoneId and ZoneOffset that we can use to represent time zones. Beyond, the ZoneOffset.UTC is a constant member of the ZoneOffset class. It represents the fixed offset of UTC, which is always +00: 00. This translates to the fact regardless of the season, UTC is the same.
java.time包是从 Java 8 引入的,它提供了 ZoneId 和 ZoneOffset 等类,我们可以用它们来表示时区。此外,ZoneOffset.UTC 是 ZoneOffset 类的常量成员。它表示 UTC 的固定偏移量,始终为 +00: 00。这意味着无论季节如何,UTC 都是相同的。
Here’s an example of using ZoneOffset.UTC:
下面是使用 ZoneOffset.UTC 的示例:
@Test
public void givenOffsetDateTimeWithUTCZoneOffset_thenOffsetShouldBeUTC() {
OffsetDateTime dateTimeWithOffset = OffsetDateTime.now(ZoneOffset.UTC);
assertEquals(dateTimeWithOffset.getOffset(), ZoneOffset.UTC);
}
In the above code snippet, we first create an OffsetDateTime object representing the current date and time with the UTC offset. Following, we specify the UTC zone offset (which is zero hours ahead of UTC) using the ZoneOffset.UTC constant. Afterward, the result is verified using the assertEquals() method.
在上述代码片段中,我们首先创建了一个 OffsetDateTime 对象,该对象代表当前日期和时间以及 UTC 偏移。然后,我们使用 ZoneOffset.UTC 常量指定 UTC 区域偏移(比 UTC 提前零时)。然后,使用 assertEquals() 方法验证结果。
3. The ZoneId.of(“UTC”)
3.ZoneId.of(“UTC”)
On the other hand, ZoneId.of(“UTC”) creates a ZoneId instance representing the UTC zone. Unlike ZoneOffset.UTC, ZoneId.of() can be used to represent other time zones by changing the zone ID. Here’s an example:
另一方面,ZoneId.of(“UTC”) 会创建一个代表 UTC 时区的 ZoneId 实例。与 ZoneOffset.UTC 不同,ZoneId.of() 可以通过更改区域 ID 来代表其他时区。下面是一个例子:
@Test
public void givenZonedDateTimeWithUTCZoneId_thenZoneShouldBeUTC() {
ZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneId.of("UTC"));
assertEquals(zonedDateTime.getZone(), ZoneId.of("UTC"));
}
In the above code block, we create a ZonedDateTime object to represent the current date and time in the UTC zone. Then, we use the ZoneId.of(“UTC”) to specify the UTC zone.
在上述代码块中,我们创建了一个 ZonedDateTime 对象来表示 UTC 区域中的当前日期和时间。然后,我们使用 ZoneId.of(“UTC”) 指定 UTC 区。
4. The Differences and Use Cases
4.差异和用例
The following table summarizes the key differences between ZoneOffset.UTC and ZoneId.of(“UTC”):
下表总结了 ZoneOffset.UTC 和 ZoneId.of(“UTC”) 之间的主要区别:
Characteristic | ZoneOffset.UTC | ZoneId.of(“UTC”) |
---|---|---|
Immutability | Constant and Immutable | Flexible and Immutable |
Usage | Fixed offset of UTC | Can represent various time zones |
The following table provides use cases for both methods:
下表提供了这两种方法的使用案例:
Use Case | ZoneOffset.UTC | ZoneId.of(“UTC”) |
---|---|---|
Fixed Offset | Suitable for applications dealing exclusively with UTC | N/A (Use ZoneOffset.UTC) |
Flexibility for Different Time Zones | Use ZoneOffset.UTC if the fixed offset is sufficient | Suitable for scenarios involving multiple time zones |
Handling Various Time Zones | Use ZoneOffset.UTC for fixed UTC offset | Provides flexibility for handling different time zones |
5. Conclusion
5.结论
In conclusion, we take a good overview of the ZoneOffset.UTC and ZoneId.of(“UTC”) methods. Moreover, it is important to distinguish between both methods when handling time zones in Java.
总之,我们对 ZoneOffset.UTC 和 ZoneId.of(“UTC”) 方法进行了很好的概述。此外,在 Java 中处理时区时,区分这两种方法非常重要。
As usual, the accompanying source code can be found over on GitHub.
与往常一样,您可以在 GitHub 上找到随附的源代码。