java - 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 !
解决方案
时区规则是通过抽象ZoneRulesProvider
类提供的。ZoneRulesProvider
文档在的类描述中提到了默认实现。
Java 虚拟机有一个默认提供程序,为 IANA 时区数据库 (TZDB) 定义的时区提供区域规则
因此,所有规则都源自 IANA 维护的时区数据库,并且它的副本随 JVM 一起提供。
对于所有时区,tz 数据库都定义了规则,即时钟时间已更改或将要更改的转换。通过这种方式,人们可以确定在时间线上的任意时刻它曾经是或将是什么日期和时间。
所以在2001 年 10 月 26 日1,UTC 偏移量显然是 +01:00。
我必须添加两件事。首先,我相信来自 tzdb 的数据,因为它的维护者可能对时区及其规则的工作方式有更好的理解。其次,正如 Joachim 在评论中已经提到的,时区是一个世纪前发明的概念,因此将时区与 -2001 等年份结合起来有点意义。
1请注意,世界上大多数人使用的日历是公历。在 -2001 年,那个日历还没有发明。该java.time
软件包使用预测的公历。
推荐阅读
- javascript - 拼接第一个对象返回TypeError: Cannot read property of undefined
- java - Thymeleaf 模板不发布正文
- c# - 我无法解决 - 无法从具有索引像素格式错误的图像创建图形对象
- google-cloud-platform - 使用 nodejs 10 运行时遵循谷歌云工作流教程时出错
- java - 为什么java变量只能包含下划线和美元符号
- debugging - PyCharm 调试器越过跳过一行?
- basic - 我能做些什么来运行这个 BASIC 程序?
- pandas - 在python中计算一小时内的不同动作
- linux - 有没有办法将 bash 脚本中的标准输出存储到 GO 中的变量中?
- firebase - Firebase storage.rules 版本 2