Java Day Between hourDiff(Date firstDate, Date lastDate)

Here you can find the source of hourDiff(Date firstDate, Date lastDate)

Description

hour Diff

License

Open Source License

Declaration

public static String hourDiff(Date firstDate, Date lastDate) 

Method Source Code

//package com.java2s;

import java.util.Calendar;

import java.util.Date;
import java.util.GregorianCalendar;

import java.util.TimeZone;

public class Main {
    public static String hourDiff(Date firstDate, Date lastDate) {
        Calendar cal1 = new GregorianCalendar(TimeZone.getDefault());
        Calendar cal2 = new GregorianCalendar(TimeZone.getDefault());
        cal1.setTime(firstDate);//from w  w  w. jav a  2 s.  c  o m
        cal2.setTime(lastDate);
        int d1 = cal1.get(Calendar.DAY_OF_YEAR);
        int d2 = cal2.get(Calendar.DAY_OF_YEAR);
        int h1 = cal1.get(Calendar.HOUR_OF_DAY);
        int h2 = cal2.get(Calendar.HOUR_OF_DAY);
        int m1 = cal1.get(Calendar.MINUTE);
        int m2 = cal2.get(Calendar.MINUTE);
        int diff = d2 * 24 * 60 + h2 * 60 + m2 - d1 * 24 * 60 - h1 * 60 - m1;
        String min = String.valueOf(diff % 60);
        return (diff / 60) + ":" + lpad(min, '0', 2);

    }

    public static String lpad(String str, char pad, int len) {
        while (str.length() < len) {
            str = pad + "" + str;
        }
        return str;
    }
}

Related

  1. getTimeDifference(Date otherDate)
  2. getTodayDiff(String diffDate)
  3. getWorkingDaysBetween(Date fromDate, Date toDate)
  4. getYearDiff(Date date1, Date date2)
  5. getYearsDifference(Date startTime, Date endTime)
  6. internalTrimOrAlterDate(Date date, boolean trim, int dayDiff)
  7. monthDiff(Date beginDate, Date endDate)
  8. monthDiff(Date from, Date to)
  9. monthDiff(Date fromDate, Date toDate)