Connecting Tech Pros Worldwide Forums | Help | Site Map

Count Days Between Dates

Newbie
 
Join Date: Nov 2008
Posts: 2
#1: Nov 5 '08
I need help to count days between 2 dates,where i want to count the days between current date and registeration date.Here is the code that i have write but it only compare the days , it do not compare the month and the year, where it will return 0 if the day of the second date is the same as the first date even the month or year are different, plz help me...

my code:
public static int dayDiff(java.sql.Date date1, java.sql.Date date2)throws Exception
{
long diff = date1.getTime()-date2.getTime();
return (int)diff/1000/60/60/24;
}

Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#2: Nov 5 '08

re: Count Days Between Dates


Always first consider using the GregorianCalendar class whenever you need to fiddle with dates.
Expert
 
Join Date: Nov 2007
Location: Germany
Posts: 294
#3: Nov 5 '08

re: Count Days Between Dates


Your calculation is not correct!
Some days have 23 or 25 hours (daylight saving)
Also as what i have heard (but not verified), the day before new year is a few minutes and seconds shorter or longer than 24 hours.

GregorianCalendar is your friend to do the correct calulation for you.

Gregorian calendar:
365.2425 days has a year here.
the error rate to the real value is -0.0003.
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#4: Nov 5 '08

re: Count Days Between Dates


Quote:

Originally Posted by chaarmann

Your calculation is not correct!
Some days have 23 or 25 hours (daylight saving)
Also as what i have heard (but not verified), the day before new year is a few minutes and seconds shorter or longer than 24 hours.

Not that much! The braking effect of the tides does cause a slow down in the
earth rotation but not by several minutes per year; we'd all fall over yearly ;-)
Leap seconds are injected once or twice a year to compensate for the slow down.
Read this: http://tycho.usno.navy.mil/leapsec.html

kind regards,

Jos
Expert
 
Join Date: Nov 2007
Location: Germany
Posts: 294
#5: Nov 6 '08

re: Count Days Between Dates


Quote:

Originally Posted by JosAH

Not that much! The braking effect of the tides does cause a slow down in the
earth rotation but not by several minutes per year; we'd all fall over yearly ;-)
Leap seconds are injected once or twice a year to compensate for the slow down.
Read this: http://tycho.usno.navy.mil/leapsec.html

kind regards,

Jos

Thanks for the link, Jos.
I searched for such a link before, but couldn't find anything.
It is interesting to know that a leap second is mostly, but not always inserted at first January. From 1973 to now it was always either first January or July or no insertion every year. From 1961 to 1973 it was at different months. Before 1961 it did not happen.

Also very interesting that the GPS is already 14 seconds ahead of our time (UTC), because it is not adjusted for leap seconds.
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#6: Nov 6 '08

re: Count Days Between Dates


I haven't found any regularity in whether or not a second was inserted. I think it's
a matter of important old men that aye or naye for a second. It's the continuous
slow down of earth's rotation that complicates matters.

The good side of the medal is that they insert that second at night: we can sleep
longer then ;-)

kind regards,

Jos
Newbie
 
Join Date: Nov 2008
Posts: 2
#7: Nov 10 '08

re: Count Days Between Dates


sory im really a beginner..can u help me by giving code with some explanation?plz...urgent
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#8: Nov 10 '08

re: Count Days Between Dates


Read about that GregorianCalendar class. No one is going to write the code for you here.
myusernotyours's Avatar
Familiar Sight
 
Join Date: Nov 2007
Posts: 168
#9: Nov 10 '08

re: Count Days Between Dates


Quote:

Originally Posted by r035198x

Read about that GregorianCalendar class. No one is going to write the code for you here.

r0, You may add that the OP shouldn't take advantage of the one added second to sleep longer if they want to learn how to use the GregorianCalendar. :)

Regards,

Alex.
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#10: Nov 10 '08

re: Count Days Between Dates


Yes, that would be a waste of time, wouldn't it?
Reply