Java Date to Day getDayStart(final Date date)

Here you can find the source of getDayStart(final Date date)

Description

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

License

Apache License

Parameter

Parameter Description
date The date to get the day start date for.

Return

The date for the start of the specified day.

Declaration

public static Date getDayStart(final Date date) 

Method Source Code

//package com.java2s;
/**//from  w ww .  ja v a 2  s  . c  o  m
 * 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 {
    /**
     * Get a Date representing the start of the day for the specified date.
     *
     * @param date
     *          The date to get the day start date for.
     * @return The date for the start of the specified day.
     */
    public static Date getDayStart(final Date date) {
        return getDayStart(date, null);
    }

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

Related

  1. getDaysEarlierDate(int days)
  2. getDayShort(Date date)
  3. getDaysMin(Date d)
  4. getDaysPassedSince(Date dateLastModified)
  5. getDayStart(Date date)
  6. getDaysToLong(Date date1, Date date2)
  7. getDayTime(Date date, int hour, int minute, int second)