473,372 Members | 901 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.

Number of days between Date1 and Date2 ?

Tux

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 <form name="thisform"> <h3>Enter time to add to or subtract from:</h3> (If not entered, current time will be used)<br> Day: <input name="d1" alt="Day of month"...
3
by: MMFBprez | last post by:
I am trying to compute storage charges by getting the number of months between dates and multiplying it by a rate. I cannot get a correct number of months if the date is greater than a year ago. ...
6
by: carl.barrett | last post by:
Hi, I have a continuous form based on a query ( I will also be creating a report based on the same query). There are 2 fields: Date Obtained and Date Of Expiry I want a further 3 columns...
3
by: steve | last post by:
Hello , does anyone know of a method/way to get the number of days between two dates? TIA
2
by: Scott | last post by:
Hi I have done a program that is to work out travel insurance for single and annual trips. For single trips I put in a start and end date no problems. But for an annual trip I only need to...
2
by: David | last post by:
Hi, I have an order processing system in which I have an OrderDate and a Completion Date I want to print the difference in Weeks and Days between the two dates, i.e. if the completion date is...
6
by: W. eWatson | last post by:
That's the question in Subject. For example, the difference between 08/29/2008 and 09/03/2008 is +5. The difference between 02/28/2008 and 03/03/2008 is 4, leap year--extra day in Feb. I'm really...
9
by: sha2484 | last post by:
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...
1
by: AXRabbit | last post by:
Hi guys, I am currently doing a Car rental website project. Like my Car collection and return date is like in the website. http://www.avis.com/car-rental/avisHome/home.ac Well i need a function or...
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
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: 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: 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: 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
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
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...

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.