File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Difference beteween two dates Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Difference beteween two dates" Watch "Difference beteween two dates" New topic
Author

Difference beteween two dates

Gaurav Chhabras
Ranch Hand

Joined: Sep 21, 2005
Posts: 126
Hi

I have to take out the difference betewen two dates like 1980/10/10 and 2000/05/05.

Could you please tell me that how will i will be able to calculate difference in Months.

Thanks
Regards
Gaurav
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35241
    
    7
The java.text.SimpleDateformat.parse method will convert those strings into Date objects. Then you can use Date.getTime to obtain a millisecond value of both dates. If you then subtract those from each other, and properly divide the result, you will get the number of months in between.


Android appsImageJ pluginsJava web charts
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16483
    
    2

What's the answer for your example? 235? Or 234? Both could be acceptable depending on how you define the problem. So the first thing would be to specify what the question means.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Months are not a uniform size, so they are my cutoff in time units. By that I mean hours, minutes, seconds, days, weeks are all meaningful but months are confusing.

For example, what's January 31 + 1 month? What's January 31 + 1 month - 1 month?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Matthew Taylor
Rancher

Joined: Jun 13, 2004
Posts: 110
I always translate my GregorianCalendar dates into milliseconds before I do any date calculations. Then you can just subtract to get the difference and do some math to calculate the months.


Grails Consultant
http://dangertree.net
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Difference beteween two dates
 
Similar Threads
BIG PROBLEM ON DATE
How to calculate difference between two dates in months
DateDiff Function in Java?
Oracle Date Difference
difference between two dates