Android Date Interval Get getOffectHour(long date1, long date2)

Here you can find the source of getOffectHour(long date1, long date2)

Description

get Offect Hour

Declaration

public static int getOffectHour(long date1, long date2) 

Method Source Code

//package com.java2s;

import java.util.Calendar;

public class Main {

    public static int getOffectHour(long date1, long date2) {
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTimeInMillis(date1);
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTimeInMillis(date2);
        int h1 = calendar1.get(Calendar.HOUR_OF_DAY);
        int h2 = calendar2.get(Calendar.HOUR_OF_DAY);
        int h = 0;
        int day = getOffectDay(date1, date2);
        h = h1 - h2 + day * 24;/*www  .j a va  2  s. c om*/
        return h;
    }

    public static int getOffectDay(long date1, long date2) {
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTimeInMillis(date1);
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTimeInMillis(date2);
        //???????????
        int y1 = calendar1.get(Calendar.YEAR);
        int y2 = calendar2.get(Calendar.YEAR);
        int d1 = calendar1.get(Calendar.DAY_OF_YEAR);
        int d2 = calendar2.get(Calendar.DAY_OF_YEAR);
        int maxDays = 0;
        int day = 0;
        if (y1 - y2 > 0) {
            maxDays = calendar2.getActualMaximum(Calendar.DAY_OF_YEAR);
            day = d1 - d2 + maxDays;
        } else if (y1 - y2 < 0) {
            maxDays = calendar1.getActualMaximum(Calendar.DAY_OF_YEAR);
            day = d1 - d2 - maxDays;
        } else {
            day = d1 - d2;
        }
        return day;
    }
}

Related

  1. getTimeRangeStr(Date startDate, Date endDate)
  2. formatDuration(int duration)
  3. calcTimeBetween(Date start, Date end)
  4. getOffectDay(long date1, long date2)
  5. getOffectDay(long date1, long date2)
  6. getOffectHour(long date1, long date2)
  7. getOffectMinutes(long date1, long date2)
  8. getOffectMinutes(long date1, long date2)
  9. getStringByOffset(Date date, String format, int calendarField, int offset)