Why does the program printout the same output/time with different TimeZone?
Calendar cal = GregorianCalendar.getInstance();
TimeZone timeZone_1 = TimeZone.getTimeZone("Asia/Rangoon");
cal.setTimeZone(timeZone_1);
System.out.println(cal.getTime());
TimeZone timeZone_2 = TimeZone.getTimeZone("Asia/Tokyo");
cal.setTimeZone(timeZone_2);
System.out.println(cal.getTime());
Example Output :
Thu Nov 22 09:00:33 MMT 2012
Thu Nov 22 09:00:33 MMT 2012
My Expected Output is:
Thu Nov 22 09:00:33 MMT 2012
Thu Nov 22 11:30:33 MMT 2012
Your code is fine, it’s only the debug output that’s wrong (misleading).
cal.getTime()returnsDateobject, which is independent from time zone. ButDate.toString()always prints this date with systems’ time zone.Date.toString()is so counterintuitive (it displays calendar time with system time zone while it barely stores number of millliseconds) that it should have been banned/deprecated.To have accurate logging use
SimpleDateFormator call variousCalendar.get*()methods instead:And as always with any questions regarding Java date/time handling, consider jodatime…