1. Member
Join Date
Jul 2010
Posts
6
Rep Power
0

## 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:mS");
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)));

2. 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

3. Member
Join Date
Jul 2010
Posts
6
Rep Power
0
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?

4. Member
Join Date
Jul 2010
Posts
6
Rep Power
0
Running on two different machines - result the same!

Eclipse Platform
Version: 3.4.1
Build id: M20080911-1700

5. Ever heard of DST?

db

6. Member
Join Date
Jul 2010
Posts
6
Rep Power
0
Nope, but will look into it if u believe it is the reason

7. 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

8. Member
Join Date
Jul 2010
Posts
6
Rep Power
0
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!

9. Daylight Saving Time -- DST. Not DTS

db

10. Member
Join Date
Jul 2010
Posts
6
Rep Power
0
sure)) thanx for correcting anyway

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•