Here you can find the source of hoursBetween(Calendar start, Calendar end, boolean assumeSameDate)
public static int hoursBetween(Calendar start, Calendar end, boolean assumeSameDate)
//package com.java2s; /******************************************************************************* * Copyright (c) Emil Crumhorn - Hexapixel.com - emil.crumhorn@gmail.com * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors:/*w w w .j av a 2 s.c om*/ * emil.crumhorn@gmail.com - initial API and implementation *******************************************************************************/ import java.util.Calendar; import java.util.Date; import java.util.Locale; public class Main { private static Locale _locale; public static int hoursBetween(Calendar start, Calendar end, boolean assumeSameDate) { return minutesBetween(start.getTime(), end.getTime(), assumeSameDate, false) / 60; } public static int hoursBetween(Date start, Date end, boolean assumeSameDate) { return minutesBetween(start, end, assumeSameDate, false) / 60; } public static int minutesBetween(Date start, Date end, boolean assumeSameDate, boolean assumeSameHour) { Calendar sDate = Calendar.getInstance(_locale); Calendar eDate = Calendar.getInstance(_locale); sDate.setTime(start); eDate.setTime(end); if (assumeSameDate) { // set same date eDate.set(Calendar.YEAR, 2000); sDate.set(Calendar.YEAR, 2000); eDate.set(Calendar.DAY_OF_YEAR, 1); sDate.set(Calendar.DAY_OF_YEAR, 1); } if (assumeSameHour) { eDate.set(Calendar.HOUR, 1); sDate.set(Calendar.HOUR, 1); } long diff = eDate.getTimeInMillis() - sDate.getTimeInMillis(); diff /= 1000; diff /= 60; return (int) diff; } }