Calendars, TimeZones and DateFormats... odd creatures!
I am writing some cleanup function that should erase all entries from a database before a certain date/time... I am working on a Solaris server set up to work in MET. The strange behaviour I'm seeing is that the timezone setting changes when I modify the time of my GregorianCalendar object. The following code illustrates the problem.
TimeZone metZone = TimeZone.getTimeZone("MET");
SimpleDateFormat sdf =new SimpleDateFormat("yyyy.MM.dd HH:mm:ss:SSS z");
sdf.setTimeZone(metZone);
long sysParamRetentionSafety = 95;
long offset = sysParamRetentionSafety * DAY_IN_MILLIS;
Calendar cal = Calendar.getInstance();
cal.setTimeZone(metZone);
System.out.println(cal.getTimeZone().getDisplayName() +" " + cal.getTimeZone().getID());
System.out.println("Test run ended at " + sdf.format(cal.getTime()));
cal.setTimeInMillis(cal.getTimeInMillis() - offset);
sdf.setTimeZone(metZone);
System.out.println("Removed entries for " + sdf.format(cal.getTime()));
I get the following output:
[java] Test run started
[java] Middle Europe Time MET
[java] Test run ended at 2007.01.29 11:31:21:907 MET
[java] Removed entries for 2006.10.26 12:31:21:907 MEST
Why does the same SimpleDateFormatter display the content of the same Calendar object in a different timezone (even if I explicitly set it to MET)?

