• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Calculation of months between two dates

 
Ranch Hand
Posts: 186
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I claim this furniture in the name of The Ottoman Empire! You can keep this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic