Java Calendar Night setMidnight(final Calendar cal)

Here you can find the source of setMidnight(final Calendar cal)

Description

Set the time component of the calendar to midnight.

License

BSD License

Parameter

Parameter Description
cal a parameter

Declaration

public static void setMidnight(final Calendar cal) 

Method Source Code


//package com.java2s;
/*/*w w  w.ja v a 2 s .com*/
 * = License =
 * 
 * McLean Computer Services Open Source Software License
 * 
 * (Looks like the BSD license, but less restrictive.)
 * 
 * Copyright (c) 2006-2011 Evan McLean. All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * 
 * 2. Neither the names "Evan McLean", "McLean Computer Services", "EvLib" nor
 * the names of any contributors may be used to endorse or promote products
 * derived from this software without prior written permission.
 * 
 * 3. Products derived from this software may not be called "Evlib", nor may
 * "Evlib" appear in their name, without prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * 
 * = License =
 */

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

public class Main {
    /**
     * Set the time component of the calendar to midnight.
     * 
     * @param cal
     */
    public static void setMidnight(final Calendar cal) {
        cal.set(Calendar.MILLISECOND, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.HOUR_OF_DAY, 0);
    }

    /**
     * Set the time component of the date to midnight, using the local time zone.
     * 
     * @param dt
     */
    public static void setMidnight(final Date dt) {
        final Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        setMidnight(cal);
        dt.setTime(cal.getTimeInMillis());
    }

    /**
     * Set the time component of the date to midnight.
     * 
     * @param dt
     * @param tz
     */
    public static void setMidnight(final Date dt, final TimeZone tz) {
        final Calendar cal = Calendar.getInstance(tz);
        cal.setTime(dt);
        setMidnight(cal);
        dt.setTime(cal.getTimeInMillis());
    }
}

Related

  1. getMidnightCalendar()
  2. getMidnightCalendarInstance()
  3. midnightOf(Calendar cal)
  4. setMidnight(Calendar cal)
  5. setMidnight(Calendar date)
  6. toMidnightCalendar(Date date)