aspose file tools*
The moose likes Java in General and the fly likes how do I find date difference in months Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "how do I find date difference in months" Watch "how do I find date difference in months" New topic
Author

how do I find date difference in months

prince davies
Ranch Hand

Joined: May 08, 2009
Posts: 74
I have date from Oracle database and Today's date from Java.

How do I find the months difference between these two dates?

Thanks,
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19790
    
  20

Using DateDiff. Oh wait, that VB

Check out java.util.Calendar. The difference in months is the difference in months for the current year plus the difference in years times 12. So for October 2009 and May 2010, the difference in months is -5 (May - October), the difference in years is 1 (2010 - 2009), so the total difference is -5 + 12 * 1 == 7.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Bimal Sahay
Greenhorn

Joined: Aug 14, 2009
Posts: 4
Since you are already fetching date from oracle, and it also provides today's date(SYSDATE), why don't you do it directly in oracle SQL. Just a suggestion Not that I don't love Java but I see this a an easy task in SQL.


Bimal
prince davies
Ranch Hand

Joined: May 08, 2009
Posts: 74
Bimal Sahay wrote:Since you are already fetching date from oracle, and it also provides today's date(SYSDATE), why don't you do it directly in oracle SQL. Just a suggestion Not that I don't love Java but I see this a an easy task in SQL.


yeah I tried using months_between function.But I need to do somethingelse after retirieving it.
can you send me the code for it?

months between sql date from database and java.util.Date(todays date = new Date())
prince davies
Ranch Hand

Joined: May 08, 2009
Posts: 74
Difference in months
java.sql.DATE from database and java.util.DATE(Todays DATE)?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19790
    
  20

java.sql.Date extends java.util.Date. You can use both in a Calendar:
prince davies
Ranch Hand

Joined: May 08, 2009
Posts: 74
but how do I find date difference in years?
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
prince davies wrote:but how do I find date difference in years?


I think that by using the information in the replies above you should be able to use the techniques for finding months and apply it to years. If not, please clarify exactly what you've tried and how it is or isn't working.

Best of luck!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19790
    
  20

I repeat:
Rob Prime wrote:Check out java.util.Calendar. [snip] the difference in years is 1 (2010 - 2009)

Now you can't tell me you can't find anything in that class to get the year of a Calendar object?
prince davies
Ranch Hand

Joined: May 08, 2009
Posts: 74
I went through some of the examples,but I found dicrepencies in months difference






The above METHOD will return result as follows monthsdIFFERENCE = 7

Date from Database sqldate :2009-10-11
todayDate :Sat May 15 14:29:02 PDT 2010
currentMonth :4
currentYear :2010
lastMonth :9
lastYear :2009
Hello currentMonth - databaseFetchedMonth) :-5
Hello (currentYear - databaseFetchedYear) :1
Hello cal.getMaximum(Calendar.MONTH) :12
Hello monthsDiff :7


But when I calculate with othe EXAMPLE,it gives me 8 MONTHS difference.

Its conflicting results.




Date from Database sqldate :2009-10-11
todayDate :Sat May 15 14:34:40 PDT 2010
currentMonth :4
currentYear :2010
lastMonth :9
lastYear :2009
Hello currentMonth - databaseFetchedMonth) :-5
Hello (currentYear - databaseFetchedYear) :1
Hello cal.getMaximum(Calendar.MONTH) :12
noofmonths :8
Hello monthsDiff :8

Can anybody help me to fine tune these methods or suggest better solution for finding the difference in moths?

Thanks
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14435
    
  23

Please UseCodeTags when you post source code.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19790
    
  20

prince davies wrote:

The two don't return the same, do they? The first returns what I suggested. The second does take the difference in months for the years, then you add something. Applying a bit of math to remove these brackets, I get this:
That "+1" is causing the difference. Where does this come from?
prince davies
Ranch Hand

Joined: May 08, 2009
Posts: 74
how to get number of months between two given dates in java

Jim Yingst
wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18587

posted Saturday, December 16, 2006 1:27 PM
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:
view plaincopy to clipboardprint?
private final static double AVERAGE_MILLIS_PER_MONTH = 365.24 * 24 * 60 * 60 * 1000 / 12;

public static double monthsBetween(Date d1, Date d2) {
return (d2.getTime() - d1.getTime()) / AVERAGE_MILLIS_PER_MONTH;
}
private final static double AVERAGE_MILLIS_PER_MONTH = 365.24 * 24 * 60 * 60 * 1000 / 12;

