Here you can find the source of getDiffMonths(Date date1, Date date2)
public static int getDiffMonths(Date date1, Date date2)
//package com.java2s; //License from project: Open Source License import java.util.Calendar; import java.util.Date; public class Main { public static int getDiffMonths(Date date1, Date date2) { Calendar c1 = Calendar.getInstance(); Calendar c2 = Calendar.getInstance(); c1.setTime(date1);//from w w w. j av a 2 s.c om if (date2 == null) c2.setTime(new java.util.Date()); else c2.setTime(date2); int year1 = c1.get(Calendar.YEAR); int year2 = c2.get(Calendar.YEAR); int month1 = c1.get(Calendar.MONTH); int month2 = c2.get(Calendar.MONTH); return (year2 - year1) * 12 + month2 - month1 + 1; } }