Java Day Between getTimeDifference(Date otherDate)

Here you can find the source of getTimeDifference(Date otherDate)

Description

Get the Difference in time From a Date to Now.

License

Apache License

Parameter

Parameter Description
otherDate - The Date to be used to determine difference in Time between these two points.

Return

long - The Number of Millisecond Difference between a Date and Now in Time.

Declaration

public static long getTimeDifference(Date otherDate) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Calendar;
import java.util.Date;

public class Main {
    /**/*from  w  w w .  j av a2 s .co  m*/
     * Get the Difference in time From a Date to Now.
     *
     * @param otherDate - The Date to be used to determine difference in Time between these two points.
     * @return long - The Number of Millisecond Difference between a Date and Now in Time.
     */
    public static long getTimeDifference(Date otherDate) {
        if (otherDate == null) {
            throw new IllegalArgumentException("Invalid Date Parameter!");
        }
        Calendar now = Calendar.getInstance();
        now.setTime(new Date());
        Calendar aPointInTime = Calendar.getInstance();
        aPointInTime.setTime(otherDate);
        return aPointInTime.getTimeInMillis() - now.getTimeInMillis();
    }

    /**
     * Get the Difference in time From a Date to Another Date.
     *
     * @param thisDate  - A Date referencing a Point in Time.
     * @param otherDate - The Date to be used to determine difference in Time between these two points.
     * @return long - The Number of Millisecond Difference between a Date and Now in Time.
     */
    public static long getTimeDifference(Date thisDate, Date otherDate) {
        if ((thisDate == null) || (otherDate == null)) {
            throw new IllegalArgumentException("Invalid Date Parameter!");
        }
        Calendar aPointInTime = Calendar.getInstance();
        aPointInTime.setTime(thisDate);
        Calendar anotherPointInTime = Calendar.getInstance();
        anotherPointInTime.setTime(otherDate);
        return aPointInTime.getTimeInMillis() - anotherPointInTime.getTimeInMillis();
    }
}

Related

  1. getMonthDifference(Date from, Date to)
  2. getMonthsDifference(Date earlierDate, Date laterDate)
  3. getNumDaysDiffExclTime(Date date1, Date date2)
  4. getTimeDifference(Date d1, Date d2)
  5. getTimeDifference(Date date1, Date date2)
  6. getTodayDiff(String diffDate)
  7. getWorkingDaysBetween(Date fromDate, Date toDate)
  8. getYearDiff(Date date1, Date date2)
  9. getYearsDifference(Date startTime, Date endTime)