Calendar Class
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
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
- 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 < 00:00 on Jan 1, 2000 < 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) < 12:01 am, and 12:00 pm (noon) < 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 |
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 |
Am |
Value of the |
AmPm |
Obsolete.
Field number for |
April |
Value of the |
August |
Value of the |
Date |
Obsolete.
Field number for |
DayOfMonth |
Obsolete.
Field number for |
DayOfWeek |
Obsolete.
Field number for |
DayOfWeekInMonth |
Obsolete.
Field number for |
DayOfYear |
Obsolete.
Field number for |
December |
Value of the |
DstOffset |
Obsolete.
Field number for |
Era |
Obsolete.
Field number for |
February |
Value of the |
FieldCount |
The number of distinct fields recognized by |
Friday |
Value of the |
Hour |
Obsolete.
Field number for |
HourOfDay |
Obsolete.
Field number for |
January |
Value of the |
July |
Value of the |
June |
Value of the |
Long |
Obsolete.
A style specifier for |
LongFormat |
A style specifier for |
LongStandalone |
A style specifier for |
March |
Value of the |
May |
Value of the |
Millisecond |
Obsolete.
Field number for |
Minute |
Obsolete.
Field number for |
Monday |
Value of the |
Month |
Obsolete.
Field number for |
NarrowFormat |
A style specifier for |
NarrowStandalone |
A style specifier for |
November |
Value of the |
October |
Value of the |
Pm |
Value of the |
Saturday |
Value of the |
Second |
Obsolete.
Field number for |
September |
Value of the |
Short |
Obsolete.
A style specifier for |
ShortFormat |
A style specifier for |
ShortStandalone |
A style specifier for |
Sunday |
Value of the |
Thursday |
Value of the |
Tuesday |
Value of the |
Undecimber |
Value of the |
Wednesday |
Value of the |
WeekOfMonth |
Obsolete.
Field number for |
WeekOfYear |
Obsolete.
Field number for |
Year |
Obsolete.
Field number for |
ZoneOffset |
Obsolete.
Field number for |
Properties
AreFieldsSet |
True if |
AvailableCalendarTypes |
Returns an unmodifiable |
CalendarType |
Returns the calendar type of this |
Class |
Returns the runtime class of this |
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 |
IsWeekDateSupported |
Returns whether this |
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 |
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
|
WeekYear |
Returns the week year represented by this |
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 |
Before(Object) |
Returns whether this |
Clear() |
Sets all the calendar field values and the time value
(millisecond offset from the "#Epoch">Epoch) of
this |
Clear(CalendarField) |
Sets the given calendar field value and the time value
(millisecond offset from the "#Epoch">Epoch) of
this |
Clone() |
Creates and returns a copy of this object. |
CompareTo(Calendar) |
Compares the time values (millisecond offsets from the "#Epoch">Epoch) represented by two
|
Complete() |
Fills in any unset fields in the calendar fields. |
ComputeFields() |
Converts the current millisecond time value |
ComputeTime() |
Converts the current calendar field values in |
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
|
GetActualMinimum(CalendarField) |
Returns the minimum value that the specified calendar field
could have, given the time value of this |
GetAvailableLocales() |
Returns an array of all locales for which the |
GetDisplayName(Int32, Int32, Locale) |
Returns the string representation of the calendar
|
GetDisplayNames(Int32, Int32, Locale) |
Returns a |
GetGreatestMinimum(CalendarField) |
Returns the highest minimum value for the given calendar field
of this |
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 |
GetMaximum(CalendarField) |
Returns the maximum value for the given calendar field of this
|
GetMinimum(CalendarField) |
Returns the minimum value for the given calendar field of this
|
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 |
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 |
Set(Int32, Int32, Int32, Int32, Int32) |
Sets the values for the calendar fields |
Set(Int32, Int32, Int32) |
Sets the values for the calendar fields |
SetHandle(IntPtr, JniHandleOwnership) |
Sets the Handle property. (Inherited from Object) |
SetWeekDate(Int32, Int32, Int32) |
Sets the date of this |
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) |