473,372 Members | 1,039 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

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

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

Similar topics

7
by: Bambero | last post by:
Hello all Problem like in subject. There is no problem when I want to count days between two dates. Problem is when I want to count years becouse of leap years. For ex. between 2002-11-19...
5
by: BlackFireNova | last post by:
I need to write a report in which one part shows a count of how many total records fall within the working days (Monday - Friday) inside of a (prompted) given date range, in a particular...
2
by: jpr | last post by:
Is there a way I can count days? I would like to place a control on my form that counts the days from the first day of each year. THanks.
6
newnewbie
by: newnewbie | last post by:
Was looking all over to find an answer to my question, but did not. I have a database that lists different users entering multiple records every day. For me, the only way to see how many days the...
2
by: DeanO | last post by:
I want to count the number of days reference a shipping order. I need to exclude weekends and holidays. Thank you.
2
by: ghjk | last post by:
How can i compare two days in php and my sql? In my web application user should register a date and the validity period(ex.5 days). I want to count days and cancel the user operations after that...
7
by: Mike | last post by:
I have a routine that's calculating business days but its not counting the weekend days that are between the start date and end date. If my start date is 9/26/08 and my end date is 10/01/08, I...
9
by: Scholar81 | last post by:
Hello, I am a novice Access developer and my boss asked me to build a database and I said yes. Now I realized the bite is way too big and I'm trying not to choke ;-) And what's worse, he does...
3
Jerry Maiapu
by: Jerry Maiapu | last post by:
Hi Scott Prince, Since this dean is not interested I need to do the same as Dean is trying to do. Since Dean has not replied would you mind to show me how to do this: You'll have to open this into...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.