public static double monthsBetween(Date d1, Date d2) {
return (d2.getTime() - d1.getTime()) / AVERAGE_MILLIS_PER_MONTH;
}
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

posted Sunday, December 17, 2006 8:56 PM
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: 90

posted Monday, December 18, 2006 5:19 AM
If you want the number of month boundaries crossed between dates you can use something like this:
view plaincopy to clipboardprint?

public static int monthsBetween(Date minuend, Date subtrahend)
{
Calendar cal = Calendar.getInstance();
// default will be Gregorian in US Locales
cal.setTime(minuend);
int minuendMonth = cal.get(Calendar.MONTH);
int minuendYear = cal.get(Calendar.YEAR);
cal.setTime(subtrahend);
int subtrahendMonth = cal.get(Calendar.MONTH);
int subtrahendYear = cal.get(Calendar.YEAR);

// the following will work okay for Gregorian but will not
// work correctly in a Calendar where the number of months
// in a year is not constant
return ((minuendYear - subtrahendYear) * cal.getMaximum(Calendar.MONTH)) +
(minuendMonth - subtrahendMonth);
}

public static int monthsBetween(Date minuend, Date subtrahend)
{
Calendar cal = Calendar.getInstance();
// default will be Gregorian in US Locales
cal.setTime(minuend);
int minuendMonth = cal.get(Calendar.MONTH);
int minuendYear = cal.get(Calendar.YEAR);
cal.setTime(subtrahend);
int subtrahendMonth = cal.get(Calendar.MONTH);
int subtrahendYear = cal.get(Calendar.YEAR);

// the following will work okay for Gregorian but will not
// work correctly in a Calendar where the number of months
// in a year is not constant
return ((minuendYear - subtrahendYear) * cal.getMaximum(Calendar.MONTH)) +
(minuendMonth - subtrahendMonth);
}


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

posted Thursday, April 29, 2010 6:55:31 AM
view plaincopy to clipboardprint?


// tyear --->Year of the to date; fyear--->Year of the from date; fmonth-->Month of the from date; tmonth-->Month of the todate;
int noofmonths=(tyear-fyear)*12;
noofmonths=noofmonths+((12-(fmonth-1)+(tmonth-12)));




// tyear --->Year of the to date; fyear--->Year of the from date; fmonth-->Month of the from date; tmonth-->Month of the todate;
int noofmonths=(tyear-fyear)*12;
noofmonths=noofmonths+((12-(fmonth-1)+(tmonth-12)));



Thanks & Regards
Nazeer




:
Nazeer says
int noofmonths=(tyear-fyear)*12;
noofmonths=noofmonths+((12-(fmonth-1)+(tmonth-12)));




Chris Beckey , Ranch Hand says
return ((minuendYear - subtrahendYear) * cal.getMaximum(Calendar.MONTH)) +
(minuendMonth - subtrahendMonth);
prince davies
Ranch Hand

Joined: May 08, 2009
Posts: 74
Rob Prime Suggests to go ahead with the following code.is that right?





Question 1)But why we need to add 1 for this piece of code ?
cal.getMaximum(Calendar.MONTH)+1)

cal.getMaximum(Calendar.MONTH) always return 11 instead of 12.why?


Question 2) Rob suggests to go ahead with above mentioned code for difference in months.
Thanks
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19790
    
  20

prince davies wrote:Question 1)But why we need to add 1 for this piece of code ?
cal.getMaximum(Calendar.MONTH)+1)

cal.getMaximum(Calendar.MONTH) always return 11 instead of 12.why?

The month returns is one of the constants Calendar.JANUARY through Calendar.DECEMBER. For some reason, Calendar.JANUARY is 0 and Calendar.DECEMBER is 11 instead of 1 and 12. cal.getMaximum(Calendar.MONTH) does not return 11 - it returns Calendar.DECEMBER. That happens to have the value 11.
What I usually do to make months 1-based is subtract Calendar.JANUARY, making it 100% guaranteed 0-based, then add 1. Of course, since Calendar.JANUARY is 0 you can omit that, but my code is safer in case it would ever change.
prince davies
Ranch Hand

Joined: May 08, 2009
Posts: 74
Hi Rob, Is this your solution?

return (currentMonth - databaseFetchedMonth)+((currentYear - databaseFetchedYear) * (cal.getMaximum(Calendar.MONTH)+1));


What would you suggest for the above piece of code?

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how do I find date difference in months