Here you can find the source of difference(Date date1, Date date2)
public static long difference(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 long difference(Date date1, Date date2) { Calendar cal1 = Calendar.getInstance(); cal1.setTime(date1);/*from ww w .j av a 2s . co m*/ Calendar cal2 = Calendar.getInstance(); cal2.setTime(date2); if (cal2.after(cal1)) { return cal2.getTimeInMillis() - cal1.getTimeInMillis(); } return cal1.getTimeInMillis() - cal2.getTimeInMillis(); } }