472,145 Members | 1,451 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Number of days between Date1 and Date2 ?

Tux

How can I calculate number of days between two dates?

Jul 17 '05 #1
5 32155
Tux wrote:
How can I calculate number of days between two dates?


Call the getTime() method on both dates. getTime() returns the number of
milliseconds since January 1, 1970, as a long. Subtract both numbers,
then divide the result by 86400000 (the number of milliseconds in one
day; 24 x 60 x 60 x 1000) and voilà. Perhaps the result needs some
rounding but I leave that up to your implementation.

Regards,

Joost
Jul 17 '05 #2
Calendar calDueDate = new GregorianCalendar();
calDueDate.setTime(getDueDate());
int dueDateYear = calDueDate.get(Calendar.YEAR);
int dueDateDayofYear = calDueDate.get(Calendar.DAY_OF_YEAR);
Calendar calToday = new GregorianCalendar();
int todayYear = calToday.get(Calendar.YEAR);
int todayDayofYear = calToday.get(Calendar.DAY_OF_YEAR);
int nDaysElapse = todayDayofYear - dueDateDayofYear;
int nYearsElapse = todayYear - dueDateYear;
int nChargeableDays = (nYearsElapse * 365) + nDaysElapse;
return nChargeableDays;
Maybe there is a better way, but that is what I used.
Jul 17 '05 #3
Hello, mr********@rogers.com!
You wrote on Fri, 26 Mar 2004 01:49:49 GMT:

m> Calendar calDueDate = new GregorianCalendar();
m> calDueDate.setTime(getDueDate());
m> int dueDateYear = calDueDate.get(Calendar.YEAR);
m> int dueDateDayofYear = calDueDate.get(Calendar.DAY_OF_YEAR);
m> Calendar calToday = new GregorianCalendar();
m> int todayYear = calToday.get(Calendar.YEAR);
m> int todayDayofYear = calToday.get(Calendar.DAY_OF_YEAR);
m> int nDaysElapse = todayDayofYear - dueDateDayofYear;
m> int nYearsElapse = todayYear - dueDateYear;
m> int nChargeableDays = (nYearsElapse * 365) + nDaysElapse;
m> return nChargeableDays;
m> Maybe there is a better way, but that is what I used.

With best regards, Kozynenko Ganna. E-mail: gk**@isd.dp.ua
Jul 17 '05 #4
Are you guaranteed that each year elapsed has 365 days? What abt leap years?

Hello, mr********@rogers.com!
You wrote on Fri, 26 Mar 2004 01:49:49 GMT:

m> Calendar calDueDate = new GregorianCalendar();
m> calDueDate.setTime(getDueDate());
m> int dueDateYear = calDueDate.get(Calendar.YEAR);
m> int dueDateDayofYear = calDueDate.get(Calendar.DAY_OF_YEAR);
m> Calendar calToday = new GregorianCalendar();
m> int todayYear = calToday.get(Calendar.YEAR);
m> int todayDayofYear = calToday.get(Calendar.DAY_OF_YEAR);
m> int nDaysElapse = todayDayofYear - dueDateDayofYear;
m> int nYearsElapse = todayYear - dueDateYear;
m> int nChargeableDays = (nYearsElapse * 365) + nDaysElapse;
m> return nChargeableDays;
m> Maybe there is a better way, but that is what I used.

With best regards, Kozynenko Ganna. E-mail: gk**@isd.dp.ua
Jul 17 '05 #5

"Tux" <ma*********@inet.hr> wrote in message
news:c3**********@sunce.iskon.hr...

How can I calculate number of days between two dates?


long days = (date.getTime() - date2.getTime()) / (1000 * 60 * 60 * 24);
// next time, don't be so lazy, and use your own brain.

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by MMFBprez | last post: by
3 posts views Thread by steve | last post: by
6 posts views Thread by W. eWatson | last post: by
9 posts views Thread by sha2484 | last post: by

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.