Java Date Create date(final int year, final int month, final int dayOfMonth)

Here you can find the source of date(final int year, final int month, final int dayOfMonth)

Description

An easy and non-deprecated non-lenient way of creating Date objects.

License

Apache License

Parameter

Parameter Description
year the year, e.g. 2007 is year 2007
month the month, where 1 == January
dayOfMonth the day of the month, where 1 == first day of the month

Return

a Date object with time set to midnight, ie. hour = 00, minutes = 00, seconds = 00 and milliseconds = 000

Declaration

public static Date date(final int year, final int month,
        final int dayOfMonth) 

Method Source Code

//package com.java2s;
/*/*from   w ww.j  a va2  s  .c om*/
 * Copyright 2007 Kasper B. Graversen
 * 
 * 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;

public class Main {
    /**
     * An easy and non-deprecated non-lenient way of creating Date objects.
     * 
     * @param year
     *            the year, e.g. 2007 is year 2007
     * @param month
     *            the month, where 1 == January
     * @param dayOfMonth
     *            the day of the month, where 1 == first day of the month
     * @return a Date object with time set to midnight, ie. hour = 00, minutes = 00, seconds = 00 and milliseconds = 000
     */
    public static Date date(final int year, final int month,
            final int dayOfMonth) {
        final Calendar cal = Calendar.getInstance();
        cal.setLenient(false);
        cal.set(year, month - 1, dayOfMonth, 0, 0, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * An easy and non-deprecated non-lenient way of creating Date objects.
     * 
     * @param year
     *            the year, e.g. 2007 is year 2007
     * @param month
     *            the month, where 1 == January
     * @param dayOfMonth
     *            the day of the month, where 1 == first day of the month
     * @param hour
     *            the hour in 24 hour format where 0 == midnight
     * @param minute
     *            is the minute 0-59
     * @param second
     *            is the seconds 0-59
     * @return a Date object with time set to midnight, ie. hour = 00, minutes = 00, seconds = 00 and milliseconds = 000
     */
    public static Date date(final int year, final int month,
            final int dayOfMonth, final int hour, final int minute,
            final int second) {
        final Calendar cal = Calendar.getInstance();
        cal.setLenient(false);
        cal.set(year, month - 1, dayOfMonth, hour, minute, second);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }
}

Related

  1. createFutureDate(int min)
  2. createInitialDateQuery(final Date date)
  3. createTime(Date date)
  4. date()
  5. date(Date d)
  6. date(int day, int month, int year)
  7. date(int y, int m, int d)
  8. date(int year, int month, int date)
  9. date(int year, int month, int day)