• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Calculation of months between two dates

 
Ranch Hand
Posts: 186
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 43011
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
You firghten me terribly. I would like to go home now. Here, take this tiny ad:
Enterprise-grade Excel API for Java
https://products.aspose.com/cells/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!