Java Time Zone GregorianCalendar

I have a strange problem with the Java Gregorian calendar:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SZ"); 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 code on a machine with the default time zone = US Pacific, but the machine works in Germany.

The result is the following:

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

I really donโ€™t understand why the result is a different time zone ... I also checked the code on another computer (Timezone = GMT by default) and it works correctly.

Does anyone have an idea why this problem occurs?

Best Michael

+6
source share
4 answers

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 will 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 looks like you are changing summer time to winter time. My time zone is CET (UTC + 01: 00), so I canโ€™t say why it works on your second machine.

+5
source

November 6, 2011, when daylight saving time ended in the USA. So, what you see is a 2am hourly hit on November 6th, and then it goes back to 1 oโ€™clock to go back to standard time. Thus, the offset also varies from -7 to -8 from GMT, which I count for Pacific time. Therefore, it works correctly, what I see.

+2
source

I would advise you to use Gregorian chronology in Yoda-Time.

+1
source

Please check on the control panel that you have not activated Daylight Saving. This error may be due to this

0
source

Source: https://habr.com/ru/post/918082/


All Articles