By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,991 Members | 1,888 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,991 IT Pros & Developers. It's quick & easy.

Number of days between Date1 and Date2 ?

P: n/a
Tux

How can I calculate number of days between two dates?

Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

"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.