This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Calculation of months between two dates Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Calculation of months between two dates" Watch "Calculation of months between two dates" New topic
Author

Calculation of months between two dates

Vijay Chandran
Ranch Hand

Joined: Jan 07, 2007
Posts: 175
Dear friends,

I have the following two date formats:

Date1: Sun Nov 04 00:00:00 IST 2007
Date2: Tue Jan 01 00:00:00 IST 2008


How to calculate the difference between these two dates in months?
Any special formatting needed using SimpleDateFormat?

I don't know how to use the Calendar class with the above date format.

Kindly provide your help

Regards,
Vijay
Prashant Ladha
Greenhorn

Joined: Feb 06, 2008
Posts: 8
You dint mention what type of object these dates are stored in.

But anyways, I could think of this formula which would return you the number of months between two dates.

((YYYY*12)+MM) - ((YYYY*12)+MM)

Do let me know if it works.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35443
    
    9
That depends on how you define the number of months between two dates. In the example you provide 1, 2 and 3 are all valid answers, according to different definitions. Which one do you use?

The SimpleDateFormat class helps with converting dates into string and vice versa; it does not perform any date calculations.


Android appsImageJ pluginsJava web charts
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Calculation of months between two dates
 
Similar Threads
Precise age calculation..
Date comparison
how do I find date difference in months
about dates
Extracting months from Date object data