ZonedDateTime和OffsetDateTime有什么区别? [英] What's the difference between ZonedDateTime and OffsetDateTime?

查看:270
本文介绍了ZonedDateTime和OffsetDateTime有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了文档,但是当我应该使用其中一个时,还是看不到:

根据文档OffsetDateTime,在将日期写入数据库时​​应使用,但我不知道为什么.

解决方案

问:java 8 ZonedDateTime和OffsetDateTime有什么区别?

javadocs这样说:

"OffsetDateTimeZonedDateTimeInstant都在时间轴上以纳秒级精度存储一个时刻.Instant是最简单的,仅表示该时刻.OffsetDateTime添加到该时刻与UTC/格林威治标准时间的偏移量,可以获取本地日期时间.ZonedDateTime添加完整的时区规则."

来源: https://docs.oracle. com/javase/8/docs/api/java/time/OffsetDateTime.html

因此,OffsetDateTimeZonedDateTime之间的区别在于,后者包括涵盖夏令时调整和各种其他异常的规则.

简单地陈述:

时区 =(

但是,如果您正在编写手动存储日期/时间值或依赖于java.sql.DateTime的应用程序,则可能需要避免区域ID的复杂性.因此,建议.

请注意,其含义/顺序随时间变化的日期对于应用程序可能有问题.而且由于更改区域规则只是边缘情况,因此这些问题很可能在意想不到的时间出现.


建议的第二个可能原因是ZonedDateTime的构造在某些点上是模棱两可的.例如,在您放回时钟"的时间段中,将本地时间和zone-id组合可以为您提供两个不同的偏移量. ZonedDateTime会一如既往地选择一个……但这并不总是正确的选择.

现在,对于以这种方式构造ZonedDateTime值的所有应用程序来说,这可能是个问题.但是,从某人构建企业应用程序的角度来看,当(可能是错误的)ZonedDateTime值保持不变并在以后使用时,这是一个更大的问题.

I've read the documentation, but I still can't get when I should use one or the other:

According to documentation OffsetDateTime should be used when writing date to database, but I don't get why.

解决方案

Q: What's the difference between java 8 ZonedDateTime and OffsetDateTime?

The javadocs say this:

"OffsetDateTime, ZonedDateTime and Instant all store an instant on the time-line to nanosecond precision. Instant is the simplest, simply representing the instant. OffsetDateTime adds to the instant the offset from UTC/Greenwich, which allows the local date-time to be obtained. ZonedDateTime adds full time-zone rules."

Source: https://docs.oracle.com/javase/8/docs/api/java/time/OffsetDateTime.html

Thus the difference between OffsetDateTime and ZonedDateTime is that the latter includes the rules that cover daylight saving time adjustments and various other anomalies.

Stated simply:

Time Zone = ( Offset-From-UTC + Rules-For-Anomalies )


Q: According to documentation OffsetDateTime should be used when writing date to database, but I don't get why.

Dates with local time offsets always represent the same instants in time, and therefore have a stable ordering. By contrast, the meaning of dates with full timezone information is unstable in the face of adjustments to the rules for the respective timezones. (And these do happen; e.g. for date-time values in the future.) So if you store and then retrieve a ZonedDateTime the implementation has a problem:

  • It can store the computed offset ... and the retrieved object may then have an offset that is inconsistent with the current rules for the zone-id.

  • It can discard the computed offset ... and the retrieved object then represents a different point in the absolute / universal timeline than the one that was stored.

If you use Java object serialization, the Java 9 implementation takes the first approach. This is arguably the "more correct" way to handle this, but this doesn't appear to be documented. (JDBC drivers and ORM bindings are presumably making similar decisions, and are hopefully getting it right.)

But if you are writing an application that manually stores date/time values, or that rely on java.sql.DateTime, then dealing with the complications of a zone-id is ... probably something to be avoided. Hence the advice.

Note that dates whose meaning / ordering is unstable over time may be problematic for an application. And since changes to zone rules are an edge case, the problems are liable to emerge at unexpected times.


A (possible) second reason for the advice is that the construction of a ZonedDateTime is ambiguous at the certain points. For example in the period in time when you are "putting the clocks back", combining a local time and a zone-id can give you two different offsets. The ZonedDateTime will consistently pick one over the other ... but this isn't always the correct choice.

Now, this could be a problem for any applications that construct ZonedDateTime values that way. But from the perspective of someone building an enterprise application is a bigger problem when the (possibly incorrect) ZonedDateTime values are persistent and used later.

这篇关于ZonedDateTime和OffsetDateTime有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