Java tutorial
//package com.java2s; /* * Copyright 2006-2008 The Kuali Foundation * * Licensed under the Educational Community License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.opensource.org/licenses/ecl2.php * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.sql.Timestamp; import java.util.Calendar; public class Main { /** * @param startDateTime * @param endDateTime * @return the difference in hours between the second timestamp and first */ public static double getDifferenceInHours(Timestamp startDateTime, Timestamp endDateTime) { int difference = 0; Calendar startCalendar = Calendar.getInstance(); startCalendar.setTime(startDateTime); Calendar endCalendar = Calendar.getInstance(); endCalendar.setTime(endDateTime); // First, get difference in whole days Calendar startCompare = Calendar.getInstance(); startCompare.setTime(startDateTime); startCompare.set(Calendar.HOUR_OF_DAY, 0); startCompare.set(Calendar.MINUTE, 0); Calendar endCompare = Calendar.getInstance(); endCompare.setTime(endDateTime); endCompare.set(Calendar.HOUR_OF_DAY, 0); endCompare.set(Calendar.MINUTE, 0); return (endCalendar.getTimeInMillis() - startCalendar.getTimeInMillis()) / (60.0000 * 60.0000 * 1000.0000); } }