469,349 Members | 1,532 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,349 developers. It's quick & easy.

Count Days Between Dates

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;
}
Nov 5 '08 #1
9 11200
r035198x
13,262 8TB
Always first consider using the GregorianCalendar class whenever you need to fiddle with dates.
Nov 5 '08 #2
chaarmann
785 Expert 512MB
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.
Nov 5 '08 #3
JosAH
11,448 Expert 8TB
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
Nov 5 '08 #4
chaarmann
785 Expert 512MB
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.
Nov 6 '08 #5
JosAH
11,448 Expert 8TB
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
Nov 6 '08 #6
sory im really a beginner..can u help me by giving code with some explanation?plz...urgent
Nov 10 '08 #7
r035198x
13,262 8TB
Read about that GregorianCalendar class. No one is going to write the code for you here.
Nov 10 '08 #8
myusernotyours
188 100+
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.
Nov 10 '08 #9
r035198x
13,262 8TB
Yes, that would be a waste of time, wouldn't it?
Nov 10 '08 #10

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

7 posts views Thread by Bambero | last post: by
2 posts views Thread by jpr | last post: by
2 posts views Thread by DeanO | last post: by
2 posts views Thread by ghjk | last post: by
7 posts views Thread by Mike | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.