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

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

Description

Calculates the difference between the given Date in minutes.

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 minutes.

Declaration

public static long calculateDiffInMinutes(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_MINUTE = 60 * MILLIS_PER_SECOND;
    /**/*from  ww w  .  j  a  va2 s  . c o m*/
     * Calculates the difference between the given {@link Date} in minutes.
     * <br>
     * Internally calls {@link #calculateDiffInMilliSeconds(Date, Date)}. 
     * @param date1 -{@link Date} object.
     * @param date2 -{@link Date} object.
     * @return -time difference in long minutes.
     * @throws NullPointerException if any of the parameters is null.
     */
    public static long calculateDiffInMinutes(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 differenceInMinutes = (calculateDiffInMilliSeconds(date1,
                date2)) / MILLIS_PER_MINUTE;
        return differenceInMinutes;
    }
    /**
     * 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. calculateDiffInDays(Date date1, Date date2)
  3. calculateDiffInHours(Date date1, Date date2)
  4. calculateDiffInMilliSeconds(Date date1, Date date2)
  5. dateDiff(String startTime, String endTime)
  6. dateDiff(String startTime, String endTime, String format, String str)
  7. dayDiff(Date date1, Date date2)
  8. dayOffset(Date date, int offset)