Here you can find the source of monthDifference(final Date a, final Date b)
public static int monthDifference(final Date a, final Date b)
//package com.java2s; //License from project: Open Source License import java.util.Calendar; import java.util.Date; public class Main { public static int monthDifference(final Date a, final Date b) { final Calendar calendarA = Calendar.getInstance(); final Calendar calendarB = Calendar.getInstance(); int multiplier; if (b.getTime() - a.getTime() > 0) { multiplier = -1;// w ww. j a v a2 s . c o m calendarA.setTime(b); calendarB.setTime(a); } else { multiplier = 1; calendarA.setTime(a); calendarB.setTime(b); } int result = 0; final int years = calendarA.get(Calendar.YEAR) - calendarB.get(Calendar.YEAR); final int months = calendarA.get(Calendar.MONTH) - calendarB.get(Calendar.MONTH); final int days = calendarA.get(Calendar.DAY_OF_MONTH) - calendarB.get(Calendar.DAY_OF_MONTH); result += years * 12; result += months; if (days < 0) { result -= 1; } return result * multiplier; } }