【发布时间】:2016-04-05 12:23:57
【问题描述】:
我很难理解以下代码的行为:
TimeZone zone = TimeZone.getTimeZone("Asia/Jerusalem");
DateFormat format = DateFormat.getDateTimeInstance();
format.setTimeZone(zone);
//printing out 4 different epoch dates
//print out using formatter with TZ
System.out.println(format.format(new Date(-1712458800000L)));
//print using Date's toString method
System.out.println(new Date(-1712458800000L));
System.out.println(format.format(new Date(-57844760000L)));
System.out.println(new Date(-57844760000L));
System.out.println(format.format(new Date(-1888228760000L)));
System.out.println(new Date(-1888228760000L));
System.out.println(format.format(new Date(1456920000000L)));
System.out.println(new Date(1456920000000L));
在我的本地机器(时区 GMT+2 耶路撒冷)上运行时,在 Windows 中选中“自动调整 DST 时钟”(这使我成为 GMT+3) 它产生以下输出:
Sep 26, 1915 11:20:40 PM
Sun Sep 26 23:20:40 IST 1915
Mar 2, 1968 2:00:40 PM
Sat Mar 02 14:00:40 IST 1968
Mar 2, 1910 2:21:20 PM
Wed Mar 02 14:21:20 IST 1910
Mar 2, 2016 2:00:00 PM
Wed Mar 02 14:00:00 IST 2016
但是当我取消选中它时(这使我成为 GMT+2),运行相同的代码会产生
Sep 26, 1915 11:20:40 PM
Sun Sep 26 23:00:00 GMT+02:00 1915
Mar 2, 1968 2:00:40 PM
Sat Mar 02 14:00:40 GMT+02:00 1968
Mar 2, 1910 2:21:20 PM
Wed Mar 02 14:00:40 GMT+02:00 1910
Mar 2, 2016 2:00:00 PM
Wed Mar 02 14:00:00 GMT+02:00 2016
第一个示例和第三个示例在使用 Date 的 toString() 时的分钟和秒不同。 如您所见,当我使用 DateFormat 明确设置 TZ 时,它会打印出相同的结果。 我在这里错过了什么?
谢谢!
【问题讨论】:
-
问题可能是您正在根据历史上(在 1910 年代)对 GMT 有不同偏移量的时区打印时间。
-
您可以在这里查看timeanddate.com/time/zone/israel/jerusalem 并选择
Time zone changes for1900-1924 期间。在那里,您可以看到所有年份的偏移量都不相同。但我不知道是什么原因造成的变化,在互联网上快速搜索时找不到。
标签: java timezone timezone-offset