Creates a Calendar instance for the given year, month (1-based), and day (1-based) in the default time zone - Java java.util

Java examples for java.util:Month

Description

Creates a Calendar instance for the given year, month (1-based), and day (1-based) in the default time zone

Demo Code

/**/*from   w w  w .ja  va 2s  .  com*/
 * This file is part of Words With Crosses.
 *
 * Copyright (C) 2013 Adam Rosenfield
 *
 * This program 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.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */
//package com.java2s;
import java.util.Calendar;
import java.util.TimeZone;

public class Main {
    public static void main(String[] argv) throws Exception {
        int year = 2;
        int month = 2;
        int day = 2;
        System.out.println(createDate(year, month, day));
    }

    /**
     * Creates a Calendar instance for the given year, month (1-based), and day
     * (1-based) in the default time zone
     */
    public static Calendar createDate(int year, int month, int day) {
        Calendar date = Calendar.getInstance();
        date.clear();
        date.set(year, month - 1, day); // Months start at 0 for Calendar!
        return date;
    }

    /**
     * Creates a Calendar instance for the given year, month (1-based), and day
     * (1-based) in the given time zone
     */
    public static Calendar createDate(TimeZone timeZone, int year,
            int month, int day) {
        Calendar date = Calendar.getInstance(timeZone);
        date.clear();
        date.set(year, month - 1, day); // Months start at 0 for Calendar!
        return date;
    }

    /**
     * Creates a Calendar instance for the given year, month (1-based), day
     * (1-based), hour, minute and second in the default time zone
     */
    public static Calendar createDate(int year, int month, int day,
            int hour, int minute, int second) {
        Calendar date = Calendar.getInstance();
        date.set(year, month - 1, day, hour, minute, second); // Months start at 0 for Calendar!
        date.set(Calendar.MILLISECOND, 0);
        return date;
    }

    /**
     * Creates a Calendar instance for the given year, month (1-based), day
     * (1-based), hour, minute and second in the given time zone
     */
    public static Calendar createDate(TimeZone timeZone, int year,
            int month, int day, int hour, int minute, int second) {
        Calendar date = Calendar.getInstance(timeZone);
        date.set(year, month - 1, day, hour, minute, second); // Months start at 0 for Calendar!
        date.set(Calendar.MILLISECOND, 0);
        return date;
    }
}

Related Tutorials