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

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

Description

Difference between two dates

License

Mozilla Public License

Return

duration

Declaration

public static Long dateDifference(Date date1, Date date2, TimeUnit timeUnit) 

Method Source Code

//package com.java2s;
/**//from  w  ww .  j  a  va2s. c o  m
 * This Source Code Form is subject to the terms of the Mozilla Public License,
 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
 * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
 * the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
 *
 * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
 * graphic logo is a trademark of OpenMRS Inc.
 */

import java.util.Date;
import java.util.concurrent.TimeUnit;

public class Main {
    /**
     * Difference between two dates
     * @return duration
     */
    public static Long dateDifference(Date date1, Date date2, TimeUnit timeUnit) {
        long diffInMillies = date2.getTime() - date1.getTime();
        return timeUnit.convert(diffInMillies, TimeUnit.MILLISECONDS);
    }
}

Related

  1. calculateDelatBasedOnRate(int rateLimit, TimeUnit periodUnit, int period, TimeUnit resultingTimeUnit)
  2. calculateTimeout(long timeout, TimeUnit timeUnit)
  3. checkTime(long time, TimeUnit timeunit)
  4. checkTimeUnit(TimeUnit timeUnit)
  5. createEquidistantDates(Calendar reference, final int periods, final TimeUnit sampleUnit, final int sampleRate, Calendar calendar)
  6. dateIn(long distance, TimeUnit unit)
  7. deepSleep(long sleepFor, TimeUnit unit)
  8. delayQuietly(final long time, final TimeUnit unit)
  9. diff(Date earlier, Date later, TimeUnit timeUnit)