Differences Between ZonedDateTime and OffsetDateTime – ZonedDateTime和OffsetDateTime之间的区别

最后修改: 2019年 1月 12日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

ZonedDateTime and OffsetDateTime are pretty popular classes in the Java 8 DateTime API. Furthermore, both store an instant on the timeline up to a precision of nanoseconds. And, at first, it may get confusing to choose between them.

ZonedDateTimeOffsetDateTimeJava 8 DateTime API中相当流行的类。此外,两者都在时间线上存储一个精度为纳秒的瞬间而且,一开始,在它们之间选择可能变得很困惑。

In this quick tutorial, we’re going to look at the differences between ZonedDateTime and OffsetDateTime.

在这个快速教程中,我们要看一下ZonedDateTimeOffsetDateTime之间的区别。

2. ZonedDateTime

2.分区日期时间

A ZonedDateTime is an immutable representation of a date-time with a timezone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30+01:00 Europe/Paris. It holds state equivalent to three separate objects: a LocalDateTime, a ZoneId, and the resolved ZoneOffset

AZonedDateTime是ISO-8601日历系统中带有时区的日期时间的不可改变的表示,例如2007-12-03T10:15:30+01:00 Europe/Paris。它持有相当于三个独立对象的状态:一个LocalDateTime、一个ZoneId和已解决的ZoneOffset

Here, the ZoneId determines how and when the offset changes. So, the offset can’t be freely set, as the zone controls which offsets are valid.

这里,ZoneId决定了偏移量的变化方式和时间。所以,偏移量不能自由设置,因为区域控制哪些偏移量是有效的。

To get the current ZonedDateTime for a specific region, we’ll use:

要获得一个特定地区的当前ZonedDateTime,我们将使用。

ZoneId zone = ZoneId.of("Europe/Berlin");
ZonedDateTime zonedDateTime = ZonedDateTime.now(zone);

The ZonedDateTime class also provides built-in methods for converting a given date from one timezone to another:

The ZonedDateTime类还提供了内置的方法,用于将一个给定的日期从一个时区转换到另一个时区。

ZonedDateTime destZonedDateTime = sourceZonedDateTime.withZoneSameInstant(destZoneId);

Finally, it’s fully DST-aware and handles daylight saving time adjustments. It often comes in handy when we want to display a date-time field in a specific timezone.

最后,它是完全DST感知的,可以处理夏令时的调整。当我们想在一个特定的时区显示一个日期-时间字段时,它经常派上用场。

3. OffsetDateTime

3.OffsetDateTime

An OffsetDateTime is an immutable representation of a date-time with an offset from UTC/Greenwich in the ISO-8601 calendar system, such as 2007-12-03T10:15:30+01:00. In other words, it stores all date and time fields, to a precision of nanoseconds, as well as the offset from GMT/UTC.

一个 OffsetDateTime是一个不可改变的日期时间表示,它与ISO-8601日历系统中的UTC/Greenwich有一个偏移,例如2007-12-03T10:15:30+01:00。换句话说,它存储所有的日期和时间字段,精度为纳秒,以及从GMT/UTC的偏移量

Let’s get the current OffsetDateTime with a two-hour offset from GMT/UTC:

让我们来获取当前的OffsetDateTime 与GMT/UTC有两小时的偏移。

ZoneOffset zoneOffSet= ZoneOffset.of("+02:00");
OffsetDateTime offsetDateTime = OffsetDateTime.now(zoneOffSet);

4. The Main Differences

4.主要的差异

First, it doesn’t make sense (without conversions) to directly compare two dates with full timezone information. Therefore, we should always prefer storing OffsetDateTime in the database over the ZonedDateTime, as dates with a local time offset always represent the same instants in time.

首先,直接比较具有完整时区信息的两个日期是没有意义的(没有转换)。因此,我们应该总是倾向于在数据库中存储OffsetDateTime而不是ZonedDateTime,因为具有本地时间偏移的日期总是代表相同的时间时刻。

Moreover, unlike with the ZonedDateTime, adding an index over a column storing the OffsetDateTime won’t change the meaning of the date.

此外,与ZonedDateTime不同,在存储OffsetDateTime的列上添加索引不会改变日期的含义。

Let’s quickly sum up the key differences.

让我们快速总结一下关键的区别。

ZonedDateTime:

ZonedDateTime:

  • stores all date and time fields, to a precision of nanoseconds, and a timezone, with a zone offset used to handle ambiguous local date-times
  • can’t freely set offsets, as the zone controls the valid offset values
  • is fully DST-aware and handles daylight savings adjustments
  • comes in handy for displaying date-time fields in a user-specific timezone

OffsetDateTime:

OffsetDateTime:

  • stores all date and time fields, to a precision of nanoseconds, as well as the offset from GMT/UTC (no timezone information)
  • should be used for storing a date in the database or communicating it over a network

5. Conclusion

5.总结

In this tutorial, we covered the differences between the ZonedDateTime and the OffsetDateTime.

在本教程中,我们介绍了ZonedDateTimeOffsetDateTime之间的区别。

As usual, the complete source code is available over on Github.

像往常一样,完整的源代码可在Github上获得。