Java Calendar Year getValidCalendar(int year, int month, int day)

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

Description

Validate the actual date of the given date elements and returns a calendar instance based on the given date elements.

License

Apache License

Parameter

Parameter Description
year The year part of the date.
month The month part of the date.
day The day part of the date.

Exception

Parameter Description
IllegalArgumentException If the given date elements does not represent a valid date.

Return

A Calendar instance prefilled with the given date elements.

Declaration

public static Calendar getValidCalendar(int year, int month, int day) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.Calendar;

public class Main {
    /**/*from  w  w  w. j  a va  2 s . co m*/
     * Validate the actual date of the given date elements and returns a calendar instance based on
     * the given date elements. The time is forced to 00:00:00.
     * @param year The year part of the date.
     * @param month The month part of the date.
     * @param day The day part of the date.
     * @return A Calendar instance prefilled with the given date elements.
     * @throws IllegalArgumentException If the given date elements does not represent a valid date.
     */
    public static Calendar getValidCalendar(int year, int month, int day) {
        return getValidCalendar(year, month, day, 0, 0, 0);
    }

    /**
     * Validate the actual date of the given date elements and returns a calendar instance based on
     * the given date elements.
     * @param year The year part of the date.
     * @param month The month part of the date.
     * @param day The day part of the date.
     * @param hour The hour part of the date.
     * @param minute The minute part of the date.
     * @param second The second part of the date.
     * @return A Calendar instance prefilled with the given date elements.
     * @throws IllegalArgumentException If the given date elements does not represent a valid date.
     */
    public static Calendar getValidCalendar(int year, int month, int day, int hour, int minute, int second) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.setLenient(false); // Don't automatically convert invalid date.
        calendar.set(year, month - 1, day, hour, minute, second);
        calendar.getTimeInMillis(); // Lazy update, throws IllegalArgumentException if invalid date.
        return calendar;
    }
}

Related

  1. getDecimalYear(Calendar calendar)
  2. getFiscalYear(Calendar calendar)
  3. getIntYear(Calendar c)
  4. getLastYear(Calendar cal)
  5. getStartOfWeekCalendar(final int year, final int weekOfYear, final Locale locale)
  6. getWeekOfYear(Calendar currentDate)
  7. getWeekOfYear(int startDay, Calendar dt)
  8. getWeekOfYearYear(GregorianCalendar cal)
  9. getYear(Calendar cal)