Java Hour getHours(Date t, Date baseDate)

Here you can find the source of getHours(Date t, Date baseDate)

Description

Get hours difference between two dates

License

Open Source License

Parameter

Parameter Description
t The time
baseDate Base date

Return

The time delta value

Declaration

public static int getHours(Date t, Date baseDate) 

Method Source Code

//package com.java2s;
/* Copyright 2012 Yaqiang Wang,
* yaqiang.wang@gmail.com/*from w w w . ja  v  a2 s. co  m*/
* 
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or (at
* your option) any later version.
* 
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
* General Public License for more details.
*/

import java.util.Calendar;
import java.util.Date;

public class Main {
    /**
     * Get hours difference between two dates
     *
     * @param t The time
     * @param baseDate Base date
     * @return The time delta value
     */
    public static int getHours(Date t, Date baseDate) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(baseDate);
        long sl = cal.getTimeInMillis();
        long el, delta;
        cal.setTime(t);
        el = cal.getTimeInMillis();
        delta = el - sl;
        int value = (int) (delta / (60 * 60 * 1000));

        return value;
    }
}

Related

  1. getHourOfDay(final Date date)
  2. getHourOfDayInAm(Date dt)
  3. getHourOfWeek(Date dateIn)
  4. getHours()
  5. getHours()
  6. getHours(final Date date)
  7. getHours(java.util.Date _date)
  8. getHoursByDate(Date date, int hours)
  9. getHoursOfTowDiffDate(String p_startDate, String p_endDate)