Here you can find the source of monthDiff(Date beginDate, Date endDate)
public static int monthDiff(Date beginDate, Date endDate)
//package com.java2s; //License from project: Apache License import java.util.Calendar; import java.util.Date; public class Main { private static final int ONE_YEAR_MONTH_TOTAL = 12; public static int monthDiff(Date beginDate, Date endDate) { if (beginDate == null || endDate == null) { return 0; }//from w ww. j ava2 s .c o m Calendar beginCal = Calendar.getInstance(); beginCal.setTime(beginDate); Calendar endCal = Calendar.getInstance(); endCal.setTime(endDate); int yearDiff = endCal.get(Calendar.YEAR) - beginCal.get(Calendar.YEAR); int monthDiff = ONE_YEAR_MONTH_TOTAL * yearDiff + endCal.get(Calendar.MONTH) - beginCal.get(Calendar.MONTH); return monthDiff; } }