• 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

how to get number of months between two given dates in java

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi ,
how to get months difference between two given dates in java by using Date or Gregorian Calendar.
(like months_between(date1,date2) function in oracle.)

please give me reply.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I don't think there's any well-defined standard for what that would mean. The length of a month varies, after all. How many months are between Jan 31 and March 1? How many between March 1 and March 31? There are several ways you could answer those questions, and it's not clear that one answer is "right".

If you want a simple appoximation, it's easy:

Otherwise you probably need to define precisely what a month is. Good luck.
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If i remember, i used two calendars, one in the original date and one in the reference date. Then added while the original calendar was less (Date.before(Date)) than the reference date, have an integer variable holding how many cicles you have used. If not, add a month. Using the Calendar.add(). Hopes it helps.
 
Ranch Hand
Posts: 116
Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want the number of month boundaries crossed between dates you can use something like this:


BUT ... as was suggested earlier you may want to think about what "X months ago" really means.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Thanks & Regards
Nazeer
 
Mohamed Nazeer
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


thank and regards
Nazeer
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Why are you using Vector as a List implementation? That is regarded as legacy code. All these substrings look very awkward.
And please fins the Code button; I have edited your post so you can see how much better it looks.
 
Mohamed Nazeer
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks ritchie from now i will post like that. Sorry if anything wrong in my post.

thanks and regards
Nazeer
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

Please note that you're answering a post from December 2006, three and a half years ago. The original poster is most likely not still waiting for an answer...
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mohamed Nazeer wrote:. . . Sorry if anything wrong in my post.

There is now You have deleted all your code, so nobody understands the rest of the thread. Again:
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic