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

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

Description

Calculates the difference between the given Date in milliseconds.

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

Declaration

public static long calculateDiffInMilliSeconds(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{
    /**/*from w ww  . ja  va 2 s . c om*/
     * 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. calculateDiffInMinutes(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)