Here you can find the source of hourDiff(Date firstDate, Date lastDate)
public static String hourDiff(Date firstDate, Date lastDate)
//package com.java2s; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.TimeZone; public class Main { public static String hourDiff(Date firstDate, Date lastDate) { Calendar cal1 = new GregorianCalendar(TimeZone.getDefault()); Calendar cal2 = new GregorianCalendar(TimeZone.getDefault()); cal1.setTime(firstDate);//from w w w. jav a 2 s. c o m cal2.setTime(lastDate); int d1 = cal1.get(Calendar.DAY_OF_YEAR); int d2 = cal2.get(Calendar.DAY_OF_YEAR); int h1 = cal1.get(Calendar.HOUR_OF_DAY); int h2 = cal2.get(Calendar.HOUR_OF_DAY); int m1 = cal1.get(Calendar.MINUTE); int m2 = cal2.get(Calendar.MINUTE); int diff = d2 * 24 * 60 + h2 * 60 + m2 - d1 * 24 * 60 - h1 * 60 - m1; String min = String.valueOf(diff % 60); return (diff / 60) + ":" + lpad(min, '0', 2); } public static String lpad(String str, char pad, int len) { while (str.length() < len) { str = pad + "" + str; } return str; } }