1. Introduction
1.绪论
In our world, each country follows a certain time-zone. These time-zones are crucial for expressing time conveniently and effectively. However, time-zones can sometimes be inexplicit due to variables such as daylight saving time, coming into the picture.
在我们的世界里,每个国家都遵循一定的时区。这些时区对于方便和有效地表达时间至关重要。然而,由于日光节约时间等变量的出现,时区有时会不明确。
Moreover, while representing these time-zones in our code, things can get confusing. Java has provided multiple classes such as Date, Time and DateTime in the past to also take care of time-zones.
此外,在我们的代码中表示这些时区时,事情会变得很混乱。Java在过去提供了多个类,如Date、Time和DateTime,以同样照顾到时区。
However, new Java versions have come up with more useful and expressive classes such as ZoneId and ZoneOffset, for managing time-zones.
然而,新的Java版本已经出现了更有用、更有表现力的类,如ZoneId和ZoneOffset,用于管理时区。
In this article, we’ll discuss ZoneId and ZoneOffset as well as related DateTime classes.
在这篇文章中,我们将讨论ZoneId和ZoneOffset以及相关的DateTime类。
We can also read about the new set of DateTime classes introduced in Java 8, in our previous post.
我们还可以在上一篇文章中了解Java 8中引入的一组新的DateTime类。
2. ZoneId and ZoneOffset
2.ZoneId和ZoneOffset
With the advent of JSR-310, some useful APIs were added for managing date, time and time-zones. ZoneId and ZoneOffset classes were also added as a part of this update.
随着JSR-310的出现,一些有用的API被添加到管理日期、时间和时区中。ZoneId和ZoneOffset类也作为此次更新的一部分被添加。
2.1. ZoneId
2.1.区号
As stated above, ZoneId is a representation of the time-zone such as ‘Europe/Paris‘.
如上所述,ZoneId是时区的代表,如’Europe/Paris‘。
There are 2 implementations of ZoneId. First, with a fixed offset as compared to GMT/UTC. And second, as a geographical region, which has a set of rules to calculate the offset with GMT/UTC.
ZoneId有两种实现方式。首先,与GMT/UTC相比,有一个固定的偏移。第二,作为一个地理区域,它有一套规则来计算与GMT/UTC的偏移。
Let’s create a ZoneId for Berlin, Germany:
让我们为德国柏林创建一个ZoneId。
ZoneId zone = ZoneId.of("Europe/Berlin");
2.2. ZoneOffset
2.2. ZoneOffset
ZoneOffset extends ZoneId and defines the fixed offset of the current time-zone with GMT/UTC, such as +02:00.
ZoneOffset扩展了ZoneId和定义当前时区与GMT/UTC的固定偏移,如+02:00。
This means that this number represents fixed hours and minutes, representing the difference between the time in current time-zone and GMT/UTC:
这意味着这个数字代表固定的小时和分钟,代表当前时区的时间与GMT/UTC之间的差异。
LocalDateTime now = LocalDateTime.now();
ZoneId zone = ZoneId.of("Europe/Berlin");
ZoneOffset zoneOffSet = zone.getRules().getOffset(now);
In case a country has 2 different offsets – in summer and winter, there will be 2 different ZoneOffset implementations for the same region, hence the need to specify a LocalDateTime.
如果一个国家有2个不同的偏移量–在夏季和冬季,同一地区将有2个不同的ZoneOffset实现,因此需要指定一个LocalDateTime。
3. DateTime Classes
3.DateTime类
Next let’s discuss some DateTime classes, that actually take advantage of ZoneId and ZoneOffset.
接下来让我们讨论一些DateTime类,它们实际上利用了ZoneId和ZoneOffset的优势。
3.1. ZonedDateTime
3.1.ZonedDateTime
ZonedDateTime is an immutable representation of a date-time with a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30+01:00 Europe/Paris. A ZonedDateTime holds state equivalent to three separate objects, a LocalDateTime, a ZoneId and the resolved ZoneOffset.
ZonedDateTime是ISO-8601日历系统中带有时区的日期时间的不可改变的表示,例如2007-12-03T10:15:30+01:00 Europe/Paris。一个ZonedDateTime持有相当于三个独立对象的状态,一个LocalDateTime,一个ZoneId和解决的ZoneOffset。
This class stores all date and time fields, to a precision of nanoseconds, and a time-zone, with a ZoneOffset, to handle ambiguous local date-times. For example, ZonedDateTime can store the value “2nd October 2007 at 13:45.30.123456789 +02:00 in the Europe/Paris time-zone”.
这个类存储所有的日期和时间字段,精度为纳秒,还有一个时区,带有一个ZoneOffset,以处理模糊的本地日期时间。例如,ZonedDateTime可以存储 “2007年10月2日13:45.30.123456789 +02:00在欧洲/巴黎时区 “的值。
Let’s get the current ZonedDateTime for the previous region:
让我们获得前一个区域的当前ZonedDateTime。
ZoneId zone = ZoneId.of("Europe/Berlin");
ZonedDateTime date = ZonedDateTime.now(zone);
ZonedDateTime also provides inbuilt functions, to convert a given date from one time-zone to another:
ZonedDateTime还提供了内置的函数,将一个给定的日期从一个时区转换到另一个时区。
ZonedDateTime destDate = sourceDate.withZoneSameInstant(destZoneId);
3.2. OffsetDateTime
3.2.OffsetDateTime
OffsetDateTime is an immutable representation of a date-time with an offset in the ISO-8601 calendar system, such as 2007-12-03T10:15:30+01:00.
OffsetDateTime是一个在ISO-8601日历系统中带有偏移量的日期时间的不可改变的表示,例如2007-12-03T10:15:30+01:00。
This class stores all date and time fields, to a precision of nanoseconds, as well as the offset from GMT/UTC. For example,OffsetDateTime can store the value “2nd October 2007 at 13:45.30.123456789 +02:00”.
该类存储所有的日期和时间字段,精度为纳秒,以及从GMT/UTC的偏移量。例如,OffsetDateTime可以存储 “2007年10月2日13:45.30.123456789 +02:00 “的值。
Let’s get the current OffsetDateTime with 2 hours of offset from GMT/UTC:
让我们得到当前的OffsetDateTime与GMT/UTC有2小时的偏移。
ZoneOffset zoneOffSet= ZoneOffset.of("+02:00");
OffsetDateTime date = OffsetDateTime.now(zoneOffSet);
3.3. OffsetTime
3.3. OffsetTime
OffsetTime is an immutable date-time object that represents a time, often viewed as hour-minute-second-offset, in the ISO-8601 calendar system, such as 10:15:30+01:00.
OffsetTime是一个不可改变的日期时间对象,它代表了ISO-8601日历系统中的一个时间,通常被视为小时-分钟-秒的偏移,例如10:15:30+01:00。
This class stores all time fields, to a precision of nanoseconds, as well as a zone offset. For example, OffsetTime can store the value “13:45.30.123456789+02:00”.
该类存储所有的时间字段,精度为纳秒,以及一个区域偏移量。例如,OffsetTime可以存储值 “13:45.30.123456789+02:00″。
Let’s get the currentOffsetTime with 2 hours of offset:
让我们得到当前的OffsetTime与2小时的偏移。
ZoneOffset zoneOffSet = ZoneOffset.of("+02:00");
OffsetTime time = OffsetTime.now(zoneOffSet);
4. Conclusion
4.总结
Getting back to the focal point, ZoneOffset is a representation of time-zone in terms of the difference between GMT/UTC and the given time. This is a handy way of representing time-zone, although there are other representations also available.
回到重点,ZoneOffset是用GMT/UTC和给定时间之间的差异来表示时区。这是表示时区的一种方便的方法,尽管还有其他的表示方法可用。
Moreover, ZoneId and ZoneOffset are not only used independently but also by certain DateTime Java classes such as ZonedDateTime, OffsetDateTime, and OffsetTime.
此外,ZoneId和ZoneOffset不仅独立使用,而且还被某些DateTime Java类使用,如ZonedDateTime、OffsetDateTime和OffsetTime。
As usual, the code is available in our GitHub repository.
像往常一样,代码可以在我们的GitHub仓库中找到。