Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java Language
Expertise: Beginner
Apr 7, 2000

RE: Date Arithmatic in Java

Question:
I previously asked a question that may not have been worded as clearly as possible and you answered something slightly different from what I was looking for. I want to know how to subtract one date from another as in the following pseudo-code:
calender1.setTime(databasedate)
calender2.setTime(todaysdate)

differenceindays = calender2 - calender1
How can I determine the difference in days between two Calendar dates?

Answer:
To do this you must convert your Calendar times to Date objects with Calendar.getTime(). Then convert your Date objects to longs representing the number of milliseconds since midnight January 1, 1970. Date.getTime() will do this. Now subtract the two times and divide by the number of milliseconds in a day.

DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date