Issue with GregorianCalendar class

Hi I'm having issues with the GregorianCalender class. I am trying to enable daylight savings time but it doesn't seem to be working properly for. As a test I have taken the difference of time (in milliseconds) between a date where daylight savings causes a shift in time (April 4, 2004 for example). The two sample dates I have chosen are April 5, 2004 and April 4, 2004, ideally the time difference should be 23 hrs (since one hour is lost due to daylight savings) but I am getting 24hrs. I have a sample program in C++ which gives me the correct answer so I know the result am I getting here is wrong. Anybody have any suggestions?

String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);

if (ids.length == 0)

System.exit(0);

// create a Pacific Standard Time time zone

SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);

// set up rules for daylight savings time

pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 60 * 60 * 1000, true);

pdt.setEndRule(Calendar.OCTOBER, 31, Calendar.SUNDAY, 60 * 60 * 1000, false);

pdt.setDSTSavings( 60*60*1000 );

GregorianCalendar cal = new GregorianCalendar(2004, 4, 4);

GregorianCalendar cal1 = new GregorianCalendar(2007, 4, 5);

cal1.setTimeZone ( pdt );

cal.setTimeZone( pdt );

//This values is incorrect

long diff = cal1.getTimeInMillis() - cal.getTimeInMillis();

//I have also tried the following

String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);

if (ids.length == 0)

System.exit(0);

// create a Pacific Standard Time time zone

SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);

// set up rules for daylight savings time

pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 60 * 60 * 1000, true);

pdt.setEndRule(Calendar.OCTOBER, 31, Calendar.SUNDAY, 60 * 60 * 1000, false);

pdt.setDSTSavings( 60*60*1000 );

GregorianCalendar cal = new GregorianCalendar(pdt);

GregorianCalendar cal1 = new GregorianCalendar(pdt);

cal1.set(2004, 4, 5 );

cal.set( 2004, 4, 4 );

//This values is incorrect

long diff = cal1.getTimeInMillis() - cal.getTimeInMillis();

Thanks

[2291 byte] By [KrazyEyez] at [2007-9-30 5:38:07]
# 1

There may be any of several issues:

> Daylight savings time moves from year to year. Maybe last year, it was not on the same week number.

> Daylight savings time officially occurs at 2am. Java date's default to midnight if explicitly initialized. So, try calculating from after 2-3 am, depending on which way time was shifted.

- Saish

"My karma ran over your dogma." - Anon

Saish at 2007-7-1 18:14:30 > top of Java-index,Administration Tools,Sun Connection...
# 2
Sorry, that Date intiialization comment came out funny. If you explicltly set month, date and year in GregorianCalendar, the time should be midnight. - Saish"My karma ran over your dogma." - Anon
Saish at 2007-7-1 18:14:30 > top of Java-index,Administration Tools,Sun Connection...
# 3
cal1.set(2004, 4, 5 );cal.set( 2004, 4, 4 );The month field in Calendar.set(year, month, date) is zero-based, so you're actually setting the calendars to May, not April.Dave
jstorm321 at 2007-7-1 18:14:30 > top of Java-index,Administration Tools,Sun Connection...
# 4
Yeah I figured that out after some painful debugging. Thanks
KrazyEyez at 2007-7-1 18:14:30 > top of Java-index,Administration Tools,Sun Connection...