Java Gregorian calendar time zone
•
Java
I have a strange question about Java Gregorian calendar:
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 code given above on the machine with default time zone = US Pacific, but the machine is running in Germany
The results are as follows:
2011-11-06 01:00:00:0 -0700 2011-11-06 01:00:00:0 -0800
I really don't understand why there are different time zones in the results... I also tested the code on another machine (default time zone = GMT), and it works normally
Someone has an idea. Why is this problem?
Best, Michael
Solution
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())); }
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 seems that you are changing daylight saving time to winter My time zone is CET (UTC 01:00), so I don't understand why it works on your second machine
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码