Java Day End secondsToEndOfDay(Date aDate)

Here you can find the source of secondsToEndOfDay(Date aDate)

Description

Compute the seconds from a date to the end of day.

License

Apache License

Parameter

Parameter Description
aDate Any date.

Return

The number of seconds from the given date until midnight.

Declaration

public static Long secondsToEndOfDay(Date aDate) 

Method Source Code

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

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

public class Main {
    /**/*from  w  w w .  j  ava 2 s .  c o m*/
     * Compute the seconds from a date to the end of day.
     *
     * @param aDate Any date.
     * @return The number of seconds from the given date until midnight.
     */
    public static Long secondsToEndOfDay(Date aDate) {
        Date midnight = endOfDay(aDate);
        return timeDifferenceInSeconds(aDate, midnight);
    }

    /**
     * Compute midnight of the date provided.
     *
     * @param aDate
     * @return Midnight of the date provided.
     */
    public static Date endOfDay(Date aDate) {
        Calendar cal = GregorianCalendar.getInstance();
        cal.setTime(aDate);
        cal.set(Calendar.MILLISECOND, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.HOUR_OF_DAY, 24);
        Date midnight = cal.getTime();
        return midnight;
    }

    /**
     * Return the time difference in seconds between two dates.
     *
     * @param startDate Start date.
     * @param endDate End date.
     * @return Time difference in seconds.
     */
    public static Long timeDifferenceInSeconds(Date startDate, Date endDate) {
        if (startDate == null || endDate == null) {
            return null;
        }
        return (endDate.getTime() - startDate.getTime()) / (1000L);
    }
}

Related

  1. isWorkingDay(final Calendar calendar, final Long[] holidays)
  2. postponeWorkingDay(final Calendar calendar, final int measureUnit, final int amount, final Long[] holidays)
  3. previousWorkingDay(final Calendar calendar, final Long[] holidays)
  4. secondsBetween(Date start, Date end, boolean assumeSameDate, boolean assumeSameHour)
  5. secondsToEndOfDay(Date aDate)
  6. setEndDay(Date date)
  7. setTimeToEndofDay(Calendar calendar)
  8. setTimeToEndOfDay(Date date)
  9. subtractSecond(Date startDate, Date endDate)