This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes 3 Oct 2010 only has 23 hours? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "3 Oct 2010 only has 23 hours?" Watch "3 Oct 2010 only has 23 hours?" New topic
Author

3 Oct 2010 only has 23 hours?

Joseph Charles
Greenhorn

Joined: Jun 06, 2010
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

Joined: Mar 05, 2008
Posts: 2982
    
    9
So, you're in the Southern hemisphere somewhere? It's the end of daylight saving time, or summer time.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Mar 05, 2008
Posts: 2982
    
    9
Hah, yes. I worked out the hemisphere correctly and then brilliantly replaced "start" with "end" for no good reason.
D. Ogranos
Ranch Hand

Joined: Feb 02, 2009
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

Joined: Jun 06, 2010
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

Joined: Jan 28, 2008
Posts: 5575

welcome to javaranch
Joseph Charles
Greenhorn

Joined: Jun 06, 2010
Posts: 4
Just some additional info, questions:

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

Joined: Oct 27, 2005
Posts: 19649
    
  18

Do a search around the Java in General forum; calculating the date difference has come up before.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Joseph Charles
Greenhorn

Joined: Jun 06, 2010
Posts: 4
Thanks, Rob. Found an old post of yours that seems to be what I want.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

You're quite welcome.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: 3 Oct 2010 only has 23 hours?
 
Similar Threads
While Loop in Gantt Chart values
Date arithmetic
Getting months from date
Calendar class
Submit and Excel buttons and my Jsp