Java TimeUnit Calculate differenceBetween(Date initDate, Date endDate, TimeUnit units)

Here you can find the source of differenceBetween(Date initDate, Date endDate, TimeUnit units)

Description

difference Between

License

Open Source License

Declaration

public static long differenceBetween(Date initDate, Date endDate, TimeUnit units) 

Method Source Code

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

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

import java.util.concurrent.TimeUnit;

public class Main {
    public static long differenceBetween(Date initDate, Date endDate, TimeUnit units) {
        return differenceBetween(initDate, endDate, units, true);
    }/*from   www .  java  2 s .  c o  m*/

    public static long differenceBetween(Date initDate, Date endDate, TimeUnit units, Boolean includeEndDate) {
        return getDifference(toCalendar(initDate), toCalendar(endDate), units) + (includeEndDate ? 1 : 0);
    }

    public static long getDifference(Calendar initDate, Calendar endDate, TimeUnit units) {
        return units.convert(endDate.getTimeInMillis() - initDate.getTimeInMillis(), TimeUnit.MILLISECONDS);
    }

    public static Calendar toCalendar(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }
}

Related

  1. dateDifference(Date date1, Date date2, TimeUnit timeUnit)
  2. dateIn(long distance, TimeUnit unit)
  3. deepSleep(long sleepFor, TimeUnit unit)
  4. delayQuietly(final long time, final TimeUnit unit)
  5. diff(Date earlier, Date later, TimeUnit timeUnit)
  6. getBase(final TimeUnit unit)
  7. getBucketInMillis(int bucketSize, TimeUnit bucketUnit)
  8. getComingTime(Integer delta, TimeUnit unit)
  9. getDate(final TimeUnit unit, final int offset)