Java Calendar Between hoursBetween(Calendar start, Calendar end, boolean assumeSameDate)

Here you can find the source of hoursBetween(Calendar start, Calendar end, boolean assumeSameDate)

Description

hours Between

License

Open Source License

Declaration

public static int hoursBetween(Calendar start, Calendar end, boolean assumeSameDate) 

Method Source Code

//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;
    }
}

Related

  1. getDaysBetween(Calendar firstDate, Calendar lastDate)
  2. getDaysBetween(Calendar start, Calendar end)
  3. getDaysBetween(Calendar startDate, Calendar endDate)
  4. getDaysBetween(final Calendar start, final Calendar end)
  5. getFmtCalendar(Calendar c, long betweenTimeout, StringBuilder sb)
  6. hoursBetween(Calendar startDate, Calendar endDate)
  7. isBetween(Calendar time, Calendar start, Calendar end)
  8. numberOfDaysBetween(Calendar start, Calendar end)