File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes how to get number of months  between two given dates in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to get number of months  between two given dates in java" Watch "how to get number of months  between two given dates in java" New topic
Author

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

kiran kumar
Greenhorn

Joined: Feb 07, 2005
Posts: 29
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.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
Jaime M. Tovar
Ranch Hand

Joined: Mar 28, 2005
Posts: 133
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.


She will remember your heart when men are fairy tales in books written by rabbits.<br /> As long as there is duct tape... there is also hope.
Chris Beckey
Ranch Hand

Joined: Jun 09, 2006
Posts: 116

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.
Mohamed Nazeer
Greenhorn

Joined: Apr 29, 2010
Posts: 3


Thanks & Regards
Nazeer
Mohamed Nazeer
Greenhorn

Joined: Apr 29, 2010
Posts: 3


thank and regards
Nazeer
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
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

Joined: Apr 29, 2010
Posts: 3
Thanks ritchie from now i will post like that. Sorry if anything wrong in my post.

thanks and regards
Nazeer
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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...


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
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:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to get number of months between two given dates in java
 
Similar Threads
How to calculate difference between two dates in months
Date
Getting months from date
how do I find date difference in months
calculate difference in months