I’ve written a Java app in which I start the date at Jan 1 00:00 of a particular year. Then I increment the day 355 times and print out the results along the way. I was trying to understand how the timezones work around daylight savings time, and if adding 1 day would shift the time from midnight to 1am when crossing into daylight savings. My code looks like:
TimeZone tz = TimeZone.getDefault();
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss z");
sdf.setTimeZone(tz);
Calendar cal = Calendar.getInstance(tz);
cal.set(2008, 0, 1, 0, 0, 0);
cal.set(Calendar.MILLISECOND, 0);
for ( int i = 0; i < 355; i++ ) {
System.out.println(sdf.format(cal.getTime()));
cal.add(Calendar.DAY_OF_MONTH, 1);
}
When running in my default time zone (US Eastern), the output looks like as I would expect:
01/01/2008 00:00:00 EST
01/02/2008 00:00:00 EST
01/03/2008 00:00:00 EST
01/04/2008 00:00:00 EST
...
and when I cross into daylight savings time, I get
03/10/2008 00:00:00 EDT
03/11/2008 00:00:00 EDT
03/12/2008 00:00:00 EDT
When I use a timezone that observes Eastern European summer time, (in this case, I chose the Africa/Cairo timezone) I get the output:
01/01/2008 00:00:00 EET
01/02/2008 00:00:00 EET
01/03/2008 00:00:00 EET
01/04/2008 00:00:00 EET
01/05/2008 00:00:00 EET
but when I cross into Eastern European summer time, I get:
04/25/2008 01:00:00 EEST
04/26/2008 01:00:00 EEST
04/27/2008 01:00:00 EEST
And strangely when EEST ends, I still get
08/29/2008 01:00:00 EET
08/30/2008 01:00:00 EET
I’m confused as to why these timezones behave differently and if it is a bug with Africa/Cairo timezone, or if I am misunderstanding how the timezones work…
Any insight into this would be appreciated.
The Cairo time zone changes into daylight saving time at midnight – so the hour between midnight on April 25th 2008 was skipped… the wall clocks went:
Try adding one day at a time from 2am in the US Eastern time zone and you’ll see the same thing – it’ll go to 03:00 at some point.
It’s not clear what you’re trying to achieve exactly – I tend to think it’s better to either add “a number of experienced milliseconds” to a date/time in a particular time zone, or add “a number of logical milliseconds” to a local date/time, which doesn’t take time zones into account. When you convert from local time to time in a specific zone, you need to consider the possibility of ambiguity (one local time occurring twice) or skipping (one local time not occurring at all).
For added fun, last year Samoa skipped December 30th entirely, due to changing its time zone from -14 to +10…
EDIT: Oh, and my standard recommendation applies: ditch java.util.Calendar/Date, and go with Joda Time