Here you can find the source of differenceBetween(Date initDate, Date endDate, TimeUnit units)
public static long differenceBetween(Date initDate, Date endDate, TimeUnit units)
//package com.java2s; //License from project: Open Source License import java.util.Calendar; import java.util.Date; import java.util.concurrent.TimeUnit; public class Main { public static long differenceBetween(Date initDate, Date endDate, TimeUnit units) { return differenceBetween(initDate, endDate, units, true); }/*from www . java 2 s . c o m*/ public static long differenceBetween(Date initDate, Date endDate, TimeUnit units, Boolean includeEndDate) { return getDifference(toCalendar(initDate), toCalendar(endDate), units) + (includeEndDate ? 1 : 0); } public static long getDifference(Calendar initDate, Calendar endDate, TimeUnit units) { return units.convert(endDate.getTimeInMillis() - initDate.getTimeInMillis(), TimeUnit.MILLISECONDS); } public static Calendar toCalendar(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); return calendar; } }