Java Timestamp Field getDifferenceInHours(Timestamp startDateTime, Timestamp endDateTime)

Here you can find the source of getDifferenceInHours(Timestamp startDateTime, Timestamp endDateTime)

Description

get Difference In Hours

License

Educational Community License

Parameter

Parameter Description
startDateTime a parameter
endDateTime a parameter

Return

the difference in hours between the second timestamp and first

Declaration

public static double getDifferenceInHours(Timestamp startDateTime, Timestamp endDateTime) 

Method Source Code


//package com.java2s;
/*/*w w w  .  j a  v a  2  s.  c  om*/
 * Copyright 2005-2013 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);
    }
}

Related

  1. getDaysBetween(Timestamp start, Timestamp end)
  2. getDayStartTimestamp()
  3. getDayTime(Timestamp day, Timestamp time)
  4. getDefaultTimestamp()
  5. getDiffDay(Timestamp time1, Timestamp time2)
  6. getDiffSecond(long orgTimestamp, long desTimestamp)
  7. getDiffTime(Timestamp last)
  8. getDiffYear(java.sql.Timestamp start, java.sql.Timestamp end)
  9. getDiscrepancyNum(Timestamp time1, Timestamp time2)