Here you can find the source of getDistanceMonthOfTwoDate(Date before, Date after)
public static int getDistanceMonthOfTwoDate(Date before, Date after)
//package com.java2s; import java.util.Calendar; import java.util.Date; public class Main { public static int getDistanceMonthOfTwoDate(Date before, Date after) { if (before.compareTo(after) >= 0) { return 0; }//from w ww.j av a 2 s . co m Calendar calendar1 = Calendar.getInstance(); Calendar calendar2 = Calendar.getInstance(); calendar1.setTime(before); calendar2.setTime(after); int distance = (calendar2.get(Calendar.YEAR) - calendar1.get(Calendar.YEAR)) * 12 + calendar2.get(Calendar.MONTH) - calendar1.get(Calendar.MONTH); if (calendar2.get(Calendar.DAY_OF_MONTH) < calendar1.get(Calendar.DAY_OF_MONTH)) { distance--; } return distance; } }