473,385 Members | 1,867 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,385 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 11564
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.