• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

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'm gonna teach you a lesson! Start by looking at this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic