Here you can find the source of dateDiff(int type, Calendar fromDate, Calendar toDate, boolean future)
private static int dateDiff(int type, Calendar fromDate, Calendar toDate, boolean future)
//package com.java2s; //License from project: Open Source License import java.util.Calendar; public class Main { private static final int MAX_YEARS = 100000; private static int dateDiff(int type, Calendar fromDate, Calendar toDate, boolean future) { int year = Calendar.YEAR; int fromYear = fromDate.get(year); int toYear = toDate.get(year); if (Math.abs(fromYear - toYear) > MAX_YEARS) { toDate.set(year, fromYear + (future ? MAX_YEARS : -MAX_YEARS)); }/*w w w. ja va2 s . co m*/ int diff = 0; long savedDate = fromDate.getTimeInMillis(); while ((future && !fromDate.after(toDate)) || (!future && !fromDate.before(toDate))) { savedDate = fromDate.getTimeInMillis(); fromDate.add(type, future ? 1 : -1); diff++; } diff--; fromDate.setTimeInMillis(savedDate); return diff; } }