Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Date time difference in months

Posted on 2006-10-24
Medium Priority
232 Views
If i have two dates as Calendar instances:  2006-23-02 00:00:00 and 2002-12-09 00:00:00 then i need the number of months between the two dates. how can i do this?

Note: the dates never have times.

// currently this is what i have but it's not completely accurate because i assume that each month is 30 days
private int getNumMonths(Calendar mostrecent, Calendar old, boolean cleartime) {
if(cleartime) {
mostrecent.set(Calendar.MILLISECOND, 0);
mostrecent.set(Calendar.SECOND, 0);
mostrecent.set(Calendar.MINUTE, 0);
mostrecent.set(Calendar.HOUR_OF_DAY, 0);

old.set(Calendar.MILLISECOND, 0);
old.set(Calendar.SECOND, 0);
old.set(Calendar.MINUTE, 0);
old.set(Calendar.HOUR_OF_DAY, 0);
}

// difference between the 2 dates in millis
long difference = mostrecent.getTimeInMillis() - old.getTimeInMillis();

// set the calendar object representing the difference
Calendar diff = Calendar.getInstance();
diff.setTimeInMillis(difference);

long diffMonths = (difference/(24*60*60*1000))/30;
return(StringTools.strToInt("" + diffMonths));
}
0
Question by:tbboyett
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2
• 2

LVL 24

Expert Comment

ID: 17798439
>>>    long difference = mostrecent.getTimeInMillis() - old.getTimeInMillis();

return(difference/(24*60*60*1000*30);// returns a long, rounded down.
or:

return(difference/(24.0*60*60*1000*30);// returns a double with a fraction.

Don't forget to type your method long or double.
;JOOP!
0

LVL 5

Author Comment

ID: 17798516
thanks for the response.

>> return(difference/(24*60*60*1000*30);
this returns a (negative) number that is way off from the number of months between the two dates
0

LVL 9

Assisted Solution

shinobun earned 400 total points
ID: 17798922
Take the difference of (year * 12 + month).
0

LVL 24

Accepted Solution

sciuriware earned 1200 total points
ID: 17801208
Of course negative if you don't control the sequence of the dates.
Then use:

return(Math.abs(difference/(24*60*60*1000*30));// returns a long, rounded down.
or:

return(Math.abs(difference/(24.0*60*60*1000*30));// returns a double with a fraction.

;JOOP!
0

LVL 5

Author Comment

ID: 17803280
Thanks guys for your help, but i've found a solution that works for what i need.

here it is incase anyone needs it:

private int getNumMonthBetweenTwoDates(Calendar mostrecent, Calendar old, boolean cleartime) {
boolean done = false;
int numMonths = 0;

if(cleartime) {
mostrecent.set(Calendar.MILLISECOND, 0);
mostrecent.set(Calendar.SECOND, 0);
mostrecent.set(Calendar.MINUTE, 0);
mostrecent.set(Calendar.HOUR_OF_DAY, 0);

old.set(Calendar.MILLISECOND, 0);
old.set(Calendar.SECOND, 0);
old.set(Calendar.MINUTE, 0);
old.set(Calendar.HOUR_OF_DAY, 0);
}

while(!done) {
if((mostrecent.get(Calendar.MONTH) == old.get(Calendar.MONTH)) &&
(mostrecent.get(Calendar.YEAR) == old.get(Calendar.YEAR))) {
// found the old date month

numMonths ++; // add an extra month to round up
done = true;
}
numMonths ++;
}

return (numMonths);
}
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.
###### Suggested Courses
Course of the Month4 days, 21 hours left to enroll