Java Calendar Calculate calculateHourDiff(Calendar cal1, Calendar cal2)

Here you can find the source of calculateHourDiff(Calendar cal1, Calendar cal2)

Description

calculate Hour Diff

License

Open Source License

Declaration

private static int calculateHourDiff(Calendar cal1, Calendar cal2) 

Method Source Code

//package com.java2s;

import java.util.Calendar;

public class Main {
    public static final int SECOND = 1;
    public static final int MINUTE = 2;
    private static final long MILLISECOND_IN_HOUR = 1000 * 60 * 60;

    private static int calculateHourDiff(Calendar cal1, Calendar cal2) {
        int mil2 = cal2.get(Calendar.MILLISECOND);
        int mil1 = cal1.get(Calendar.MILLISECOND);
        int sec2 = cal2.get(Calendar.SECOND);
        int sec1 = cal1.get(Calendar.SECOND);
        int min2 = cal2.get(Calendar.MINUTE);
        int min1 = cal1.get(Calendar.MINUTE);

        cal2.set(Calendar.MINUTE, 0);
        cal1.set(Calendar.MINUTE, 0);
        cal2.set(Calendar.SECOND, 0);
        cal1.set(Calendar.SECOND, 0);
        cal2.set(Calendar.MILLISECOND, 0);
        cal1.set(Calendar.MILLISECOND, 0);

        int result = (int) ((cal2.getTimeInMillis() - cal1.getTimeInMillis()) / MILLISECOND_IN_HOUR);

        cal2.set(Calendar.MINUTE, min2);
        cal1.set(Calendar.MINUTE, min1);
        cal2.set(Calendar.SECOND, sec2);
        cal1.set(Calendar.SECOND, sec1);
        cal2.set(Calendar.MILLISECOND, mil2);
        cal1.set(Calendar.MILLISECOND, mil1);
        return result;
    }//w  ww  . ja  v a  2  s. c  o m
}

Related

  1. calcDifferenceAsDays(Calendar aBaseDate, Calendar aTargetDate)
  2. calcDifferenceAsYearsPrivate(Calendar aBaseDate, Calendar aTargetDate)
  3. calcTime(Date end, Date start, int calendarField)
  4. calcTimezone(Calendar cal)
  5. calculateDuration(Calendar para_c1, Calendar para_c2)
  6. calculateMinimalDaysInFirstWeek(Calendar calendar)
  7. calculateMonthsInBetween(Calendar startPoint, Calendar endPoint)
  8. calculateRangeInDays(Calendar start, Calendar end)
  9. calculateWeeksInBetween(Calendar startPoint, Calendar endPoint)