Java Date Difference DifferenceInSeconds(Date date1, Date date2)

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

Description

Finds the difference in seconds between two dates.

License

Open Source License

Parameter

Parameter Description
date1 First date
date2 Second date

Return

Difference in seconds

Declaration

public static double DifferenceInSeconds(Date date1, Date date2) 

Method Source Code

//package com.java2s;
// it under the terms of the GNU General Public License as published by

import java.util.*;

public class Main {
    /** Finds the difference in seconds between two dates.
     */*w  w w.ja va2  s .  co  m*/
     * @param date1 First date
     * @param date2 Second date
     * @return Difference in seconds
     */
    public static double DifferenceInSeconds(Date date1, Date date2) {
        return DifferenceInMilliseconds(date1, date2) / 1000.0;
    }

    private static double DifferenceInMilliseconds(Date date1, Date date2) {
        return GetTimeInMilliseconds(date1) - GetTimeInMilliseconds(date2);
    }

    private static long GetTimeInMilliseconds(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.getTimeInMillis() + cal.getTimeZone().getOffset(cal.getTimeInMillis());
    }
}

Related

  1. differDays(Date src, Date target)
  2. difference(Date date1, Date date2)
  3. differenceDay(Date toDate, Date fromDate)
  4. DifferenceInMilliseconds(Date date1, Date date2)
  5. differenceInMonths(Date beginningDate, Date endingDate)
  6. differenceMonth(Date date1, Date date2)
  7. differenceOnMonth(Date date1, Date date2)
  8. differenceOnYear(Date date1, Date date2)
  9. diffIntoMilliSecond(java.util.Date date, java.util.Date date1)