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)?

[1628 byte] By [Peetzorea] at [2007-11-26 16:33:07]
# 1

If MET is "middle europe time" then MEST is middle europe summer time. Summer time was still used on October 26; the change to winter time happened on Sunday October 29. This means that October 29 was 23 hours long instead of the usual 24 hours.

If you want to keep the time of the day the same but change only the date, use the Calendar.add or Calendar.set methods. Read the API documentation carefully.

jsalonena at 2007-7-8 22:57:47 > top of Java-index,Java Essentials,Java Programming...
# 2
Calendars (and astronomy) are hairy issues, they were not without reason the driving forces to science like mathematics...
BIJ001a at 2007-7-8 22:57:47 > top of Java-index,Java Essentials,Java Programming...
# 3
> This means that October 29 was 23 hours long Sorry, that would be 25 hours. The clocks were moved backwards making the day one hour longer, not shorter.
jsalonena at 2007-7-8 22:57:47 > top of Java-index,Java Essentials,Java Programming...
# 4
Thanks, that makes sence...
Peetzorea at 2007-7-8 22:57:47 > top of Java-index,Java Essentials,Java Programming...