Java Date to Day getDayEnd(final Date date, TimeZone timezone)

Here you can find the source of getDayEnd(final Date date, TimeZone timezone)

Description

Get a Date representing the end of the day for the specified date.

License

Apache License

Parameter

Parameter Description
date The date to get the day end date for.
timezone The timezone that the Date should have.

Return

The date for the end of the specified day.

Declaration

public static Date getDayEnd(final Date date, TimeZone timezone) 

Method Source Code

//package com.java2s;
/**/*from  w w  w  . java 2  s. c  om*/
 * Copyright 2000-2012 TrackMate
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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

public class Main {
    /** The last hour of the day (23). */
    private static final int LAST_HOUR_IN_DAY = 23;
    /** The last minute of the hour (59). */
    private static final int LAST_MINUTE_IN_HOUR = 59;
    /** The last minute of the hour (59). */
    private static final int LAST_SECOND_IN_MINUTE = 59;
    /** The last millisecond of the second (999). */
    private static final int LAST_MILLI_IN_SECOND = 999;

    /**
     * Get a Date representing the end of the day for the specified date.
     *
     * @param date
     *          The date to get the day end date for.
     * @return The date for the end of the specified day.
     */
    public static Date getDayEnd(final Date date) {
        return getDayEnd(date, null);
    }

    /**
     * Get a Date representing the end of the day for the specified date.
     *
     * @param date
     *          The date to get the day end date for.
     * @param timezone The timezone that the Date should have.
     * @return The date for the end of the specified day.
     */
    public static Date getDayEnd(final Date date, TimeZone timezone) {
        Calendar calendar = Calendar.getInstance();
        if (timezone != null) {
            calendar.setTimeZone(timezone);
        }
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, LAST_HOUR_IN_DAY);
        calendar.set(Calendar.MINUTE, LAST_MINUTE_IN_HOUR);
        calendar.set(Calendar.SECOND, LAST_SECOND_IN_MINUTE);
        calendar.set(Calendar.MILLISECOND, LAST_MILLI_IN_SECOND);
        return calendar.getTime();
    }
}

Related

  1. getDayCount(java.util.Date _startDate, java.util.Date _endDate)
  2. getDayEnd(Date date)
  3. getDayEnd(Date date)
  4. getDayEnd(Date date)
  5. getDayEnd(Date obj)
  6. getDayFromDate(Date date)
  7. getDayMonthYear(Date aDate, boolean isAddSpace)
  8. getDayNumber(Date startDate, Date endDate)
  9. getDayOfDate(Date date)