Here you can find the source of dateDiff(int type, Calendar fromDate, Calendar toDate, boolean future)
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 { static int dateDiff(int type, Calendar fromDate, Calendar toDate, boolean future) { 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++;//from ww w. j av a 2 s . c om } diff--; fromDate.setTimeInMillis(savedDate); return diff; } }