Java Day Between getTimeDifference(Date d1, Date d2)

Here you can find the source of getTimeDifference(Date d1, Date d2)

Description

get Time Difference

License

Open Source License

Declaration

public static long[] getTimeDifference(Date d1, Date d2) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    public static long[] getTimeDifference(Date d1, Date d2) {
        long[] result = new long[5];

        Calendar cal = Calendar.getInstance();
        cal.setTime(d1);//  w w w.ja v  a2 s.co m
        long t1 = cal.getTimeInMillis();

        cal.setTime(d2);

        long diff = Math.abs(cal.getTimeInMillis() - t1);
        final int ONE_DAY = 1000 * 60 * 60 * 24;
        final int ONE_HOUR = ONE_DAY / 24;
        final int ONE_MINUTE = ONE_HOUR / 60;
        final int ONE_SECOND = ONE_MINUTE / 60;

        long d = diff / ONE_DAY;
        diff %= ONE_DAY;

        long h = diff / ONE_HOUR;
        diff %= ONE_HOUR;

        long m = diff / ONE_MINUTE;
        diff %= ONE_MINUTE;

        long s = diff / ONE_SECOND;
        long ms = diff % ONE_SECOND;
        result[0] = d;
        result[1] = h;
        result[2] = m;
        result[3] = s;
        result[4] = ms;

        return result;
    }
}

Related

  1. getMinuteDiffByTime(Date time1, Date time2)
  2. getMinutesDifference(final Date begin, final Date end)
  3. getMonthDifference(Date from, Date to)
  4. getMonthsDifference(Date earlierDate, Date laterDate)
  5. getNumDaysDiffExclTime(Date date1, Date date2)
  6. getTimeDifference(Date date1, Date date2)
  7. getTimeDifference(Date otherDate)
  8. getTodayDiff(String diffDate)
  9. getWorkingDaysBetween(Date fromDate, Date toDate)