Java Date Create createDate(int year, int month, int day)

Here you can find the source of createDate(int year, int month, int day)

Description

Creates a Date object with the specfied value and the time fields set to zero.

License

Open Source License

Parameter

Parameter Description
year the year
month zero-based month number
day one-based day number

Return

a java.util.Date object

Declaration

static Date createDate(int year, int month, int day) 

Method Source Code

//package com.java2s;
/*//  w  w  w  .j a va 2s  . c o  m
 * Copyright 2009-2010 Data Archiving and Networked Services (DANS), Netherlands.
 *
 * This file is part of DANS DBF Library.
 *
 * DANS DBF Library is free software: you can redistribute it and/or modify it under the terms of
 * the GNU General Public License as published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * DANS DBF Library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with DANS DBF Library. If
 * not, see <http://www.gnu.org/licenses/>.
 */

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

public class Main {
    static final int NR_OF_DIGITS_IN_YEAR = 4;

    /**
     * Creates a Date object with the specfied value and the time fields set to zero. Note that
     * month is zero-based. The <tt>java.util.Calendar</tt> class has constants for all the months.
     *
     * @param year the year
     * @param month zero-based month number
     * @param day one-based day number
     *
     * @return a <tt>java.util.Date</tt> object
     */
    static Date createDate(int year, int month, int day) {
        final Calendar cal = Calendar.getInstance();

        if (Integer.toString(year).length() > NR_OF_DIGITS_IN_YEAR) {
            throw new IllegalArgumentException("Year more than" + NR_OF_DIGITS_IN_YEAR + " digits long");
        }

        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month);
        cal.set(Calendar.DAY_OF_MONTH, day);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);

        return cal.getTime();
    }
}

Related

  1. createDate(int year, int month, int date)
  2. createDate(int year, int month, int date)
  3. createDate(int year, int month, int day)
  4. createDate(int year, int month, int day)
  5. createDate(int year, int month, int day)
  6. createDate(int year, int month, int day)
  7. createDate(int year, int month, int day)
  8. createDate(int year, int month, int day, int hour, int minute, int second)
  9. createDate(int year, int month, int day, int hour, int minute, int second, int millisecond)