# 3 Oct 2010 only has 23 hours?

Joseph Charles
Greenhorn
Posts: 4
Hi, I barely know what I am doing but ... I was playing with the Calendar class, trying to get the difference in milliseconds, then days, between two dates.
All was going well until I noticed, don't ask me how, that 3 Oct 2010 seems to only have 23 hours (0.958333 days).

I wrote, probably badly, a little console program to take in two dates and get the number of days between them. If you enter (dd/mm/yyyy) 3/10/2010 as the start date, and 4/10/2010 as the end date, the program gives a difference of 0.958333 days. All other days give a difference of 1.00000 days.

Is something wrong with my code, or the Calendar.

Mike Simmons
Ranch Hand
Posts: 3090
14
So, you're in the Southern hemisphere somewhere? It's the end of daylight saving time, or summer time.

Paul Clapham
Sheriff
Posts: 21111
32
The end of DST is when the clock is turned back, right? So that day has 25 hours (the hour when the clock is turned back happens twice). It's the start of DST when there's a day with 23 hours.

But yes, if the start of DST is in October than that would be in the Southern Hemisphere.

Mike Simmons
Ranch Hand
Posts: 3090
14
Hah, yes. I worked out the hemisphere correctly and then brilliantly replaced "start" with "end" for no good reason.

D. Ogranos
Ranch Hand
Posts: 214
Calendar also has a funny "feature" (?) when changing from winter to summer time: if you have a Date set to 2am the day before summer time change, and then add() one day, the hour field gets set to 1am. So, for example "2010-03-27 02:00" becomes "2010-03-28 01:00". It only happens when the hour field has the value 2, for all other values it works correctly (?). Cost me some hours of debugging once.

Joseph Charles
Greenhorn
Posts: 4
Doh! Yes, I'm in the south of Australia, so it will be the start of daylight saving time.
Thanks very much for your help. No doubt I'll be back with more uneducated observations.

Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
welcome to javaranch

Joseph Charles
Greenhorn
Posts: 4

I'm doing an introductory Java unit at uni, but thought that setting myself an extra-curricula project might be fun. So I'm trying to write a little program in which I can read my relatives' birthdates from a file, compare with the current date, and output something like "Uncle Fred's birthday in 23 days. He will be 102!", which will, hopefully, help me remember his birthday and become a beneficiary in his will.

So, I'm just after whole days difference.

I noticed, by being somewhat obsessive, that my little program was accurate up until 3 Oct, then seemed to be a day short. (As chronicled above.)

My first thought is to end the above program by adding a couple of hours to the daysDiff calculation and casting to an int:

int daysDiff = (int)(msDiff/ (1000 * 60 * 60 * 24) + 0.1);

So the calculation is always more than 1 day, and then the fractional part is dropped.

Is this acceptable, or is there a nicer way?

Rob Spoor
Sheriff
Posts: 20545
56
Do a search around the Java in General forum; calculating the date difference has come up before.

Joseph Charles
Greenhorn
Posts: 4
Thanks, Rob. Found an old post of yours that seems to be what I want.

Rob Spoor
Sheriff
Posts: 20545
56
You're quite welcome.

 It is sorta covered in the JavaRanch Style Guide.