- java.lang.Object
- 
- java.util.Calendar.Builder
 
- 
- Enclosing class:
- Calendar
 
 public static class Calendar.Builder extends Object Calendar.Builderis used for creating aCalendarfrom various date-time parameters.There are two ways to set a Calendarto 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 asYEAR, to their desired values. These two ways can't be mixed. Trying to set both the instant and individual fields will cause anIllegalStateExceptionto 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 theYEARvalue 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 Calendarconstants are statically imported.The following code produces a Calendarwith 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 Calendarwith date 1989-01-08 (Gregorian), assuming that the defaultERAis Heisei that started on that day.Calendar cal = new Calendar.Builder().setCalendarType("japanese") .setFields(YEAR, 1, DAY_OF_YEAR, 1).build();- Since:
- 1.8
- See Also:
- Calendar.getInstance(TimeZone, Locale),- Calendar.fields
 
- 
- 
Constructor SummaryConstructors Constructor Description Builder()Constructs aCalendar.Builder.
 - 
Method SummaryAll Methods Instance Methods Concrete Methods Modifier and Type Method Description Calendarbuild()Returns aCalendarbuilt from the parameters set by the setter methods.Calendar.Builderset(int field, int value)Sets thefieldparameter to the givenvalue.Calendar.BuildersetCalendarType(String type)Sets the calendar type parameter to the giventype.Calendar.BuildersetDate(int year, int month, int dayOfMonth)Sets the date field parameters to the values given byyear,month, anddayOfMonth.Calendar.BuildersetFields(int... fieldValuePairs)Sets field parameters to their values given byfieldValuePairsthat are pairs of a field and its value.Calendar.BuildersetInstant(long instant)Sets the instant parameter to the giveninstantvalue that is a millisecond offset from the Epoch.Calendar.BuildersetInstant(Date instant)Sets the instant parameter to theinstantvalue given by aDate.Calendar.BuildersetLenient(boolean lenient)Sets the lenient mode parameter to the value given bylenient.Calendar.BuildersetLocale(Locale locale)Sets the locale parameter to the givenlocale.Calendar.BuildersetTimeOfDay(int hourOfDay, int minute, int second)Sets the time of day field parameters to the values given byhourOfDay,minute, andsecond.Calendar.BuildersetTimeOfDay(int hourOfDay, int minute, int second, int millis)Sets the time of day field parameters to the values given byhourOfDay,minute,second, andmillis.Calendar.BuildersetTimeZone(TimeZone zone)Sets the time zone parameter to the givenzone.Calendar.BuildersetWeekDate(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.Calendar.BuildersetWeekDefinition(int firstDayOfWeek, int minimalDaysInFirstWeek)Sets the week definition parameters to the values given byfirstDayOfWeekandminimalDaysInFirstWeekthat are used to determine the first week of a year.
 
- 
- 
- 
Method Detail- 
setInstantpublic Calendar.Builder setInstant(long instant) Sets the instant parameter to the giveninstantvalue that is a millisecond offset from the Epoch.- Parameters:
- instant- a millisecond offset from the Epoch
- Returns:
- this Calendar.Builder
- Throws:
- IllegalStateException- if any of the field parameters have already been set
- See Also:
- Calendar.setTime(Date),- Calendar.setTimeInMillis(long),- Calendar.time
 
 - 
setInstantpublic Calendar.Builder setInstant(Date instant) Sets the instant parameter to theinstantvalue given by aDate. This method is equivalent to a call tosetInstant(instant.getTime()).- Parameters:
- instant- a- Daterepresenting a millisecond offset from the Epoch
- Returns:
- this Calendar.Builder
- Throws:
- NullPointerException- if- instantis- null
- IllegalStateException- if any of the field parameters have already been set
- See Also:
- Calendar.setTime(Date),- Calendar.setTimeInMillis(long),- Calendar.time
 
 - 
setpublic Calendar.Builder set(int field, int value) Sets thefieldparameter to the givenvalue.fieldis an index to theCalendar.fields, such asDAY_OF_MONTH. Field value validation is not performed in this method. Any out of range values are either normalized in lenient mode or detected as an invalid value in non-lenient mode when building aCalendar.- Parameters:
- field- an index to the- Calendarfields
- value- the field value
- Returns:
- this Calendar.Builder
- Throws:
- IllegalArgumentException- if- fieldis invalid
- IllegalStateException- if the instant value has already been set, or if fields have been set too many (approximately- Integer.MAX_VALUE) times.
- See Also:
- Calendar.set(int, int)
 
 - 
setFieldspublic Calendar.Builder setFields(int... fieldValuePairs) Sets field parameters to their values given byfieldValuePairsthat are pairs of a field and its value. For example,setFields(Calendar.YEAR, 2013, Calendar.MONTH, Calendar.DECEMBER, Calendar.DAY_OF_MONTH, 23);is equivalent to the sequence of the followingsetcalls:set(Calendar.YEAR, 2013) .set(Calendar.MONTH, Calendar.DECEMBER) .set(Calendar.DAY_OF_MONTH, 23); - Parameters:
- fieldValuePairs- field-value pairs
- Returns:
- this Calendar.Builder
- Throws:
- NullPointerException- if- fieldValuePairsis- null
- IllegalArgumentException- if any of fields are invalid, or if- fieldValuePairs.lengthis an odd number.
- IllegalStateException- if the instant value has been set, or if fields have been set too many (approximately- Integer.MAX_VALUE) times.
 
 - 
setDatepublic Calendar.Builder setDate(int year, int month, int dayOfMonth) Sets the date field parameters to the values given byyear,month, anddayOfMonth. This method is equivalent to a call to:setFields(Calendar.YEAR, year, Calendar.MONTH, month, Calendar.DAY_OF_MONTH, dayOfMonth);- Parameters:
- year- the- YEARvalue
- month- the- MONTHvalue (the month numbering is 0-based).
- dayOfMonth- the- DAY_OF_MONTHvalue
- Returns:
- this Calendar.Builder
 
 - 
setTimeOfDaypublic Calendar.Builder setTimeOfDay(int hourOfDay, int minute, int second) Sets the time of day field parameters to the values given byhourOfDay,minute, andsecond. This method is equivalent to a call to:setTimeOfDay(hourOfDay, minute, second, 0); - Parameters:
- hourOfDay- the- HOUR_OF_DAYvalue (24-hour clock)
- minute- the- MINUTEvalue
- second- the- SECONDvalue
- Returns:
- this Calendar.Builder
 
 - 
setTimeOfDaypublic Calendar.Builder setTimeOfDay(int hourOfDay, int minute, int second, int millis) Sets the time of day field parameters to the values given byhourOfDay,minute,second, andmillis. This method is equivalent to a call to:setFields(Calendar.HOUR_OF_DAY, hourOfDay, Calendar.MINUTE, minute, Calendar.SECOND, second, Calendar.MILLISECOND, millis);- Parameters:
- hourOfDay- the- HOUR_OF_DAYvalue (24-hour clock)
- minute- the- MINUTEvalue
- second- the- SECONDvalue
- millis- the- MILLISECONDvalue
- Returns:
- this Calendar.Builder
 
 - 
setWeekDatepublic Calendar.Builder 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.If the specified calendar doesn't support week dates, the buildmethod will throw anIllegalArgumentException.- Parameters:
- weekYear- the week year
- weekOfYear- the week number based on- weekYear
- dayOfWeek- the day of week value: one of the constants for the- DAY_OF_WEEKfield:- SUNDAY, ...,- SATURDAY.
- Returns:
- this Calendar.Builder
- See Also:
- Calendar.setWeekDate(int, int, int),- Calendar.isWeekDateSupported()
 
 - 
setTimeZonepublic Calendar.Builder setTimeZone(TimeZone zone) Sets the time zone parameter to the givenzone. If no time zone parameter is given to thisCalendar.Builder, the defaultTimeZonewill be used in thebuildmethod.- Parameters:
- zone- the- TimeZone
- Returns:
- this Calendar.Builder
- Throws:
- NullPointerException- if- zoneis- null
- See Also:
- Calendar.setTimeZone(TimeZone)
 
 - 
setLenientpublic Calendar.Builder setLenient(boolean lenient) Sets the lenient mode parameter to the value given bylenient. If no lenient parameter is given to thisCalendar.Builder, lenient mode will be used in thebuildmethod.- Parameters:
- lenient-- truefor lenient mode;- falsefor non-lenient mode
- Returns:
- this Calendar.Builder
- See Also:
- Calendar.setLenient(boolean)
 
 - 
setCalendarTypepublic Calendar.Builder setCalendarType(String type) Sets the calendar type parameter to the giventype. The calendar type given by this method has precedence over any explicit or implicit calendar type given by the locale.In addition to the available calendar types returned by the Calendar.getAvailableCalendarTypesmethod,"gregorian"and"iso8601"as aliases of"gregory"can be used with this method.- Parameters:
- type- the calendar type
- Returns:
- this Calendar.Builder
- Throws:
- NullPointerException- if- typeis- null
- IllegalArgumentException- if- typeis unknown
- IllegalStateException- if another calendar type has already been set
- See Also:
- Calendar.getCalendarType(),- Calendar.getAvailableCalendarTypes()
 
 - 
setLocalepublic Calendar.Builder setLocale(Locale locale) Sets the locale parameter to the givenlocale. If no locale is given to thisCalendar.Builder, the defaultLocaleforLocale.Category.FORMATwill be used.If no calendar type is explicitly given by a call to the setCalendarTypemethod, theLocalevalue is used to determine what type ofCalendarto be built.If no week definition parameters are explicitly given by a call to the setWeekDefinitionmethod, theLocale's default values are used.- Parameters:
- locale- the- Locale
- Returns:
- this Calendar.Builder
- Throws:
- NullPointerException- if- localeis- null
- See Also:
- Calendar.getInstance(Locale)
 
 - 
setWeekDefinitionpublic Calendar.Builder setWeekDefinition(int firstDayOfWeek, int minimalDaysInFirstWeek) Sets the week definition parameters to the values given byfirstDayOfWeekandminimalDaysInFirstWeekthat are used to determine the first week of a year. The parameters given by this method have precedence over the default values given by the locale.- Parameters:
- firstDayOfWeek- the first day of a week; one of- Calendar.SUNDAYto- Calendar.SATURDAY
- minimalDaysInFirstWeek- the minimal number of days in the first week (1..7)
- Returns:
- this Calendar.Builder
- Throws:
- IllegalArgumentException- if- firstDayOfWeekor- minimalDaysInFirstWeekis invalid
- See Also:
- Calendar.getFirstDayOfWeek(),- Calendar.getMinimalDaysInFirstWeek()
 
 - 
buildpublic Calendar build() Returns aCalendarbuilt from the parameters set by the setter methods. The calendar type given by thesetCalendarTypemethod or the locale is used to determine whatCalendarto be created. If no explicit calendar type is given, the locale's default calendar is created.If the calendar type is "iso8601", the Gregorian change date of aGregorianCalendaris set toDate(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 thegetCalendarTypemethod of aGregorianCalendarcreated 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. - Returns:
- a Calendarbuilt with parameters of thisCalendar.Builder
- Throws:
- IllegalArgumentException- if the calendar type is unknown, or if any invalid field values are given in non-lenient mode, or if a week date is given for the calendar type that doesn't support week dates.
- See Also:
- Calendar.getInstance(TimeZone, Locale),- Locale.getDefault(Locale.Category),- TimeZone.getDefault()
 
 
- 
 
-