Java SQL Time Field clearTime(final Calendar self)

Here you can find the source of clearTime(final Calendar self)

Description

Clears the time portion of this Calendar instance; useful utility where it makes sense to compare month/day/year only portions of a Calendar.

License

Apache License

Parameter

Parameter Description
self a Calendar

Return

the Calendar but with the time portion cleared

Declaration

public static Calendar clearTime(final Calendar self) 

Method Source Code

//package com.java2s;
/*//from w  w w .  j  a va2s. c o  m
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you 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.HashMap;
import java.util.Map;

public class Main {
    private static final Map<String, Integer> CAL_MAP = new HashMap<String, Integer>();

    /**
     * Clears the time portion of this Date instance; useful utility where
     * it makes sense to compare month/day/year only portions of a Date.
     *
     * @param self a Date
     * @return the Date but with the time portion cleared
     * @since 1.6.7
     */
    public static Date clearTime(final Date self) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(self);
        clearTimeCommon(calendar);
        self.setTime(calendar.getTime().getTime());
        return self;
    }

    /**
     * Clears the time portion of this java.sql.Date instance; useful utility
     * where it makes sense to compare month/day/year only portions of a Date.
     *
     * @param self a java.sql.Date
     * @return the java.sql.Date but with the time portion cleared
     * @since 1.6.7
     */
    public static java.sql.Date clearTime(final java.sql.Date self) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(self);
        clearTimeCommon(calendar);
        self.setTime(calendar.getTime().getTime());
        return self;
    }

    /**
     * Clears the time portion of this Calendar instance; useful utility
     * where it makes sense to compare month/day/year only portions of a Calendar.
     *
     * @param self a Calendar
     * @return the Calendar but with the time portion cleared
     * @since 1.6.7
     */
    public static Calendar clearTime(final Calendar self) {
        clearTimeCommon(self);
        return self;
    }

    /**
     * Common code for {@link #clearTime(java.util.Calendar)} and {@link #clearTime(java.util.Date)}
     * and {@link #clearTime(java.sql.Date)}
     *
     * @param self a Calendar to adjust
     */
    private static void clearTimeCommon(final Calendar self) {
        self.set(Calendar.HOUR_OF_DAY, 0);
        self.clear(Calendar.MINUTE);
        self.clear(Calendar.SECOND);
        self.clear(Calendar.MILLISECOND);
    }

    /**
     * Support mutating a Calendar with a Map.
     * <p>
     * The map values are the normal values provided as the
     * second parameter to <code>java.util.Calendar#set(int, int)</code>.
     * The keys can either be the normal fields values provided as
     * the first parameter to that method or one of the following Strings:
     * <table border="1" cellpadding="4">
     *   <caption>Calendar index values</caption>
     *   <tr><td>year</td><td>Calendar.YEAR</td></tr>
     *   <tr><td>month</td><td>Calendar.MONTH</td></tr>
     *   <tr><td>date</td><td>Calendar.DATE</td></tr>
     *   <tr><td>dayOfMonth</td><td>Calendar.DATE</td></tr>
     *   <tr><td>hourOfDay</td><td>Calendar.HOUR_OF_DAY</td></tr>
     *   <tr><td>minute</td><td>Calendar.MINUTE</td></tr>
     *   <tr><td>second</td><td>Calendar.SECOND</td></tr>
     * </table>
     * Example usage:
     * <pre>
     * import static java.util.Calendar.*
     * def cal = Calendar.instance
     * def m = [:]
     * m[YEAR] = 2010
     * m[MONTH] = DECEMBER
     * m[DATE] = 25
     * cal.set(m)
     * println cal.time // Christmas 2010
     *
     * cal.set(year:2011, month:DECEMBER, date:25)
     * println cal.time // Christmas 2010
     * </pre>
     *
     * @param self    A Calendar
     * @param updates A Map of Calendar keys and values
     * @see java.util.Calendar#set(int, int)
     * @see java.util.Calendar#set(int, int, int, int, int, int)
     * @since 1.7.3
     */
    public static void set(Calendar self, Map<Object, Integer> updates) {
        for (Map.Entry<Object, Integer> entry : updates.entrySet()) {
            Object key = entry.getKey();
            if (key instanceof String)
                key = CAL_MAP.get(key);
            if (key instanceof Integer)
                self.set((Integer) key, entry.getValue());
        }
    }

    /**
     * Support mutating a Date with a Map.
     * <p>
     * The map values are the normal values provided as the
     * second parameter to <code>java.util.Calendar#set(int, int)</code>.
     * The keys can either be the normal fields values provided as
     * the first parameter to that method or one of the following Strings:
     * <table border="1" cellpadding="4">
     *   <caption>Calendar index values</caption>
     *   <tr><td>year</td><td>Calendar.YEAR</td></tr>
     *   <tr><td>month</td><td>Calendar.MONTH</td></tr>
     *   <tr><td>date</td><td>Calendar.DATE</td></tr>
     *   <tr><td>dayOfMonth</td><td>Calendar.DATE</td></tr>
     *   <tr><td>hourOfDay</td><td>Calendar.HOUR_OF_DAY</td></tr>
     *   <tr><td>minute</td><td>Calendar.MINUTE</td></tr>
     *   <tr><td>second</td><td>Calendar.SECOND</td></tr>
     * </table>
     * Example usage:
     * <pre>
     * import static java.util.Calendar.YEAR
     * def date = new Date()
     * def nextYear = date[YEAR] + 1
     * date.set(year: nextYear)
     * println date
     * </pre>
     *
     * @param self    A Date
     * @param updates A Map of Calendar keys and values
     * @see java.util.Calendar#set(int, int)
     * @see #set(java.util.Calendar, java.util.Map)
     * @since 1.7.3
     */
    public static void set(Date self, Map<Object, Integer> updates) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(self);
        set(cal, updates);
        self.setTime(cal.getTimeInMillis());
    }
}

Related

  1. clearTimeCommon(final Calendar self)
  2. clearTimeFields(java.sql.Date date)
  3. clearTimeFields(java.util.Date date)
  4. getDay(long time)