This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma 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 | Flagged Topics | Hot Topics | Zero Replies
Register / Login

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

Calculation of months between two dates

Vijay Chandran
Ranch Hand

Joined: Jan 07, 2007
Posts: 186

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

Prashant Ladha

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

Joined: Mar 22, 2005
Posts: 42958
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.
I agree. Here's the link:
subject: Calculation of months between two dates
jQuery in Action, 3rd edition