# GregorianCalendar mishap

• 07-29-2010, 09:01 PM
kasur
GregorianCalendar mishap
Well, guys, hello out there! I have recently faced a problem concerning calculation of the number of days between specific dates (within creating certain logic). I have simplified and cut off redundant code to show you the core of the problem. Below you will find a snap of the code that produces different output results for consecutive dates.

To demonstrate the trouble let us leave dateEnd unchanged and step up dateBegin from 2010,2,27 then 2010,2,28 then finally 2010,2,29
So, it performs correctly and gives out an integer and exact number of days between dateEnd and dateBegin correspondently 280,00 279,00 but when dateBegin is 2010,2,29 there comes out 278,04167 as the number of days.

Does anybody knows or has a slightest idea how to tackle this? Thanx.

GregorianCalendar dateBegin=new GregorianCalendar(2010,2,29);
GregorianCalendar dateEnd=new GregorianCalendar(2011,0,1);
SimpleDateFormat dtf=new SimpleDateFormat("dd/MM/yyyy H:m:s:S");
DecimalFormat ddtf=new DecimalFormat("0.00000");
System.out.println(dtf.format(dateBegin.getTime()) );
System.out.println(dtf.format(dateEnd.getTime()));
System.out.println(dateEnd.getTimeInMillis()-dateBegin.getTimeInMillis());
System.out.println(ddtf.format((dateEnd.getTimeInM illis()-dateBegin.getTimeInMillis())/(24.0*60*60*1000L)));
• 07-29-2010, 09:32 PM
Norm
When I run it I get this output:
29/03/2010 0:0:0:0
01/01/2011 0:0:0:0
24019200000
278.00000
• 07-29-2010, 09:38 PM
kasur
29/03/2010 0:0:0:0
01/01/2011 0:0:0:0
24022800000
278,04167

Should say that the difference is exactly 1 hour.... Rather interesting thing don't u think?
• 07-29-2010, 09:40 PM
kasur
Running on two different machines - result the same!

Eclipse Platform
Version: 3.4.1
Build id: M20080911-1700
• 07-29-2010, 11:20 PM
DarrylBurke
Ever heard of DST?

db
• 07-30-2010, 07:00 AM
kasur
Nope, but will look into it if u believe it is the reason
• 07-30-2010, 07:30 AM
JosAH
Quote:

Originally Posted by Darryl.Burke
Ever heard of DST?

Yep, that's the cause: 24022800000 millis is not a multiple of 24 hours (one hour too many). The Gregorian calendar anticipated on DST adjustment.

kind regards,

Jos
• 07-30-2010, 08:11 AM
kasur
Well, thanx everybody for yr help and time spent. Now when i am directed to read up on DTS, hope i'll find cure for that mishap. Thanx!
• 07-30-2010, 08:29 AM
DarrylBurke
Daylight Saving Time -- DST. Not DTS

db
• 07-30-2010, 10:15 AM
kasur
sure)) thanx for correcting anyway