Java Date Create createDate(final Date date)

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

Description

create Date

License

Open Source License

Declaration

public static Date createDate(final Date date) 

Method Source Code

//package com.java2s;
/**//from   w  w  w .j ava2  s . c  o  m
 *
 * Licensed under the GNU General Public License (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.gnu.org/licenses/gpl.txt
 *
 * 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.
 *
 * 
 * @author Vadim Kisen
 *
 * copyright 2010 by uTest 
 */

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

public class Main {
    public static final int HOUR = Calendar.HOUR;
    public static final int MINUTE = Calendar.MINUTE;
    public static final int SECOND = Calendar.SECOND;

    /**
     * Creates a date. Note: there is no need to call clearDate() afterwards,
     * since this method creates a Date with zeroed time.
     * 
     * @param day
     *            1-based (do not use Calendar.<DAY> constants!)
     * @param month
     *            1-based (do not use Calendar.<MONTH> constants!)
     * @param year
     */
    public static Date createDate(final int day, final int month, final int year) {
        final Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, day);
        return clearDate(calendar.getTime());
    }

    public static Date createDate(final Date date) {
        final Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return clearDate(calendar.getTime());
    }

    /**
     * Set hour, minute, second and millisecond to 0. Used for queries that need
     * precision in these parameters.
     */
    public static Date clearDate(final Date date) {
        if (date == null) {
            return null;
        }
        final Calendar calendar = GregorianCalendar.getInstance();

        calendar.setTime(date);
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        return calendar.getTime();
    }

    /**
     * Given a Date as parameter, it sets it to the specified time and returns a
     * copy.
     * 
     * @param date
     *            Original date
     * @param hours
     *            Hours to set to the date
     * @param minutes
     *            Minutes to set to the date
     * @param seconds
     *            Seconds to set to the date
     * 
     * @return A new Date with the given time.
     */
    public static Date setTime(final Date date, final int hours, final int minutes, final int seconds) {
        final Calendar aux = Calendar.getInstance();
        aux.setTime(date);
        aux.set(Calendar.MILLISECOND, 0);
        aux.set(Calendar.HOUR, hours);
        aux.set(Calendar.MINUTE, minutes);
        aux.set(Calendar.SECOND, seconds);
        return aux.getTime();
    }
}

Related

  1. buildDateTime(String curDate, String curTime, String meridiem)
  2. buildDateTimeUTC(Calendar cal)
  3. castToDate(Object value)
  4. create(int year, int month, int date, TimeZone timeZone)
  5. createDate(final Date date)
  6. createDate(final int aYear, final int aMonth, final int aDate, final int aHour, final int aMinute, final int aSecond)
  7. createDate(final int day, final int month, final int year)
  8. createDate(final int month, final int day, final int year)
  9. createDate(final int year, final int month, final int day, final int hour, final int minute, final int second, final int millisecond)