473,372 Members | 901 Online

# Number of days between Date1 and Date2 ?

How can I calculate number of days between two dates?

Jul 17 '05 #1
5 32224
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 thread has been closed and replies have been disabled. Please start a new discussion.

### Similar topics

 8 by: dlx_son | last post by: Here is the code so far

Enter time to add to or subtract from:

(If not entered, current time will be used)
Day: