Android APIs
public abstract class

Calendar

extends Object
implements Serializable Cloneable Comparable<Calendar>
java.lang.Object
   ↳ java.util.Calendar
Known Direct Subclasses

Class Overview

Calendar is an abstract base class for converting between a Date object and a set of integer fields such as YEAR, MONTH, DAY, HOUR, and so on. (A Date object represents a specific instant in time with millisecond precision. See Date for information about the Date class.)

Subclasses of Calendar interpret a Date according to the rules of a specific calendar system.

Like other locale-sensitive classes, Calendar provides a class method, getInstance, for getting a default instance of this class for general use. Calendar's getInstance method returns a calendar whose locale is based on system settings and whose time fields have been initialized with the current date and time:

Calendar rightNow = Calendar.getInstance()

A Calendar object can produce all the time field values needed to implement the date-time formatting for a particular language and calendar style (for example, Japanese-Gregorian, Japanese-Traditional). Calendar defines the range of values returned by certain fields, as well as their meaning. For example, the first month of the year has value MONTH == JANUARY for all calendars. Other values are defined by the concrete subclass, such as ERA and YEAR. See individual field documentation and subclass documentation for details.

When a Calendar is lenient, it accepts a wider range of field values than it produces. For example, a lenient GregorianCalendar interprets MONTH == JANUARY, DAY_OF_MONTH == 32 as February 1. A non-lenient GregorianCalendar throws an exception when given out-of-range field settings. When calendars recompute field values for return by get(), they normalize them. For example, a GregorianCalendar always produces DAY_OF_MONTH values between 1 and the length of the month.

Calendar defines a locale-specific seven day week using two parameters: the first day of the week and the minimal days in first week (from 1 to 7). These numbers are taken from the locale resource data when a Calendar is constructed. They may also be specified explicitly through the API.

When setting or getting the WEEK_OF_MONTH or WEEK_OF_YEAR fields, Calendar must determine the first week of the month or year as a reference point. The first week of a month or year is defined as the earliest seven day period beginning on getFirstDayOfWeek() and containing at least getMinimalDaysInFirstWeek() days of that month or year. Weeks numbered ..., -1, 0 precede the first week; weeks numbered 2, 3,... follow it. Note that the normalized numbering returned by get() may be different. For example, a specific Calendar subclass may designate the week before week 1 of a year as week n of the previous year.

When computing a Date from time fields, two special circumstances may arise: there may be insufficient information to compute the Date (such as only year and month but no day in the month), or there may be inconsistent information (such as "Tuesday, July 15, 1996" -- July 15, 1996 is actually a Monday).

Insufficient information. The calendar will use default information to specify the missing fields. This may vary by calendar; for the Gregorian calendar, the default for a field is the same as that of the start of the epoch: i.e., YEAR = 1970, MONTH = JANUARY, DATE = 1, etc.

Inconsistent information. If fields conflict, the calendar will give preference to fields set more recently. For example, when determining the day, the calendar will look for one of the following combinations of fields. The most recent combination, as determined by the most recently set single field, will be used.

 MONTH + DAY_OF_MONTH
 MONTH + WEEK_OF_MONTH + DAY_OF_WEEK
 MONTH + DAY_OF_WEEK_IN_MONTH + DAY_OF_WEEK
 DAY_OF_YEAR
 DAY_OF_WEEK + WEEK_OF_YEAR
For the time of day:
 HOUR_OF_DAY
 AM_PM + HOUR

Note: There are certain possible ambiguities in interpretation of certain singular times, which are resolved in the following ways:

  1. 24:00:00 "belongs" to the following day. That is, 23:59 on Dec 31, 1969 < 24:00 on Jan 1, 1970 < 24:01:00 on Jan 1, 1970 form a sequence of three consecutive minutes in time.
  2. Although historically not precise, midnight also belongs to "am", and noon belongs to "pm", so on the same day, we have 12:00 am (midnight) < 12:01 am, and 12:00 pm (noon) < 12:01 pm

The date or time format strings are not part of the definition of a calendar, as those must be modifiable or overridable by the user at runtime. Use DateFormat to format dates.

Field manipulation methods

Calendar fields can be changed using three methods: set(), add(), and roll().

set(f, value) changes field f to value. In addition, it sets an internal member variable to indicate that field f has been changed. Although field f is changed immediately, the calendar's milliseconds is not recomputed until the next call to get(), getTime(), or getTimeInMillis() is made. Thus, multiple calls to set() do not trigger multiple, unnecessary computations. As a result of changing a field using set(), other fields may also change, depending on the field, the field value, and the calendar system. In addition, get(f) will not necessarily return value after the fields have been recomputed. The specifics are determined by the concrete calendar class.

Example: Consider a GregorianCalendar originally set to August 31, 1999. Calling set(Calendar.MONTH, Calendar.SEPTEMBER) sets the calendar to September 31, 1999. This is a temporary internal representation that resolves to October 1, 1999 if getTime()is then called. However, a call to set(Calendar.DAY_OF_MONTH, 30) before the call to getTime() sets the calendar to September 30, 1999, since no recomputation occurs after set() itself.

add(f, delta) adds delta to field f. This is equivalent to calling set(f, get(f) + delta) with two adjustments:

Add rule 1. The value of field f after the call minus the value of field f before the call is delta, modulo any overflow that has occurred in field f. Overflow occurs when a field value exceeds its range and, as a result, the next larger field is incremented or decremented and the field value is adjusted back into its range.

Add rule 2. If a smaller field is expected to be invariant, but   it is impossible for it to be equal to its prior value because of changes in its minimum or maximum after field f is changed, then its value is adjusted to be as close as possible to its expected value. A smaller field represents a smaller unit of time. HOUR is a smaller field than DAY_OF_MONTH. No adjustment is made to smaller fields that are not expected to be invariant. The calendar system determines what fields are expected to be invariant.

In addition, unlike set(), add() forces an immediate recomputation of the calendar's milliseconds and all fields.

Example: Consider a GregorianCalendar originally set to August 31, 1999. Calling add(Calendar.MONTH, 13) sets the calendar to September 30, 2000. Add rule 1 sets the MONTH field to September, since adding 13 months to August gives September of the next year. Since DAY_OF_MONTH cannot be 31 in September in a GregorianCalendar, add rule 2 sets the DAY_OF_MONTH to 30, the closest possible value. Although it is a smaller field, DAY_OF_WEEK is not adjusted by rule 2, since it is expected to change when the month changes in a GregorianCalendar.

roll(f, delta) adds delta to field f without changing larger fields. This is equivalent to calling add(f, delta) with the following adjustment:

Roll rule. Larger fields are unchanged after the call. A larger field represents a larger unit of time. DAY_OF_MONTH is a larger field than HOUR.

Example: Consider a GregorianCalendar originally set to August 31, 1999. Calling roll(Calendar.MONTH, 8) sets the calendar to April 30, 1999. Add rule 1 sets the MONTH field to April. Using a GregorianCalendar, the DAY_OF_MONTH cannot be 31 in the month April. Add rule 2 sets it to the closest possible value, 30. Finally, the roll rule maintains the YEAR field value of 1999.

Example: Consider a GregorianCalendar originally set to Sunday June 6, 1999. Calling roll(Calendar.WEEK_OF_MONTH, -1) sets the calendar to Tuesday June 1, 1999, whereas calling add(Calendar.WEEK_OF_MONTH, -1) sets the calendar to Sunday May 30, 1999. This is because the roll rule imposes an additional constraint: The MONTH must not change when the WEEK_OF_MONTH is rolled. Taken together with add rule 1, the resultant date must be between Tuesday June 1 and Saturday June 5. According to add rule 2, the DAY_OF_WEEK, an invariant when changing the WEEK_OF_MONTH, is set to Tuesday, the closest possible value to Sunday (where Sunday is the first day of the week).

Usage model. To motivate the behavior of add() and roll(), consider a user interface component with increment and decrement buttons for the month, day, and year, and an underlying GregorianCalendar. If the interface reads January 31, 1999 and the user presses the month increment button, what should it read? If the underlying implementation uses set(), it might read March 3, 1999. A better result would be February 28, 1999. Furthermore, if the user presses the month increment button again, it should read March 31, 1999, not March 28, 1999. By saving the original date and using either add() or roll(), depending on whether larger fields should be affected, the user interface can behave as most users will intuitively expect.

Note: You should always use roll and add rather than attempting to perform arithmetic operations directly on the fields of a Calendar. It is quite possible for Calendar subclasses to have fields with non-linear behavior, for example missing months or days during non-leap years. The subclasses' add and roll methods will take this into account, while simple arithmetic manipulations may give invalid results.

Summary

Constants
int ALL_STYLES Requests both SHORT and LONG styles in the map returned by getDisplayNames(int, int, Locale).
int AM Value of the AM_PM field indicating the period of the day from midnight to just before noon.
int AM_PM Field number for get and set indicating whether the HOUR is before or after noon.
int APRIL Value of the MONTH field indicating the fourth month of the year.
int AUGUST Value of the MONTH field indicating the eighth month of the year.
int DATE Field number for get and set indicating the day of the month.
int DAY_OF_MONTH Field number for get and set indicating the day of the month.
int DAY_OF_WEEK Field number for get and set indicating the day of the week.
int DAY_OF_WEEK_IN_MONTH Field number for get and set indicating the ordinal number of the day of the week within the current month.
int DAY_OF_YEAR Field number for get and set indicating the day number within the current year.
int DECEMBER Value of the MONTH field indicating the twelfth month of the year.
int DST_OFFSET Field number for get and set indicating the daylight savings offset from the ZONE_OFFSET in milliseconds.
int ERA Field number for get and set indicating the era, e.g., AD or BC in the Julian calendar.
int FEBRUARY Value of the MONTH field indicating the second month of the year.
int FIELD_COUNT This is the total number of fields in this calendar.
int FRIDAY Value of the DAY_OF_WEEK field indicating Friday.
int HOUR Field number for get and set indicating the hour of the morning or afternoon.
int HOUR_OF_DAY Field number for get and set indicating the hour of the day.
int JANUARY Value of the MONTH field indicating the first month of the year.
int JULY Value of the MONTH field indicating the seventh month of the year.
int JUNE Value of the MONTH field indicating the sixth month of the year.
int LONG Requests long names (such as "January") from getDisplayName(int, int, Locale) or getDisplayNames(int, int, Locale).
int MARCH Value of the MONTH field indicating the third month of the year.
int MAY Value of the MONTH field indicating the fifth month of the year.
int MILLISECOND Field number for get and set indicating the millisecond within the second.
int MINUTE Field number for get and set indicating the minute within the hour.
int MONDAY Value of the DAY_OF_WEEK field indicating Monday.
int MONTH Field number for get and set indicating the month.
int NOVEMBER Value of the MONTH field indicating the eleventh month of the year.
int OCTOBER Value of the MONTH field indicating the tenth month of the year.
int PM Value of the AM_PM field indicating the period of the day from noon to just before midnight.
int SATURDAY Value of the DAY_OF_WEEK field indicating Saturday.
int SECOND Field number for get and set indicating the second within the minute.
int SEPTEMBER Value of the MONTH field indicating the ninth month of the year.
int SHORT Requests short names (such as "Jan") from getDisplayName(int, int, Locale) or getDisplayNames(int, int, Locale).
int SUNDAY Value of the DAY_OF_WEEK field indicating Sunday.
int THURSDAY Value of the DAY_OF_WEEK field indicating Thursday.
int TUESDAY Value of the DAY_OF_WEEK field indicating Tuesday.
int UNDECIMBER Value of the MONTH field indicating the thirteenth month of the year.
int WEDNESDAY Value of the DAY_OF_WEEK field indicating Wednesday.
int WEEK_OF_MONTH Field number for get and set indicating the week number within the current month.
int WEEK_OF_YEAR Field number for get and set indicating the week number within the current year.
int YEAR Field number for get and set indicating the year.
int ZONE_OFFSET Field number for get and set indicating the raw (non-DST) offset from GMT in milliseconds.
Fields
protected boolean areFieldsSet True iff the values in fields[] correspond to time.
protected int[] fields Contains broken-down field values for the current value of time if areFieldsSet is true, or stale data corresponding to some previous value otherwise.
protected boolean[] isSet Whether the corresponding element in field[] has been set.
protected boolean isTimeSet Whether time corresponds to the values in fields[].
protected long time A time in milliseconds since January 1, 1970.
Protected Constructors
Calendar()
Constructs a Calendar instance using the default TimeZone and Locale.
Calendar(TimeZone timezone, Locale locale)
Constructs a Calendar instance using the given TimeZone and Locale.
Public Methods
abstract void add(int field, int value)
Adds the given amount to a Calendar field.
boolean after(Object calendar)
Returns whether the Date represented by this Calendar instance is after the Date represented by the parameter.
boolean before(Object calendar)
Returns whether the Date represented by this Calendar instance is before the Date represented by the parameter.
final void clear(int field)
Clears the value in the given time field, marking it unset and assigning it a value of zero.
final void clear()
Clears the values of all the time fields, marking them all unset and assigning them all a value of zero.
Object clone()
Returns a partially deep copy of this Calendar; all fields from from the Calendar class are cloned (deep copy) but fields from subclasses aren't (shallow copy).
int compareTo(Calendar anotherCalendar)
Compares the time represented by this Calendar to that represented by the given Calendar.
boolean equals(Object object)
Compares the given object to this Calendar and returns whether they are equal.
int get(int field)
Returns the value of the given field after computing the field values by calling complete() first.
int getActualMaximum(int field)
Returns the maximum value of the given field for the current date.
int getActualMinimum(int field)
Returns the minimum value of the given field for the current date.
synchronized static Locale[] getAvailableLocales()
Returns an array of locales for which custom Calendar instances are available.
String getDisplayName(int field, int style, Locale locale)
Returns a human-readable string for the value of field using the given style and locale.
Map<StringInteger> getDisplayNames(int field, int style, Locale locale)
Returns a map of human-readable strings to corresponding values, for the given field, style, and locale.
int getFirstDayOfWeek()
Returns the first day of the week for this Calendar.
abstract int getGreatestMinimum(int field)
Returns the greatest minimum value of the given field.
synchronized static Calendar getInstance(Locale locale)
Constructs a new instance of the Calendar subclass appropriate for the given Locale and default TimeZone, set to the current date and time.
synchronized static Calendar getInstance(TimeZone timezone, Locale locale)
Constructs a new instance of the Calendar subclass appropriate for the given Locale and given TimeZone, set to the current date and time.
synchronized static Calendar getInstance(TimeZone timezone)
Constructs a new instance of the Calendar subclass appropriate for the default Locale and given TimeZone, set to the current date and time.
synchronized static Calendar getInstance()
Constructs a new instance of the Calendar subclass appropriate for the default Locale and default TimeZone, set to the current date and time.
abstract int getLeastMaximum(int field)
Returns the smallest maximum value of the given field.
abstract int getMaximum(int field)
Returns the greatest maximum value of the given field.
int getMinimalDaysInFirstWeek()
Returns the minimal days in the first week of the year.
abstract int getMinimum(int field)
Returns the smallest minimum value of the given field.
final Date getTime()
Returns the time of this Calendar as a Date object.
long getTimeInMillis()
Returns the time represented by this Calendar, recomputing the time from its fields if necessary.
TimeZone getTimeZone()
Returns the time zone used by this Calendar.
int hashCode()
Returns an integer hash code for this object.
boolean isLenient()
Tests whether this Calendar accepts field values which are outside the valid range for the field.
final boolean isSet(int field)
Tests whether the given field is set.
void roll(int field, int value)
Adds the given amount to the given field and wraps the value of the field when it goes beyond the maximum or minimum value for the current date.
abstract void roll(int field, boolean increment)
Increment or decrement the given field and wrap the value of the field when it goes beyond the maximum or minimum value for the current date.
void set(int field, int value)
Sets the given field to the given value.
final void set(int year, int month, int day, int hourOfDay, int minute, int second)
Sets the year, month, day of the month, hour of day, minute, and second fields.
final void set(int year, int month, int day)
Sets the year, month, and day of the month fields.
final void set(int year, int month, int day, int hourOfDay, int minute)
Sets the year, month, day of the month, hour of day, and minute fields.
void setFirstDayOfWeek(int value)
Sets the first day of the week for this Calendar.
void setLenient(boolean value)
Sets whether this Calendar accepts field values which are outside the valid range for the field.
void setMinimalDaysInFirstWeek(int value)
Sets the minimal days in the first week of the year.
final void setTime(Date date)
Sets the time of this Calendar.
void setTimeInMillis(long milliseconds)
Sets the time of this Calendar to the given Unix time.
void setTimeZone(TimeZone timezone)
Sets the TimeZone used by this Calendar.
String toString()
Returns a string representation of this Calendar, showing which fields are set.
Protected Methods
void complete()
Computes the time from the fields if the time has not already been set.
abstract void computeFields()
Computes the Calendar fields from time.
abstract void computeTime()
Computes time from the Calendar fields.
final int internalGet(int field)
Returns the value of the given field without recomputing.
[Expand]
Inherited Methods
From class java.lang.Object
From interface java.lang.Comparable

Constants

public static final int ALL_STYLES

Added in API level 9

Requests both SHORT and LONG styles in the map returned by getDisplayNames(int, int, Locale).

Constant Value: 0 (0x00000000)

public static final int AM

Added in API level 1

Value of the AM_PM field indicating the period of the day from midnight to just before noon.

Constant Value: 0 (0x00000000)

public static final int AM_PM

Added in API level 1

Field number for get and set indicating whether the HOUR is before or after noon. E.g., at 10:04:15.250 PM the AM_PM is PM.

See Also
Constant Value: 9 (0x00000009)

public static final int APRIL

Added in API level 1

Value of the MONTH field indicating the fourth month of the year.

Constant Value: 3 (0x00000003)

public static final int AUGUST

Added in API level 1

Value of the MONTH field indicating the eighth month of the year.

Constant Value: 7 (0x00000007)

public static final int DATE

Added in API level 1

Field number for get and set indicating the day of the month. This is a synonym for DAY_OF_MONTH. The first day of the month has value 1.

See Also
Constant Value: 5 (0x00000005)

public static final int DAY_OF_MONTH

Added in API level 1

Field number for get and set indicating the day of the month. This is a synonym for DATE. The first day of the month has value 1.

See Also
Constant Value: 5 (0x00000005)

public static final int DAY_OF_WEEK

Added in API level 1

Field number for get and set indicating the day of the week. This field takes values SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, and SATURDAY.

Constant Value: 7 (0x00000007)

public static final int DAY_OF_WEEK_IN_MONTH

Added in API level 1

Field number for get and set indicating the ordinal number of the day of the week within the current month. Together with the DAY_OF_WEEK field, this uniquely specifies a day within a month. Unlike WEEK_OF_MONTH and WEEK_OF_YEAR, this field's value does not depend on getFirstDayOfWeek() or getMinimalDaysInFirstWeek(). DAY_OF_MONTH 1 through 7 always correspond to DAY_OF_WEEK_IN_MONTH 1; 8 through 15 correspond to DAY_OF_WEEK_IN_MONTH 2, and so on. DAY_OF_WEEK_IN_MONTH 0 indicates the week before DAY_OF_WEEK_IN_MONTH 1. Negative values count back from the end of the month, so the last Sunday of a month is specified as DAY_OF_WEEK = SUNDAY, DAY_OF_WEEK_IN_MONTH = -1. Because negative values count backward they will usually be aligned differently within the month than positive values. For example, if a month has 31 days, DAY_OF_WEEK_IN_MONTH -1 will overlap DAY_OF_WEEK_IN_MONTH 5 and the end of 4.

Constant Value: 8 (0x00000008)

public static final int DAY_OF_YEAR

Added in API level 1

Field number for get and set indicating the day number within the current year. The first day of the year has value 1.

Constant Value: 6 (0x00000006)

public static final int DECEMBER

Added in API level 1

Value of the MONTH field indicating the twelfth month of the year.

Constant Value: 11 (0x0000000b)

public static final int DST_OFFSET

Added in API level 1

Field number for get and set indicating the daylight savings offset from the ZONE_OFFSET in milliseconds. Equivalent to getDSTSavings() if the represented time falls inside DST, or 0 otherwise.

To determine the total offset from GMT at the time represented by this calendar, you will need to add the ZONE_OFFSET and DST_OFFSET fields.

Constant Value: 16 (0x00000010)

public static final int ERA

Added in API level 1

Field number for get and set indicating the era, e.g., AD or BC in the Julian calendar. This is a calendar-specific value; see subclass documentation.

See Also
Constant Value: 0 (0x00000000)

public static final int FEBRUARY

Added in API level 1

Value of the MONTH field indicating the second month of the year.

Constant Value: 1 (0x00000001)

public static final int FIELD_COUNT

Added in API level 1

This is the total number of fields in this calendar.

Constant Value: 17 (0x00000011)

public static final int FRIDAY

Added in API level 1

Value of the DAY_OF_WEEK field indicating Friday.

Constant Value: 6 (0x00000006)

public static final int HOUR

Added in API level 1

Field number for get and set indicating the hour of the morning or afternoon. HOUR is used for the 12-hour clock. E.g., at 10:04:15.250 PM the HOUR is 10.

See Also
Constant Value: 10 (0x0000000a)

public static final int HOUR_OF_DAY

Added in API level 1

Field number for get and set indicating the hour of the day. HOUR_OF_DAY is used for the 24-hour clock. E.g., at 10:04:15.250 PM the HOUR_OF_DAY is 22.

See Also
Constant Value: 11 (0x0000000b)

public static final int JANUARY

Added in API level 1

Value of the MONTH field indicating the first month of the year.

Constant Value: 0 (0x00000000)

public static final int JULY

Added in API level 1

Value of the MONTH field indicating the seventh month of the year.

Constant Value: 6 (0x00000006)

public static final int JUNE

Added in API level 1

Value of the MONTH field indicating the sixth month of the year.

Constant Value: 5 (0x00000005)

public static final int LONG

Added in API level 9

Requests long names (such as "January") from getDisplayName(int, int, Locale) or getDisplayNames(int, int, Locale).

Constant Value: 2 (0x00000002)

public static final int MARCH

Added in API level 1

Value of the MONTH field indicating the third month of the year.

Constant Value: 2 (0x00000002)

public static final int MAY

Added in API level 1

Value of the MONTH field indicating the fifth month of the year.

Constant Value: 4 (0x00000004)

public static final int MILLISECOND

Added in API level 1

Field number for get and set indicating the millisecond within the second. E.g., at 10:04:15.250 PM the MILLISECOND is 250.

Constant Value: 14 (0x0000000e)

public static final int MINUTE

Added in API level 1

Field number for get and set indicating the minute within the hour. E.g., at 10:04:15.250 PM the MINUTE is 4.

Constant Value: 12 (0x0000000c)

public static final int MONDAY

Added in API level 1

Value of the DAY_OF_WEEK field indicating Monday.

Constant Value: 2 (0x00000002)

public static final int MONTH

Added in API level 1

Field number for get and set indicating the month. This is a calendar-specific value. The first month of the year is JANUARY; the last depends on the number of months in a year.

Constant Value: 2 (0x00000002)

public static final int NOVEMBER

Added in API level 1

Value of the MONTH field indicating the eleventh month of the year.

Constant Value: 10 (0x0000000a)

public static final int OCTOBER

Added in API level 1

Value of the MONTH field indicating the tenth month of the year.

Constant Value: 9 (0x00000009)

public static final int PM

Added in API level 1

Value of the AM_PM field indicating the period of the day from noon to just before midnight.

Constant Value: 1 (0x00000001)

public static final int SATURDAY

Added in API level 1

Value of the DAY_OF_WEEK field indicating Saturday.

Constant Value: 7 (0x00000007)

public static final int SECOND

Added in API level 1

Field number for get and set indicating the second within the minute. E.g., at 10:04:15.250 PM the SECOND is 15.

Constant Value: 13 (0x0000000d)

public static final int SEPTEMBER

Added in API level 1

Value of the MONTH field indicating the ninth month of the year.

Constant Value: 8 (0x00000008)

public static final int SHORT

Added in API level 9

Requests short names (such as "Jan") from getDisplayName(int, int, Locale) or getDisplayNames(int, int, Locale).

Constant Value: 1 (0x00000001)

public static final int SUNDAY

Added in API level 1

Value of the DAY_OF_WEEK field indicating Sunday.

Constant Value: 1 (0x00000001)

public static final int THURSDAY

Added in API level 1

Value of the DAY_OF_WEEK field indicating Thursday.

Constant Value: 5 (0x00000005)

public static final int TUESDAY

Added in API level 1

Value of the DAY_OF_WEEK field indicating Tuesday.

Constant Value: 3 (0x00000003)

public static final int UNDECIMBER

Added in API level 1

Value of the MONTH field indicating the thirteenth month of the year. Although GregorianCalendar does not use this value, lunar calendars do.

Constant Value: 12 (0x0000000c)

public static final int WEDNESDAY

Added in API level 1

Value of the DAY_OF_WEEK field indicating Wednesday.

Constant Value: 4 (0x00000004)

public static final int WEEK_OF_MONTH

Added in API level 1

Field number for get and set indicating the week number within the current month. The first week of the month, as defined by getFirstDayOfWeek() and getMinimalDaysInFirstWeek(), has value 1. Subclasses define the value of WEEK_OF_MONTH for days before the first week of the month.

Constant Value: 4 (0x00000004)

public static final int WEEK_OF_YEAR

Added in API level 1

Field number for get and set indicating the week number within the current year. The first week of the year, as defined by getFirstDayOfWeek() and getMinimalDaysInFirstWeek(), has value 1. Subclasses define the value of WEEK_OF_YEAR for days before the first week of the year.

Constant Value: 3 (0x00000003)

public static final int YEAR

Added in API level 1

Field number for get and set indicating the year. This is a calendar-specific value; see subclass documentation.

Constant Value: 1 (0x00000001)

public static final int ZONE_OFFSET

Added in API level 1

Field number for get and set indicating the raw (non-DST) offset from GMT in milliseconds. Equivalent to getRawOffset().

To determine the total offset from GMT at the time represented by this calendar, you will need to add the ZONE_OFFSET and DST_OFFSET fields.

Constant Value: 15 (0x0000000f)

Fields

protected boolean areFieldsSet

Added in API level 1

True iff the values in fields[] correspond to time. Despite the name, this is effectively "are the values in fields[] up-to-date?" --- fields[] may contain non-zero values and isSet[] may contain true values even when areFieldsSet is false. Accessing the fields via get will ensure the fields are up-to-date.

protected int[] fields

Added in API level 1

Contains broken-down field values for the current value of time if areFieldsSet is true, or stale data corresponding to some previous value otherwise. Accessing the fields via get will ensure the fields are up-to-date. The array length is always FIELD_COUNT.

protected boolean[] isSet

Added in API level 1

Whether the corresponding element in field[] has been set. Initially, these are all false. The first time the fields are computed, these are set to true and remain set even if the data becomes stale: you must check areFieldsSet if you want to know whether the value is up-to-date. Note that isSet is not a safe alternative to accessing this array directly, and will likewise return stale data! The array length is always FIELD_COUNT.

protected boolean isTimeSet

Added in API level 1

Whether time corresponds to the values in fields[]. If false, time is out-of-date with respect to changes fields[]. Accessing the time via getTimeInMillis will always return the correct value.

protected long time

Added in API level 1

A time in milliseconds since January 1, 1970. See isTimeSet. Accessing the time via getTimeInMillis will always return the correct value.

Protected Constructors

protected Calendar ()

Added in API level 1

Constructs a Calendar instance using the default TimeZone and Locale.

protected Calendar (TimeZone timezone, Locale locale)

Added in API level 1

Constructs a Calendar instance using the given TimeZone and Locale.

Public Methods

public abstract void add (int field, int value)

Added in API level 1

Adds the given amount to a Calendar field.

Parameters
field the Calendar field to modify.
value the amount to add to the field.
Throws
IllegalArgumentException if field is DST_OFFSET or ZONE_OFFSET.

public boolean after (Object calendar)

Added in API level 1

Returns whether the Date represented by this Calendar instance is after the Date represented by the parameter. The comparison is not dependent on the time zones of the Calendar.

Parameters
calendar the Calendar instance to compare.
Returns
  • true when this Calendar is after calendar, false otherwise.
Throws
IllegalArgumentException if the time is not set and the time cannot be computed from the current field values.

public boolean before (Object calendar)

Added in API level 1

Returns whether the Date represented by this Calendar instance is before the Date represented by the parameter. The comparison is not dependent on the time zones of the Calendar.

Parameters
calendar the Calendar instance to compare.
Returns
  • true when this Calendar is before calendar, false otherwise.
Throws
IllegalArgumentException if the time is not set and the time cannot be computed from the current field values.

public final void clear (int field)

Added in API level 1

Clears the value in the given time field, marking it unset and assigning it a value of zero. The actual field value will be determined the next time the field is accessed.

public final void clear ()

Added in API level 1

Clears the values of all the time fields, marking them all unset and assigning them all a value of zero. The actual field values will be determined the next time the fields are accessed.

public Object clone ()

Added in API level 1

Returns a partially deep copy of this Calendar; all fields from from the Calendar class are cloned (deep copy) but fields from subclasses aren't (shallow copy).

Returns
  • a copy of this object.

public int compareTo (Calendar anotherCalendar)

Added in API level 1

Compares the time represented by this Calendar to that represented by the given Calendar.

Returns
  • 0 if the times of the two Calendars are equal, -1 if the time of this Calendar is before the other one, 1 if the time of this Calendar is after the other one.
Throws
NullPointerException if the argument is null.
IllegalArgumentException if the argument does not include a valid time value.

public boolean equals (Object object)

Added in API level 1

Compares the given object to this Calendar and returns whether they are equal. The object must be an instance of Calendar and have the same properties.

Parameters
object the object to compare this instance with.
Returns
  • true if the given object is equal to this Calendar, false otherwise.

public int get (int field)

Added in API level 1

Returns the value of the given field after computing the field values by calling complete() first.

Throws
IllegalArgumentException if the fields are not set, the time is not set, and the time cannot be computed from the current field values.
ArrayIndexOutOfBoundsException if the field is not inside the range of possible fields. The range is starting at 0 up to FIELD_COUNT.

public int getActualMaximum (int field)

Added in API level 1

Returns the maximum value of the given field for the current date. For example, the maximum number of days in the current month.

public int getActualMinimum (int field)

Added in API level 1

Returns the minimum value of the given field for the current date.

public static synchronized Locale[] getAvailableLocales ()

Added in API level 1

Returns an array of locales for which custom Calendar instances are available.

Note that Android does not support user-supplied locale service providers.

public String getDisplayName (int field, int style, Locale locale)

Added in API level 9

Returns a human-readable string for the value of field using the given style and locale. If no string is available, returns null. The value is retrieved by invoking get(field).

For example, getDisplayName(MONTH, SHORT, Locale.US) will return "Jan" while getDisplayName(MONTH, LONG, Locale.US) will return "January".

Parameters
field the field
style SHORT or LONG
locale the locale
Returns
  • the display name, or null
Throws
NullPointerException if locale == null
IllegalArgumentException if field or style is invalid

public Map<StringInteger> getDisplayNames (int field, int style, Locale locale)

Added in API level 9

Returns a map of human-readable strings to corresponding values, for the given field, style, and locale. Returns null if no strings are available.

For example, getDisplayNames(MONTH, ALL_STYLES, Locale.US) would contain mappings from "Jan" and "January" to JANUARY, and so on.

Parameters
field the field
style SHORT, LONG, or ALL_STYLES
locale the locale
Returns
  • the display name, or null
Throws
NullPointerException if locale == null
IllegalArgumentException if field or style is invalid

public int getFirstDayOfWeek ()

Added in API level 1

Returns the first day of the week for this Calendar.

public abstract int getGreatestMinimum (int field)

Added in API level 1

Returns the greatest minimum value of the given field. This is the biggest value that getActualMinimum can return for any possible time.

public static synchronized Calendar getInstance (Locale locale)

Added in API level 1

Constructs a new instance of the Calendar subclass appropriate for the given Locale and default TimeZone, set to the current date and time.

public static synchronized Calendar getInstance (TimeZone timezone, Locale locale)

Added in API level 1

Constructs a new instance of the Calendar subclass appropriate for the given Locale and given TimeZone, set to the current date and time.

public static synchronized Calendar getInstance (TimeZone timezone)

Added in API level 1

Constructs a new instance of the Calendar subclass appropriate for the default Locale and given TimeZone, set to the current date and time.

public static synchronized Calendar getInstance ()

Added in API level 1

Constructs a new instance of the Calendar subclass appropriate for the default Locale and default TimeZone, set to the current date and time.

public abstract int getLeastMaximum (int field)

Added in API level 1

Returns the smallest maximum value of the given field. This is the smallest value that getActualMaximum() can return for any possible time.

public abstract int getMaximum (int field)

Added in API level 1

Returns the greatest maximum value of the given field. This returns the biggest value that get can return for the given field.

public int getMinimalDaysInFirstWeek ()

Added in API level 1

Returns the minimal days in the first week of the year.

public abstract int getMinimum (int field)

Added in API level 1

Returns the smallest minimum value of the given field. this returns the smallest value that get can return for the given field.

public final Date getTime ()

Added in API level 1

Returns the time of this Calendar as a Date object.

Throws
IllegalArgumentException if the time is not set and the time cannot be computed from the current field values.

public long getTimeInMillis ()

Added in API level 1

Returns the time represented by this Calendar, recomputing the time from its fields if necessary.

Throws
IllegalArgumentException if the time is not set and the time cannot be computed from the current field values.

public TimeZone getTimeZone ()

Added in API level 1

Returns the time zone used by this Calendar.

public int hashCode ()

Added in API level 1

Returns an integer hash code for this object. By contract, any two objects for which equals(Object) returns true must return the same hash code value. This means that subclasses of Object usually override both methods or neither method.

Note that hash values must not change over time unless information used in equals comparisons also changes.

See Writing a correct hashCode method if you intend implementing your own hashCode method.

Returns
  • this object's hash code.

public boolean isLenient ()

Added in API level 1

Tests whether this Calendar accepts field values which are outside the valid range for the field.

public final boolean isSet (int field)

Added in API level 1

Tests whether the given field is set. Note that the interpretation of "is set" is somewhat technical. In particular, it does not mean that the field's value is up to date. If you want to know whether a field contains an up-to-date value, you must also check areFieldsSet, making this method somewhat useless unless you're a subclass, in which case you can access the isSet array directly.

A field remains "set" from the first time its value is computed until it's cleared by one of the clear methods. Thus "set" does not mean "valid". You probably want to call get -- which will update fields as necessary -- rather than try to make use of this method.

public void roll (int field, int value)

Added in API level 1

Adds the given amount to the given field and wraps the value of the field when it goes beyond the maximum or minimum value for the current date. Other fields will be adjusted as required to maintain a consistent date.

public abstract void roll (int field, boolean increment)

Added in API level 1

Increment or decrement the given field and wrap the value of the field when it goes beyond the maximum or minimum value for the current date. Other fields will be adjusted as required to maintain a consistent date.

public void set (int field, int value)

Added in API level 1

Sets the given field to the given value.

public final void set (int year, int month, int day, int hourOfDay, int minute, int second)

Added in API level 1

Sets the year, month, day of the month, hour of day, minute, and second fields. Other fields are not changed; call clear() first if this is not desired. The month value is 0-based, so it may be clearer to use a constant like JANUARY.

public final void set (int year, int month, int day)

Added in API level 1

Sets the year, month, and day of the month fields. Other fields are not changed; call clear() first if this is not desired. The month value is 0-based, so it may be clearer to use a constant like JANUARY.

public final void set (int year, int month, int day, int hourOfDay, int minute)

Added in API level 1

Sets the year, month, day of the month, hour of day, and minute fields. Other fields are not changed; call clear() first if this is not desired. The month value is 0-based, so it may be clearer to use a constant like JANUARY.

public void setFirstDayOfWeek (int value)

Added in API level 1

Sets the first day of the week for this Calendar. The value should be a day of the week such as MONDAY.

public void setLenient (boolean value)

Added in API level 1

Sets whether this Calendar accepts field values which are outside the valid range for the field.

public void setMinimalDaysInFirstWeek (int value)

Added in API level 1

Sets the minimal days in the first week of the year.

public final void setTime (Date date)

Added in API level 1

Sets the time of this Calendar.

public void setTimeInMillis (long milliseconds)

Added in API level 1

Sets the time of this Calendar to the given Unix time. See Date for more about what this means.

public void setTimeZone (TimeZone timezone)

Added in API level 1

Sets the TimeZone used by this Calendar.

public String toString ()

Added in API level 1

Returns a string representation of this Calendar, showing which fields are set.

Returns
  • a printable representation of this object.

Protected Methods

protected void complete ()

Added in API level 1

Computes the time from the fields if the time has not already been set. Computes the fields from the time if the fields are not already set.

Throws
IllegalArgumentException if the time is not set and the time cannot be computed from the current field values.

protected abstract void computeFields ()

Added in API level 1

Computes the Calendar fields from time.

protected abstract void computeTime ()

Added in API level 1

Computes time from the Calendar fields.

Throws
IllegalArgumentException if the time cannot be computed from the current field values.

protected final int internalGet (int field)

Added in API level 1

Returns the value of the given field without recomputing.