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
- getDecimalYear(Calendar calendar)
- getFiscalYear(Calendar calendar)
- getIntYear(Calendar c)
- getLastYear(Calendar cal)
- getStartOfWeekCalendar(final int year, final int weekOfYear, final Locale locale)
- getWeekOfYear(Calendar currentDate)
- getWeekOfYear(int startDay, Calendar dt)
- getWeekOfYearYear(GregorianCalendar cal)
- getYear(Calendar cal)