|
FlexDoc/Javadoc 2.0 Demo Java Doc |
There are two ways to set a Calendar to a date-time value. One is to set the instant parameter to a millisecond offset from the Epoch. The other is to set individual field parameters, such as YEAR, to their desired values. These two ways can't be mixed. Trying to set both the instant and individual fields will cause an IllegalStateException to be thrown. However, it is permitted to override previous values of the instant or field parameters.
If no enough field parameters are given for determining date and/or time, calendar specific default values are used when building a Calendar. For example, if the YEAR value isn't given for the Gregorian calendar, 1970 will be used. If there are any conflicts among field parameters, the resolution rules are applied. Therefore, the order of field setting matters.
In addition to the date-time parameters, the locale, time zone, week definition, and leniency mode parameters can be set.
Examples
The following are sample usages. Sample code assumes that the Calendar constants are statically imported.
The following code produces a Calendar with date 2012-12-31 (Gregorian) because Monday is the first day of a week with the ISO 8601 compatible week parameters.
Calendar cal = new Calendar.Builder().setCalendarType("iso8601") .setWeekDate(2013, 1, MONDAY).build();
The following code produces a Japanese Calendar with date 1989-01-08 (Gregorian), assuming that the default ERA is Heisei that started on that day.
Calendar cal = new Calendar.Builder().setCalendarType("japanese") .setFields(YEAR, 1, DAY_OF_YEAR, 1).build();
Constructor Summary |
||
Builder()
Constructs a Calendar.Builder.
|
Method Summary |
||
build()
Returns a Calendar built from the parameters set by the
setter methods.
|
||
set(int field, int value)
Sets the field parameter to the given value.
|
||
setCalendarType(String type)
Sets the calendar type parameter to the given type.
|
||
setDate(int year, int month, int dayOfMonth)
Sets the date field parameters to the values given by year,
month, and dayOfMonth.
|
||
setFields(int... fieldValuePairs)
Sets field parameters to their values given by
fieldValuePairs that are pairs of a field and its value.
|
||
setInstant(Date instant)
Sets the instant parameter to the instant value given by a
Date.
|
||
setInstant(long instant)
Sets the instant parameter to the given instant value that is
a millisecond offset from the
Epoch.
|
||
setLenient(boolean lenient)
Sets the lenient mode parameter to the value given by lenient.
|
||
Sets the locale parameter to the given locale.
|
||
setTimeOfDay(int hourOfDay, int minute, int second)
Sets the time of day field parameters to the values given by
hourOfDay, minute, and second.
|
||
setTimeOfDay(int hourOfDay, int minute, int second, int millis)
Sets the time of day field parameters to the values given by
hourOfDay, minute, second, and
millis.
|
||
setTimeZone(TimeZone zone)
Sets the time zone parameter to the given zone.
|
||
setWeekDate(int weekYear, int weekOfYear, int dayOfWeek)
Sets the week-based date parameters to the values with the given
date specifiers - week year, week of year, and day of week.
|
||
setWeekDefinition(int firstDayOfWeek, int minimalDaysInFirstWeek)
Sets the week definition parameters to the values given by
firstDayOfWeek and minimalDaysInFirstWeek that are
used to determine the first
week of a year.
|
Methods inherited from class java.lang.Object |
public Builder |
() |
public Calendar.Builder setInstant |
(long instant) |
public Calendar.Builder setInstant |
(Date instant) |
public Calendar.Builder set |
(int field, int value) |
public Calendar.Builder setFields |
(int... fieldValuePairs) |
setFields(Calendar.YEAR, 2013, Calendar.MONTH, Calendar.DECEMBER, Calendar.DAY_OF_MONTH, 23);is equivalent to the sequence of the following set calls:
set(Calendar.YEAR, 2013) .set(Calendar.MONTH, Calendar.DECEMBER) .set(Calendar.DAY_OF_MONTH, 23);
public Calendar.Builder setDate |
(int year, int month, int dayOfMonth) |
setFields(Calendar.YEAR, year, Calendar.MONTH, month, Calendar.DAY_OF_MONTH, dayOfMonth);
public Calendar.Builder setTimeOfDay |
(int hourOfDay, int minute, int second) |
setTimeOfDay(hourOfDay, minute, second, 0);
public Calendar.Builder setTimeOfDay |
(int hourOfDay, int minute, int second, int millis) |
setFields(Calendar.HOUR_OF_DAY, hourOfDay, Calendar.MINUTE, minute, Calendar.SECOND, second, Calendar.MILLISECOND, millis);
public Calendar.Builder setWeekDate |
(int weekYear, int weekOfYear, int dayOfWeek) |
If the specified calendar doesn't support week dates, the build method will throw an IllegalArgumentException.
public Calendar.Builder setTimeZone |
(TimeZone zone) |
public Calendar.Builder setLenient |
(boolean lenient) |
public Calendar.Builder setCalendarType |
(String type) |
In addition to the available calendar types returned by the Calendar.getAvailableCalendarTypes method, "gregorian" and "iso8601" as aliases of "gregory" can be used with this method.
public Calendar.Builder setLocale |
(Locale locale) |
If no calendar type is explicitly given by a call to the setCalendarType method, the Locale value is used to determine what type of Calendar to be built.
If no week definition parameters are explicitly given by a call to the setWeekDefinition method, the Locale's default values are used.
public Calendar.Builder setWeekDefinition |
(int firstDayOfWeek, int minimalDaysInFirstWeek) |
public Calendar build |
() |
If the calendar type is "iso8601", the Gregorian change date of a GregorianCalendar is set to Date(Long.MIN_VALUE) to be the proleptic Gregorian calendar. Its week definition parameters are also set to be compatible with the ISO 8601 standard. Note that the getCalendarType method of a GregorianCalendar created with "iso8601" returns "gregory".
The default values are used for locale and time zone if these parameters haven't been given explicitly.
If the locale contains the time zone with "tz" Unicode extension, and time zone hasn't been given explicitly, time zone in the locale is used.
Any out of range field values are either normalized in lenient mode or detected as an invalid value in non-lenient mode.
|
FlexDoc/Javadoc 2.0 Demo Java Doc |