简体   繁体   English

Java GregorianCalendar时区

[英]Java GregorianCalendar Timezone

I have a weird problem with a Java Gregorian Calendar: 我有一个奇怪的Java Gregorian日历问题:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:S Z");
sdf.setTimeZone(TimeZone.getTimeZone("US/Pacific"));

GregorianCalendar cal1 = new GregorianCalendar(TimeZone.getTimeZone("US/Pacific"));
cal1.setTimeInMillis(1320566400000L);

GregorianCalendar cal2 = new GregorianCalendar(TimeZone.getTimeZone("US/Pacific"));
cal2.setTimeInMillis(1320570000000L);

System.out.println(sdf.format(cal1.getTime()));
System.out.println(sdf.format(cal2.getTime())); 

I executed the above given code on a machine with default timezone = US Pacific, but the machine is running in Germany. 我在默认时区= US Pacific的机器上执行了上面给出的代码,但机器在德国运行。

The result is the following: 结果如下:

2011-11-06 01:00:00:0 -0700
2011-11-06 01:00:00:0 -0800

I really do not understand, why there is a different time zone in the result... I also tested the code on another machine (default Timezone = GMT) and it works correct. 我真的不明白,为什么结果中有不同的时区...我还测试了另一台机器上的代码(默认时区= GMT),它的工作正常。

Do somebody have an idea, why this problem occurs? 有人有一个想法,为什么会出现这个问题?

Best, Michael 最好,迈克尔

Add these lines to your program: 将这些行添加到您的程序:

for (int i=0; i<24; i++) {
    cal1.add(Calendar.MINUTE, i*5);
    System.out.println(" : " + sdf.format(cal1.getTime()));
}

And you'll see: 你会看到:

 : 2011-11-06 01:00:00:0 -0700
 : 2011-11-06 01:05:00:0 -0700
 : 2011-11-06 01:15:00:0 -0700
 : 2011-11-06 01:30:00:0 -0700
 : 2011-11-06 01:50:00:0 -0700
 : 2011-11-06 01:15:00:0 -0800
 : 2011-11-06 01:45:00:0 -0800
 : 2011-11-06 02:20:00:0 -0800
 : 2011-11-06 03:00:00:0 -0800

So it seems you're changing summer time to winter time. 所以看来你正在把夏令时改到冬天。 My timezone is CET (UTC+01:00), so I can't tell why it's working on your second machine. 我的时区是CET(UTC + 01:00),所以我不知道为什么它在你的第二台机器上工作。

November 6, 2011 was when daylight savings time ended in the US. 2011年11月6日是夏令时在美国结束的时候。 So what you're seeing is the clock hit 2am on Nov 6th and then it rolls back to 1am to fall back an hour to standard time. 所以你看到的是11月6日凌晨2点的时钟,然后它又回到凌晨1点,回到标准时间一小时。 So the offset also changes from -7 to -8 from GMT I believe for Pacific time. 所以偏移也从格林威治标准时间-7变为-8,我相信太平洋时间。 So it is working correctly from what I can see. 所以它从我能看到的正常工作。

我建议你在Joda-Time中使用Gregorian年表

Please check in your control panel that you have not activated Summer's time. 请在您的控制面板中检查您是否未激活夏令时。 This bug may be because of this 这个错误可能是因为这个原因

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM