Compartilhar via


Calendar Class

Definition

The Calendar class is an abstract class that provides methods for converting between a specific instant in time and a set of #fields calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and so on, and for manipulating the calendar fields, such as getting the date of the next week.

[Android.Runtime.Register("java/util/Calendar", DoNotGenerateAcw=true)]
public abstract class Calendar : Java.Lang.Object, IDisposable, Java.Interop.IJavaPeerable, Java.IO.ISerializable, Java.Lang.ICloneable, Java.Lang.IComparable
[<Android.Runtime.Register("java/util/Calendar", DoNotGenerateAcw=true)>]
type Calendar = class
    inherit Object
    interface ISerializable
    interface IJavaObject
    interface IDisposable
    interface IJavaPeerable
    interface ICloneable
    interface IComparable
Inheritance
Calendar
Derived
Attributes
Implements

Remarks

The Calendar class is an abstract class that provides methods for converting between a specific instant in time and a set of #fields calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and so on, and for manipulating the calendar fields, such as getting the date of the next week. An instant in time can be represented by a millisecond value that is an offset from the "Epoch"><em>Epoch</em>, January 1, 1970 00:00:00.000 GMT (Gregorian).

The class also provides additional fields and methods for implementing a concrete calendar system outside the package. Those fields and methods are defined as protected.

Like other locale-sensitive classes, Calendar provides a class method, getInstance, for getting a generally useful object of this type. Calendar's getInstance method returns a Calendar object whose calendar fields have been initialized with the current date and time: <blockquote>

Calendar rightNow = Calendar.getInstance();

</blockquote>

A Calendar object can produce all the calendar 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 calendar fields, as well as their meaning. For example, the first month of the calendar system has value MONTH == JANUARY for all calendars. Other values are defined by the concrete subclass, such as ERA. See individual field documentation and subclass documentation for details.

<h2>Getting and Setting Calendar Field Values</h2>

The calendar field values can be set by calling the set methods. Any field values set in a Calendar will not be interpreted until it needs to calculate its time value (milliseconds from the Epoch) or values of the calendar fields. Calling the get, getTimeInMillis, getTime, add and roll involves such calculation.

<h3>Leniency</h3>

Calendar has two modes for interpreting the calendar fields, <em>lenient</em> and <em>non-lenient</em>. When a Calendar is in lenient mode, it accepts a wider range of calendar field values than it produces. When a Calendar recomputes calendar field values for return by get(), all of the calendar fields are normalized. For example, a lenient GregorianCalendar interprets MONTH == JANUARY, DAY_OF_MONTH == 32 as February 1.

When a Calendar is in non-lenient mode, it throws an exception if there is any inconsistency in its calendar fields. For example, a GregorianCalendar always produces DAY_OF_MONTH values between 1 and the length of the month. A non-lenient GregorianCalendar throws an exception upon calculating its time or calendar field values if any out-of-range field value has been set.

<h3>"first_week">First Week</h3>

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 or the locale itself when a Calendar is constructed. If the designated locale contains "fw" Unicode extensions, the first day of the week will be obtained according to those extensions. They may also be specified explicitly through the methods for setting their values.

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 <i>n</i> of the previous year.

<h3>Calendar Fields Resolution</h3>

When computing a date and time from the calendar fields, there may be insufficient information for the computation (such as only year and month with no day of month), or there may be inconsistent information (such as Tuesday, July 15, 1996 (Gregorian) -- July 15, 1996 is actually a Monday). Calendar will resolve calendar field values to determine the date and time in the following way.

"resolution">If there is any conflict in calendar field values, <c>Calendar</c> gives priorities to calendar fields that have been set more recently. The following are the default combinations of the calendar fields. The most recent combination, as determined by the most recently set single field, will be used.

"date_resolution">For the date fields: <blockquote>

YEAR + MONTH + DAY_OF_MONTH
            YEAR + MONTH + WEEK_OF_MONTH + DAY_OF_WEEK
            YEAR + MONTH + DAY_OF_WEEK_IN_MONTH + DAY_OF_WEEK
            YEAR + DAY_OF_YEAR
            YEAR + DAY_OF_WEEK + WEEK_OF_YEAR

</blockquote>

"time_resolution">For the time of day fields: <blockquote>

HOUR_OF_DAY
            AM_PM + HOUR

</blockquote>

If there are any calendar fields whose values haven't been set in the selected field combination, Calendar uses their default values. The default value of each field may vary by concrete calendar systems. For example, in GregorianCalendar, the default of a field is the same as that of the start of the Epoch: i.e., YEAR = 1970, MONTH = JANUARY, DAY_OF_MONTH = 1, etc.

<strong>Note:</strong> There are certain possible ambiguities in interpretation of certain singular times, which are resolved in the following ways: <ol> <li> 23:59 is the last minute of the day and 00:00 is the first minute of the next day. Thus, 23:59 on Dec 31, 1999 &lt; 00:00 on Jan 1, 2000 &lt; 00:01 on Jan 1, 2000.

<li> Although historically not precise, midnight also belongs to "am", and noon belongs to "pm", so on the same day, 12:00 am (midnight) &lt; 12:01 am, and 12:00 pm (noon) &lt; 12:01 pm </ol>

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.

<h3>Field Manipulation</h3>

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

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

<em>Example</em>: Consider a GregorianCalendar originally set to August 31, 1999. Calling set(Calendar.MONTH, Calendar.SEPTEMBER) sets the date 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 date to September 30, 1999, since no recomputation occurs after set() itself.

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

<blockquote>

<strong>Add rule 1</strong>. 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.

<strong>Add rule 2</strong>. 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 or other constraints, such as time zone offset changes, 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.

</blockquote>

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

<em>Example</em>: Consider a GregorianCalendar originally set to August 31, 1999. Calling add(Calendar.MONTH, 13) sets the calendar to September 30, 2000. <strong>Add rule 1</strong> 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, <strong>add rule 2</strong> 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.

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

<blockquote>

<strong>Roll rule</strong>. 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.

</blockquote>

<em>Example</em>: See java.util.GregorianCalendar#roll(int, int).

<strong>Usage model</strong>. 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.

Added in 1.1.

Java documentation for java.util.Calendar.

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Constructors

Calendar()

Constructs a Calendar with the default time zone and the default java.util.Locale.Category#FORMAT FORMAT locale.

Calendar(IntPtr, JniHandleOwnership)

A constructor used when creating managed representations of JNI objects; called by the runtime.

Calendar(TimeZone, Locale)

Constructs a calendar with the specified time zone and locale.

Fields

AllStyles
Obsolete.

A style specifier for #getDisplayNames(int, int, Locale) getDisplayNames indicating names in all styles, such as "January" and "Jan".

Am

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

AmPm
Obsolete.

Field number for get and set indicating whether the HOUR is before or after noon.

April

Value of the #MONTH field indicating the fourth month of the year in the Gregorian and Julian calendars.

August

Value of the #MONTH field indicating the eighth month of the year in the Gregorian and Julian calendars.

Date
Obsolete.

Field number for get and set indicating the day of the month.

DayOfMonth
Obsolete.

Field number for get and set indicating the day of the month.

DayOfWeek
Obsolete.

Field number for get and set indicating the day of the week.

DayOfWeekInMonth
Obsolete.

Field number for get and set indicating the ordinal number of the day of the week within the current month.

DayOfYear
Obsolete.

Field number for get and set indicating the day number within the current year.

December

Value of the #MONTH field indicating the twelfth month of the year in the Gregorian and Julian calendars.

DstOffset
Obsolete.

Field number for get and set indicating the daylight saving offset in milliseconds.

Era
Obsolete.

Field number for get and set indicating the era, e.

February

Value of the #MONTH field indicating the second month of the year in the Gregorian and Julian calendars.

FieldCount

The number of distinct fields recognized by get and set.

Friday

Value of the #DAY_OF_WEEK field indicating Friday.

Hour
Obsolete.

Field number for get and set indicating the hour of the morning or afternoon.

HourOfDay
Obsolete.

Field number for get and set indicating the hour of the day.

January

Value of the #MONTH field indicating the first month of the year in the Gregorian and Julian calendars.

July

Value of the #MONTH field indicating the seventh month of the year in the Gregorian and Julian calendars.

June

Value of the #MONTH field indicating the sixth month of the year in the Gregorian and Julian calendars.

Long
Obsolete.

A style specifier for #getDisplayName(int, int, Locale) getDisplayName and #getDisplayNames(int, int, Locale) getDisplayNames equivalent to #LONG_FORMAT.

LongFormat

A style specifier for #getDisplayName(int, int, Locale) getDisplayName and #getDisplayNames(int, int, Locale) getDisplayNames indicating a long name used for format.

LongStandalone

A style specifier for #getDisplayName(int, int, Locale) getDisplayName and #getDisplayNames(int, int, Locale) getDisplayNames indicating a long name used independently, such as a month name as calendar headers.

March

Value of the #MONTH field indicating the third month of the year in the Gregorian and Julian calendars.

May

Value of the #MONTH field indicating the fifth month of the year in the Gregorian and Julian calendars.

Millisecond
Obsolete.

Field number for get and set indicating the millisecond within the second.

Minute
Obsolete.

Field number for get and set indicating the minute within the hour.

Monday

Value of the #DAY_OF_WEEK field indicating Monday.

Month
Obsolete.

Field number for get and set indicating the month.

NarrowFormat

A style specifier for #getDisplayName(int, int, Locale) getDisplayName and #getDisplayNames(int, int, Locale) getDisplayNames indicating a narrow name used for format.

NarrowStandalone

A style specifier for #getDisplayName(int, int, Locale) getDisplayName and #getDisplayNames(int, int, Locale) getDisplayNames indicating a narrow name independently.

November

Value of the #MONTH field indicating the eleventh month of the year in the Gregorian and Julian calendars.

October

Value of the #MONTH field indicating the tenth month of the year in the Gregorian and Julian calendars.

Pm

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

Saturday

Value of the #DAY_OF_WEEK field indicating Saturday.

Second
Obsolete.

Field number for get and set indicating the second within the minute.

September

Value of the #MONTH field indicating the ninth month of the year in the Gregorian and Julian calendars.

Short
Obsolete.

A style specifier for #getDisplayName(int, int, Locale) getDisplayName and #getDisplayNames(int, int, Locale) getDisplayNames equivalent to #SHORT_FORMAT.

ShortFormat

A style specifier for #getDisplayName(int, int, Locale) getDisplayName and #getDisplayNames(int, int, Locale) getDisplayNames indicating a short name used for format.

ShortStandalone

A style specifier for #getDisplayName(int, int, Locale) getDisplayName and #getDisplayNames(int, int, Locale) getDisplayNames indicating a short name used independently, such as a month abbreviation as calendar headers.

Sunday

Value of the #DAY_OF_WEEK field indicating Sunday.

Thursday

Value of the #DAY_OF_WEEK field indicating Thursday.

Tuesday

Value of the #DAY_OF_WEEK field indicating Tuesday.

Undecimber

Value of the #MONTH field indicating the thirteenth month of the year.

Wednesday

Value of the #DAY_OF_WEEK field indicating Wednesday.

WeekOfMonth
Obsolete.

Field number for get and set indicating the week number within the current month.

WeekOfYear
Obsolete.

Field number for get and set indicating the week number within the current year.

Year
Obsolete.

Field number for get and set indicating the year.

ZoneOffset
Obsolete.

Field number for get and set indicating the raw offset from GMT in milliseconds.

Properties

AreFieldsSet

True if fields[] are in sync with the currently set time.

AvailableCalendarTypes

Returns an unmodifiable Set containing all calendar types supported by Calendar in the runtime environment.

CalendarType

Returns the calendar type of this Calendar.

Class

Returns the runtime class of this Object.

(Inherited from Object)
Fields

The calendar field values for the currently set time for this calendar.

FirstDayOfWeek

Gets what the first day of the week is; e.

Handle

The handle to the underlying Android instance.

(Inherited from Object)
Instance

Gets a calendar using the default time zone and locale.

IsTimeSet

True if then the value of time is valid.

IsWeekDateSupported

Returns whether this Calendar supports week dates.

JniIdentityHashCode (Inherited from Object)
JniPeerMembers
Lenient

Tells whether date/time interpretation is to be lenient.

MinimalDaysInFirstWeek

Gets what the minimal days required in the first week of the year are; e.

PeerReference (Inherited from Object)
ThresholdClass

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

ThresholdType

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

Time

Returns a Date object representing this Calendar's time value (millisecond offset from the "#Epoch">Epoch"). -or- Sets this Calendar's time with the given Date.

TimeInMillis

Returns this Calendar's time value in milliseconds.

TimeZone

Gets the time zone. -or- Sets the time zone with the given time zone value.

WeeksInWeekYear

Returns the number of weeks in the week year represented by this Calendar.

WeekYear

Returns the week year represented by this Calendar.

Methods

Add(CalendarField, Int32)

Adds or subtracts the specified amount of time to the given calendar field, based on the calendar's rules.

After(Object)

Returns whether this Calendar represents a time after the time represented by the specified Object.

Before(Object)

Returns whether this Calendar represents a time before the time represented by the specified Object.

Clear()

Sets all the calendar field values and the time value (millisecond offset from the "#Epoch">Epoch) of this Calendar undefined.

Clear(CalendarField)

Sets the given calendar field value and the time value (millisecond offset from the "#Epoch">Epoch) of this Calendar undefined.

Clone()

Creates and returns a copy of this object.

CompareTo(Calendar)

Compares the time values (millisecond offsets from the "#Epoch">Epoch) represented by two Calendar objects.

Complete()

Fills in any unset fields in the calendar fields.

ComputeFields()

Converts the current millisecond time value #time to calendar field values in #fields fields[].

ComputeTime()

Converts the current calendar field values in #fields fields[] to the millisecond time value #time.

Dispose() (Inherited from Object)
Dispose(Boolean) (Inherited from Object)
Equals(Object)

Indicates whether some other object is "equal to" this one.

(Inherited from Object)
Get(CalendarField)

Returns the value of the given calendar field.

GetActualMaximum(CalendarField)

Returns the maximum value that the specified calendar field could have, given the time value of this Calendar.

GetActualMinimum(CalendarField)

Returns the minimum value that the specified calendar field could have, given the time value of this Calendar.

GetAvailableLocales()

Returns an array of all locales for which the getInstance methods of this class can return localized instances.

GetDisplayName(Int32, Int32, Locale)

Returns the string representation of the calendar field value in the given style and locale.

GetDisplayNames(Int32, Int32, Locale)

Returns a Map containing all names of the calendar field in the given style and locale and their corresponding field values.

GetGreatestMinimum(CalendarField)

Returns the highest minimum value for the given calendar field of this Calendar instance.

GetHashCode()

Returns a hash code value for the object.

(Inherited from Object)
GetInstance(Locale)

Gets a calendar using the default time zone and specified locale.

GetInstance(TimeZone, Locale)

Gets a calendar with the specified time zone and locale.

GetInstance(TimeZone)

Gets a calendar using the specified time zone and default locale.

GetLeastMaximum(CalendarField)

Returns the lowest maximum value for the given calendar field of this Calendar instance.

GetMaximum(CalendarField)

Returns the maximum value for the given calendar field of this Calendar instance.

GetMinimum(CalendarField)

Returns the minimum value for the given calendar field of this Calendar instance.

InternalGet(Int32)

Returns the value of the given calendar field.

IsSet(CalendarField)

Determines if the given calendar field has a value set, including cases that the value has been set by internal fields calculations triggered by a get method call.

JavaFinalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

(Inherited from Object)
Notify()

Wakes up a single thread that is waiting on this object's monitor.

(Inherited from Object)
NotifyAll()

Wakes up all threads that are waiting on this object's monitor.

(Inherited from Object)
Roll(CalendarField, Boolean)

Adds or subtracts (up/down) a single unit of time on the given time field without changing larger fields.

Roll(CalendarField, Int32)

Adds the specified (signed) amount to the specified calendar field without changing larger fields.

Set(CalendarField, Int32)

Sets the given calendar field to the given value.

Set(Int32, Int32, Int32, Int32, Int32, Int32)

Sets the values for the fields YEAR, MONTH, DAY_OF_MONTH, HOUR_OF_DAY, MINUTE, and SECOND.

Set(Int32, Int32, Int32, Int32, Int32)

Sets the values for the calendar fields YEAR, MONTH, DAY_OF_MONTH, HOUR_OF_DAY, and MINUTE.

Set(Int32, Int32, Int32)

Sets the values for the calendar fields YEAR, MONTH, and DAY_OF_MONTH.

SetHandle(IntPtr, JniHandleOwnership)

Sets the Handle property.

(Inherited from Object)
SetWeekDate(Int32, Int32, Int32)

Sets the date of this Calendar with the the given date specifiers - week year, week of year, and day of week.

ToArray<T>() (Inherited from Object)
ToInstant()
ToString()

Returns a string representation of the object.

(Inherited from Object)
UnregisterFromRuntime() (Inherited from Object)
Wait()

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>.

(Inherited from Object)
Wait(Int64, Int32)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)
Wait(Int64)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)

Explicit Interface Implementations

IComparable.CompareTo(Object)
IJavaPeerable.Disposed() (Inherited from Object)
IJavaPeerable.DisposeUnlessReferenced() (Inherited from Object)
IJavaPeerable.Finalized() (Inherited from Object)
IJavaPeerable.JniManagedPeerState (Inherited from Object)
IJavaPeerable.SetJniIdentityHashCode(Int32) (Inherited from Object)
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) (Inherited from Object)
IJavaPeerable.SetPeerReference(JniObjectReference) (Inherited from Object)

Extension Methods

JavaCast<TResult>(IJavaObject)

Performs an Android runtime-checked type conversion.

JavaCast<TResult>(IJavaObject)
GetJniTypeName(IJavaPeerable)

Applies to