Android Date Interval Get calculateDiffInDays(Date date1, Date date2)

Here you can find the source of calculateDiffInDays(Date date1, Date date2)

Description

Calculates the difference between the given Date in days.

Parameter

Parameter Description
date1 - Date object.
date2 - Date object.

Exception

Parameter Description
NullPointerException if any of the parameters is null.

Return

-time difference in long days.

Declaration

public static long calculateDiffInDays(Date date1, Date date2) 

Method Source Code

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class Main{
    public static final long MILLIS_PER_HOUR = 60 * MILLIS_PER_MINUTE;
    /**/*from  ww w  .java  2 s.c o m*/
     * Calculates the difference between the given {@link Date} in days.
     * <br>
     * Internally calls {@link #calculateDiffInHours(Date, Date)}. 
     * @param date1 -{@link Date} object.
     * @param date2 -{@link Date} object.
     * @return -time difference in long days.
     * @throws NullPointerException if any of the parameters is null.
     */
    public static long calculateDiffInDays(Date date1, Date date2) {
        if (StringUtils.isNull(date1)) {
            throw new NullPointerException("date1 cannot be null");
        }
        if (StringUtils.isNull(date2)) {
            throw new NullPointerException("date2 cannot be null");
        }
        long differenceInDays = (calculateDiffInHours(date1, date2)) / 24;
        return differenceInDays;
    }
    /**
     * Calculates the difference between the given {@link Date} in hours.
     * <br>
     * Internally calls {@link #calculateDiffInMilliSeconds(Date, Date)}. 
     * @param date1 -{@link Date} object.
     * @param date2 -{@link Date} object.
     * @return -time difference in long hours.
     * @throws NullPointerException if any of the parameters is null.
     */
    public static long calculateDiffInHours(Date date1, Date date2) {
        if (StringUtils.isNull(date1)) {
            throw new NullPointerException("date1 cannot be null");
        }
        if (StringUtils.isNull(date2)) {
            throw new NullPointerException("date2 cannot be null");
        }
        long differenceInHours = (calculateDiffInMilliSeconds(date1, date2))
                / MILLIS_PER_HOUR;
        return differenceInHours;
    }
    /**
     * Calculates the difference between the given {@link Date} in milliseconds.
     * 
     * @param date1 -{@link Date} object.
     * @param date2 -{@link Date} object.
     * @return -time difference in long milliseconds.
     * @throws NullPointerException if any of the parameters is null.
     */
    public static long calculateDiffInMilliSeconds(Date date1, Date date2) {
        if (StringUtils.isNull(date1)) {
            throw new NullPointerException("date1 cannot be null");
        }
        if (StringUtils.isNull(date2)) {
            throw new NullPointerException("date2 cannot be null");
        }
        long milliseconds1 = date1.getTime();
        long milliseconds2 = date2.getTime();
        long differenceInMilliSeconds = milliseconds2 - milliseconds1;
        return differenceInMilliSeconds;
    }
}

Related

  1. DaysBetween(Date beginDate, Date endDate)
  2. calculateDiffInHours(Date date1, Date date2)
  3. calculateDiffInMilliSeconds(Date date1, Date date2)
  4. calculateDiffInMinutes(Date date1, Date date2)
  5. dateDiff(String startTime, String endTime)