Here you can find the source of getBetweenMonths(Date date1, Date date2)
public static int getBetweenMonths(Date date1, Date date2)
//package com.java2s; //License from project: Apache License import java.util.Calendar; import java.util.Date; public class Main { public static int getBetweenMonths(Date date1, Date date2) { int iMonth = 0; int flag = 0; 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;//www . j a v a 2s . c o m } if (objCalendarDate2.get(Calendar.DAY_OF_MONTH) < objCalendarDate1.get(Calendar.DAY_OF_MONTH)) flag = 1; if (objCalendarDate2.get(Calendar.YEAR) > objCalendarDate1.get(Calendar.YEAR)) iMonth = ((objCalendarDate2.get(Calendar.YEAR) - objCalendarDate1.get(Calendar.YEAR)) * 12 + objCalendarDate2.get(Calendar.MONTH) - flag) - objCalendarDate1.get(Calendar.MONTH); else iMonth = objCalendarDate2.get(Calendar.MONTH) - objCalendarDate1.get(Calendar.MONTH) - flag; return iMonth; } }