Home arrow static arrow Java Programming [Archive] - can set any date except 28-03-2004 02:00:00
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - can set any date except 28-03-2004 02:00:00
This topic has 3 replies on 1 page.

Posts:2
Registered: 8/4/04
can set any date except 28-03-2004 02:00:00  
Aug 4, 2004 11:57 PM



 
It seems I cannot set the datetime to 28/03/2005 02:00:00 (the problem is the hour).
I have the following pieces code:

1.
Date d0 = new Date("03/28/2004 02:00:00");
System.out.println(d0);
prints out Sun Mar 28 03:00:00 CEST 2004: the hour is 3 instead of 2

2.
Calendar startCal = GregorianCalendar.getInstance(Locale.ITALY);
startCal.set(Calendar.DAY_OF_MONTH, 28);
startCal.set(Calendar.MONTH, Calendar.MARCH);
startCal.set(Calendar.YEAR, 2004);
startCal.set(Calendar.HOUR_OF_DAY, 0);
startCal.set(Calendar.MINUTE, 0);
startCal.set(Calendar.SECOND, 0);
startCal.set(Calendar.MILLISECOND, 0);
Date start = startCal.getTime();

Calendar endCal = GregorianCalendar.getInstance();
endCal.set(Calendar.DAY_OF_MONTH, 28);
endCal.set(Calendar.MONTH, Calendar.MARCH);
endCal.set(Calendar.YEAR, 2004);
endCal.set(Calendar.HOUR_OF_DAY, 5);
endCal.set(Calendar.MINUTE, 0);
endCal.set(Calendar.SECOND, 0);
endCal.set(Calendar.MILLISECOND, 0);
Date end = endCal.getTime();

while(!startCal.getTime().after(end)){
startCal.add(Calendar.HOUR_OF_DAY, 1);
System.out.println(startCal.getTime());
}
It prints out
Sun Mar 28 01:00:00 CET 2004
Sun Mar 28 03:00:00 CEST 2004
Sun Mar 28 04:00:00 CEST 2004
Sun Mar 28 05:00:00 CEST 2004
Sun Mar 28 06:00:00 CEST 2004
and it skips Sun Mar 28 03:00:00 CEST 2004

3.
Calendar cal = GregorianCalendar.getInstance(Locale.ITALY);
cal.set(Calendar.DAY_OF_MONTH, 28);
cal.set(Calendar.MONTH, Calendar.MARCH);
cal.set(Calendar.YEAR, 2004);
cal.set(Calendar.HOUR_OF_DAY, 2);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
Date d = cal.getTime();
System.out.println(d);
it prints out Sun Mar 28 03:00:00 CEST 2004 !!!!

It seems I am unable to set hour to 2 am for the 28th March 2004. The same for 27th 2005.
Where I am wrong ? Is it a bug ? For the other days it works correctly.

thanks for your help
 

Posts:319
Registered: 11/8/00
Re: can set any date except 28-03-2004 02:00:00  
Aug 5, 2004 12:23 AM (reply 1 of 3)



 
Hi

Your problem is due to Daylight Saving Time. The dates you are using are the dates seheduled for the switch between GMT+1 and GMT+2

See: http://wwp.greenwichmeantime.com/time-zone/europe/italy/
 

Posts:150
Registered: 22/07/04
Re: can set any date except 28-03-2004 02:00:00  
Aug 5, 2004 12:25 AM (reply 2 of 3)



 
It's Daylight Savings. On 28th March this year your clocks change, you lose an hour. Java takes this into account. Don't worry, you get it back again later ;)
 

Posts:319
Registered: 11/8/00
Re: can set any date except 28-03-2004 02:00:00  
Aug 5, 2004 12:26 AM (reply 3 of 3)



 
**** why can't you edit posts!

Not sure what java's problem is but you know the cause now so study the API for any clues

http://java.sun.com/j2se/1.4.2/docs/api/
 
This topic has 3 replies on 1 page.