Here you can find the source of getMonths(Date date1, Date date2)
public static int getMonths(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 getMonths(Date date1, Date date2) { int iMonth = 0; try {/* w ww . j a v a 2 s. c o m*/ Calendar objCalendarDate1 = Calendar.getInstance(); objCalendarDate1.setTime(date1); Calendar objCalendarDate2 = Calendar.getInstance(); objCalendarDate2.setTime(date2); if (objCalendarDate2.equals(objCalendarDate1)) return 0; if (objCalendarDate1.after(objCalendarDate2)) { Calendar temp = objCalendarDate1; objCalendarDate1 = objCalendarDate2; objCalendarDate2 = temp; } if (objCalendarDate2.get(Calendar.YEAR) > objCalendarDate1.get(Calendar.YEAR)) iMonth = (objCalendarDate2.get(Calendar.YEAR) - objCalendarDate1.get(Calendar.YEAR)) * 12 + objCalendarDate2.get(Calendar.MONTH) - objCalendarDate1.get(Calendar.MONTH); else iMonth = objCalendarDate2.get(Calendar.MONTH) - objCalendarDate1.get(Calendar.MONTH); } catch (Exception e) { e.printStackTrace(); } return iMonth; } }