Here you can find the source of getTimeDifference(Date d1, Date d2)
public static long[] getTimeDifference(Date d1, Date d2)
//package com.java2s; //License from project: Open Source License import java.util.Calendar; import java.util.Date; public class Main { public static long[] getTimeDifference(Date d1, Date d2) { long[] result = new long[5]; Calendar cal = Calendar.getInstance(); cal.setTime(d1);// w w w.ja v a2 s.co m long t1 = cal.getTimeInMillis(); cal.setTime(d2); long diff = Math.abs(cal.getTimeInMillis() - t1); final int ONE_DAY = 1000 * 60 * 60 * 24; final int ONE_HOUR = ONE_DAY / 24; final int ONE_MINUTE = ONE_HOUR / 60; final int ONE_SECOND = ONE_MINUTE / 60; long d = diff / ONE_DAY; diff %= ONE_DAY; long h = diff / ONE_HOUR; diff %= ONE_HOUR; long m = diff / ONE_MINUTE; diff %= ONE_MINUTE; long s = diff / ONE_SECOND; long ms = diff % ONE_SECOND; result[0] = d; result[1] = h; result[2] = m; result[3] = s; result[4] = ms; return result; } }