Here you can find the source of calculateDiffInMinutes(Date date1, Date date2)
Parameter | Description |
---|---|
date1 | - Date object. |
date2 | - Date object. |
Parameter | Description |
---|---|
NullPointerException | if any of the parameters is null. |
public static long calculateDiffInMinutes(Date date1, Date date2)
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; } }