Java TimeUnit Calculate getDateDiff(Date date1, Date date2, TimeUnit timeUnit)

Here you can find the source of getDateDiff(Date date1, Date date2, TimeUnit timeUnit)

Description

Get a diff between two dates

License

Open Source License

Parameter

Parameter Description
date1 the oldest date
date2 the newest date
timeUnit the unit in which you want the diff

Return

the diff value, in the provided unit

Declaration

public static long getDateDiff(Date date1, Date date2, TimeUnit timeUnit) 

Method Source Code

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

import java.util.Date;

import java.util.concurrent.TimeUnit;

public class Main {
    /**/*w ww . ja  v a 2  s.  c  om*/
     * Get a diff between two dates
     * @param date1 the oldest date
     * @param date2 the newest date
     * @param timeUnit the unit in which you want the diff
     * @return the diff value, in the provided unit
     */
    public static long getDateDiff(Date date1, Date date2, TimeUnit timeUnit) {
        long diffInMillies = date2.getTime() - date1.getTime();
        return timeUnit.convert(diffInMillies, TimeUnit.MILLISECONDS);
    }
}

Related

  1. differenceBetween(Date initDate, Date endDate, TimeUnit units)
  2. getBase(final TimeUnit unit)
  3. getBucketInMillis(int bucketSize, TimeUnit bucketUnit)
  4. getComingTime(Integer delta, TimeUnit unit)
  5. getDate(final TimeUnit unit, final int offset)
  6. getDateDiff(final Date d1, final Date d2, final TimeUnit timeUnit)
  7. getDifference(Calendar initDate, Calendar endDate, TimeUnit units)
  8. getExpiringMap(long time, TimeUnit unit)
  9. getFragment(final Calendar calendar, final int fragment, final TimeUnit unit)