33

I need to get difference between two dates using Java. I need my result to be in months.

Example:

Startdate = 2013-04-03 enddate = 2013-05-03 Result should be 1

if the interval is

Startdate = 2013-04-03 enddate = 2014-04-03 Result should be 12

Using the following code I can get the results in days. How can I get in months?

Date startDate = new Date(2013,2,2);
Date endDate = new Date(2013,3,2);
int difInDays = (int) ((endDate.getTime() - startDate.getTime())/(1000*60*60*24));
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
ashu
  • 1,339
  • 7
  • 27
  • 43

3 Answers3

111

If you can't use JodaTime, you can do the following:

Calendar startCalendar = new GregorianCalendar();
startCalendar.setTime(startDate);
Calendar endCalendar = new GregorianCalendar();
endCalendar.setTime(endDate);

int diffYear = endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR);
int diffMonth = diffYear * 12 + endCalendar.get(Calendar.MONTH) - startCalendar.get(Calendar.MONTH);

Note that if your dates are 2013-01-31 and 2013-02-01, you get a distance of 1 month this way, which may or may not be what you want.

Étienne Miret
  • 6,448
  • 5
  • 24
  • 36
  • 2
    +1 This will work for every year difference. (i.e.) `Startdate = 2014-04-03 enddate = 2013-04-03 Result should be 12` as OP said. **UPDATE** Yes, he will get a 1 month difference even if it is only 1 day difference, but OP specified that he wants it in months. 1 month its not a real difference between these dates, but 0 it's not even more real difference. For that, if you want 0 months you can do something like `Math.floor`, or something like that. – DaGLiMiOuX May 15 '13 at 07:25
  • @Etienne Miret : I am getting following error. The method startCalendar(int) is undefined for the type – ashu May 15 '13 at 07:32
  • 1
    @ashu `startCalendar(int)` is not a method. You have an error in your syntax, maybe. You have to do `startCalendar.setTime(int)`. In this case, `int` it's `startDate.getTime()`. So you will need to have in your code this `startCalendar.setTime(startDate.getTime())` – DaGLiMiOuX May 15 '13 at 07:33
  • Ya but write the code exactly what given above but still am getting error like this – ashu May 15 '13 at 07:36
  • 1
    Sorry i did a sysntax mistake there – ashu May 15 '13 at 07:37
  • Now i got the result .Thank you – ashu May 15 '13 at 07:40
  • @ashu Indeed, I made a typo in my answer, which is now corrected. – Étienne Miret May 15 '13 at 07:43
  • @DaGLiMiOuX the [Calendar.setTime()](http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#setTime(java.util.Date)) method takes a `Date` argument, not an `int`. – Étienne Miret May 15 '13 at 07:49
  • @EtienneMiret I meant `long` not `int`. My bad :) – DaGLiMiOuX May 15 '13 at 07:51
  • There is one issue on this code not mentioned and that being if the startDate is after the endDate. Then it will not be correct. Or you get negative numbers. – bytor99999 Dec 12 '16 at 02:27
4

You can use Joda time library for Java. It would be much easier to calculate time-diff between dates with it.

Sample snippet for time-diff:

Days d = Days.daysBetween(startDate, endDate);
int days = d.getDays();
MadTech
  • 1,458
  • 3
  • 13
  • 32
-6

You can try this:

Calendar sDate = Calendar.getInstance();
Calendar eDate = Calendar.getInstance();
sDate.setTime(startDate.getTime());
eDate.setTime(endDate.getTime());
int difInMonths = sDate.get(Calendar.MONTH) - eDate.get(Calendar.MONTH);

I think this should work. I used something similar for my project and it worked for what I needed (year diff). You get a Calendar from a Date and just get the month's diff.

DaGLiMiOuX
  • 889
  • 9
  • 28