首页 > 解决方案 > Different UTC offset for a date&time and its negative counterpart

问题描述

On my local configuration (Eclipse 4.11 (2019.03), java runtime 1.8.0, SDK 1.8.0), when converting a local date & time (provided through separate strings) to a CET ZonedDateTime (using an intermediary LocalDateTime built from those strings), I get the following outputs :

input : "2001-10-26" and "21:32:52" => output : 2001-10-26T21:32:52+02:00[CET]
input : "-2001-10-26" and "21:32:52" => output :-2001-10-26T21:32:52+01:00[CET]

So, we see that the UTC offset is not the same. Of course, we refer to a moment in time where "UTC" and "UTC offset" had not yet been defined... However, I guess that java designers have implemented some rules for those cases as java process them anyway. Could someone give me some enlighten about this ? I have already seen this interesting post Java 8 - tz database time zones but it stays rather vague.

Thanks for helping me with this !

标签: java

解决方案


时区规则是通过抽象ZoneRulesProvider类提供的。ZoneRulesProvider文档在的类描述中提到了默认实现。

Java 虚拟机有一个默认提供程序,为 IANA 时区数据库 (TZDB) 定义的时区提供区域规则

因此,所有规则都源自 IANA 维护的时区数据库,并且它的副本随 JVM 一起提供。

对于所有时区,tz 数据库都定义了规则,即时钟时间已更改或将要更改的转换。通过这种方式,人们可以确定在时间线上的任意时刻它曾经是或将是什么日期和时间。

所以在2001 年 10 月 26 日1,UTC 偏移量显然是 +01:00。

我必须添加两件事。首先,我相信来自 tzdb 的数据,因为它的维护者可能对时区及其规则的工作方式有更好的理解。其次,正如 Joachim 在评论中已经提到的,时区是一个世纪前发明的概念,因此将时区与 -2001 等年份结合起来有点意义。


1请注意,世界上大多数人使用的日历是公历。在 -2001 年,那个日历还没有发明。该java.time软件包使用预测的公历


推荐阅读