Here you can find the source of calculateHourDiff(Calendar cal1, Calendar cal2)
private static int calculateHourDiff(Calendar cal1, Calendar cal2)
//package com.java2s; import java.util.Calendar; public class Main { public static final int SECOND = 1; public static final int MINUTE = 2; private static final long MILLISECOND_IN_HOUR = 1000 * 60 * 60; private static int calculateHourDiff(Calendar cal1, Calendar cal2) { int mil2 = cal2.get(Calendar.MILLISECOND); int mil1 = cal1.get(Calendar.MILLISECOND); int sec2 = cal2.get(Calendar.SECOND); int sec1 = cal1.get(Calendar.SECOND); int min2 = cal2.get(Calendar.MINUTE); int min1 = cal1.get(Calendar.MINUTE); cal2.set(Calendar.MINUTE, 0); cal1.set(Calendar.MINUTE, 0); cal2.set(Calendar.SECOND, 0); cal1.set(Calendar.SECOND, 0); cal2.set(Calendar.MILLISECOND, 0); cal1.set(Calendar.MILLISECOND, 0); int result = (int) ((cal2.getTimeInMillis() - cal1.getTimeInMillis()) / MILLISECOND_IN_HOUR); cal2.set(Calendar.MINUTE, min2); cal1.set(Calendar.MINUTE, min1); cal2.set(Calendar.SECOND, sec2); cal1.set(Calendar.SECOND, sec1); cal2.set(Calendar.MILLISECOND, mil2); cal1.set(Calendar.MILLISECOND, mil1); return result; }//w ww . ja v a 2 s. c o m }