GregorianCalendar 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.
GregorianCalendar
is a concrete subclass of
Calendar
and provides the standard calendar system
used by most of the world.
[Android.Runtime.Register("java/util/GregorianCalendar", DoNotGenerateAcw=true)]
public class GregorianCalendar : Java.Util.Calendar
[<Android.Runtime.Register("java/util/GregorianCalendar", DoNotGenerateAcw=true)>]
type GregorianCalendar = class
inherit Calendar
- Inheritance
- Attributes
Remarks
GregorianCalendar
is a concrete subclass of Calendar
and provides the standard calendar system used by most of the world.
GregorianCalendar
is a hybrid calendar that supports both the Julian and Gregorian calendar systems with the support of a single discontinuity, which corresponds by default to the Gregorian date when the Gregorian calendar was instituted (October 15, 1582 in some countries, later in others). The cutover date may be changed by the caller by calling #setGregorianChange(Date) setGregorianChange()
.
Historically, in those countries which adopted the Gregorian calendar first, October 4, 1582 (Julian) was thus followed by October 15, 1582 (Gregorian). This calendar models this correctly. Before the Gregorian cutover, GregorianCalendar
implements the Julian calendar. The only difference between the Gregorian and the Julian calendar is the leap year rule. The Julian calendar specifies leap years every four years, whereas the Gregorian calendar omits century years which are not divisible by 400.
GregorianCalendar
implements <em>proleptic</em> Gregorian and Julian calendars. That is, dates are computed by extrapolating the current rules indefinitely far backward and forward in time. As a result, GregorianCalendar
may be used for all years to generate meaningful and consistent results. However, dates obtained using GregorianCalendar
are historically accurate only from March 1, 4 AD onward, when modern Julian calendar rules were adopted. Before this date, leap year rules were applied irregularly, and before 45 BC the Julian calendar did not even exist.
Prior to the institution of the Gregorian calendar, New Year's Day was March 25. To avoid confusion, this calendar always uses January 1. A manual adjustment may be made if desired for dates that are prior to the Gregorian changeover and which fall between January 1 and March 24.
<h3>"week_and_year">Week Of Year and Week Year</h3>
Values calculated for the Calendar#WEEK_OF_YEAR WEEK_OF_YEAR
field range from 1 to 53. The first week of a calendar year is the earliest seven day period starting on Calendar#getFirstDayOfWeek() getFirstDayOfWeek()
that contains at least Calendar#getMinimalDaysInFirstWeek() getMinimalDaysInFirstWeek()
days from that year. It thus depends on the values of getMinimalDaysInFirstWeek()
, getFirstDayOfWeek()
, and the day of the week of January 1. Weeks between week 1 of one year and week 1 of the following year (exclusive) are numbered sequentially from 2 to 52 or 53 (except for year(s) involved in the Julian-Gregorian transition).
The getFirstDayOfWeek()
and getMinimalDaysInFirstWeek()
values are initialized using locale-dependent resources when constructing a GregorianCalendar
. "iso8601_compatible_setting">The week determination is compatible with the ISO 8601 standard when getFirstDayOfWeek()
is MONDAY
and getMinimalDaysInFirstWeek()
is 4, which values are used in locales where the standard is preferred. These values can explicitly be set by calling Calendar#setFirstDayOfWeek(int) setFirstDayOfWeek()
and Calendar#setMinimalDaysInFirstWeek(int) setMinimalDaysInFirstWeek()
.
A "week_year"><em>week year</em> is in sync with a WEEK_OF_YEAR
cycle. All weeks between the first and last weeks (inclusive) have the same <em>week year</em> value. Therefore, the first and last days of a week year may have different calendar year values.
For example, January 1, 1998 is a Thursday. If getFirstDayOfWeek()
is MONDAY
and getMinimalDaysInFirstWeek()
is 4 (ISO 8601 standard compatible setting), then week 1 of 1998 starts on December 29, 1997, and ends on January 4, 1998. The week year is 1998 for the last three days of calendar year 1997. If, however, getFirstDayOfWeek()
is SUNDAY
, then week 1 of 1998 starts on January 4, 1998, and ends on January 10, 1998; the first three days of 1998 then are part of week 53 of 1997 and their week year is 1997.
<h4>Week Of Month</h4>
Values calculated for the WEEK_OF_MONTH
field range from 0 to 6. Week 1 of a month (the days with WEEK_OF_MONTH = 1
) is the earliest set of at least getMinimalDaysInFirstWeek()
contiguous days in that month, ending on the day before getFirstDayOfWeek()
. Unlike week 1 of a year, week 1 of a month may be shorter than 7 days, need not start on getFirstDayOfWeek()
, and will not include days of the previous month. Days of a month before week 1 have a WEEK_OF_MONTH
of 0.
For example, if getFirstDayOfWeek()
is SUNDAY
and getMinimalDaysInFirstWeek()
is 4, then the first week of January 1998 is Sunday, January 4 through Saturday, January 10. These days have a WEEK_OF_MONTH
of 1. Thursday, January 1 through Saturday, January 3 have a WEEK_OF_MONTH
of 0. If getMinimalDaysInFirstWeek()
is changed to 3, then January 1 through January 3 have a WEEK_OF_MONTH
of 1.
<h4>Default Fields Values</h4>
The clear
method sets calendar field(s) undefined. GregorianCalendar
uses the following default value for each calendar field if its value is undefined.
<table class="striped" style="text-align: left; width: 66%;"> <caption style="display:none">GregorianCalendar default field values</caption> <thead> <tr> <th scope="col"> Field </th> <th scope="col"> Default Value </th> </tr> </thead> <tbody> <tr> <th scope="row"> ERA
</th> <td> AD
</td> </tr> <tr> <th scope="row"> YEAR
</th> <td> 1970
</td> </tr> <tr> <th scope="row"> MONTH
</th> <td> JANUARY
</td> </tr> <tr> <th scope="row"> DAY_OF_MONTH
</th> <td> 1
</td> </tr> <tr> <th scope="row"> DAY_OF_WEEK
</th> <td> the first day of week
</td> </tr> <tr> <th scope="row"> WEEK_OF_MONTH
</th> <td> 0
</td> </tr> <tr> <th scope="row"> DAY_OF_WEEK_IN_MONTH
</th> <td> 1
</td> </tr> <tr> <th scope="row"> AM_PM
</th> <td> AM
</td> </tr> <tr> <th scope="row"> HOUR, HOUR_OF_DAY, MINUTE, SECOND, MILLISECOND
</th> <td> 0
</td> </tr> </tbody> </table> <br>Default values are not applicable for the fields not listed above.
<strong>Example:</strong> <blockquote>
// get the supported ids for GMT-08:00 (Pacific Standard Time)
String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
// if no ids were returned, something is wrong. get out.
if (ids.length == 0)
System.exit(0);
// begin output
System.out.println("Current Time");
// create a Pacific Standard Time time zone
SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);
// set up rules for Daylight Saving Time
pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
// create a GregorianCalendar with the Pacific Daylight time zone
// and the current date and time
Calendar calendar = new GregorianCalendar(pdt);
Date trialTime = new Date();
calendar.setTime(trialTime);
// print out a bunch of interesting things
System.out.println("ERA: " + calendar.get(Calendar.ERA));
System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));
System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));
System.out.println("DATE: " + calendar.get(Calendar.DATE));
System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
System.out.println("DAY_OF_YEAR: " + calendar.get(Calendar.DAY_OF_YEAR));
System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));
System.out.println("DAY_OF_WEEK_IN_MONTH: "
+ calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM));
System.out.println("HOUR: " + calendar.get(Calendar.HOUR));
System.out.println("HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY));
System.out.println("MINUTE: " + calendar.get(Calendar.MINUTE));
System.out.println("SECOND: " + calendar.get(Calendar.SECOND));
System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND));
System.out.println("ZONE_OFFSET: "
+ (calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000)));
System.out.println("DST_OFFSET: "
+ (calendar.get(Calendar.DST_OFFSET)/(60*60*1000)));
System.out.println("Current Time, with hour reset to 3");
calendar.clear(Calendar.HOUR_OF_DAY); // so doesn't override
calendar.set(Calendar.HOUR, 3);
System.out.println("ERA: " + calendar.get(Calendar.ERA));
System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));
System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));
System.out.println("DATE: " + calendar.get(Calendar.DATE));
System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
System.out.println("DAY_OF_YEAR: " + calendar.get(Calendar.DAY_OF_YEAR));
System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));
System.out.println("DAY_OF_WEEK_IN_MONTH: "
+ calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM));
System.out.println("HOUR: " + calendar.get(Calendar.HOUR));
System.out.println("HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY));
System.out.println("MINUTE: " + calendar.get(Calendar.MINUTE));
System.out.println("SECOND: " + calendar.get(Calendar.SECOND));
System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND));
System.out.println("ZONE_OFFSET: "
+ (calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000))); // in hours
System.out.println("DST_OFFSET: "
+ (calendar.get(Calendar.DST_OFFSET)/(60*60*1000))); // in hours
</blockquote>
Added in 1.1.
Java documentation for java.util.GregorianCalendar
.
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
GregorianCalendar() |
Constructs a default |
GregorianCalendar(Int32, Int32, Int32, Int32, Int32, Int32) |
Constructs a GregorianCalendar with the given date and time set for the default time zone with the default locale. |
GregorianCalendar(Int32, Int32, Int32, Int32, Int32) |
Constructs a |
GregorianCalendar(Int32, Int32, Int32) |
Constructs a |
GregorianCalendar(IntPtr, JniHandleOwnership) |
A constructor used when creating managed representations of JNI objects; called by the runtime. |
GregorianCalendar(Locale) |
Constructs a |
GregorianCalendar(TimeZone, Locale) |
Constructs a |
GregorianCalendar(TimeZone) |
Constructs a |
Fields
Ad |
Value of the |
AllStyles |
Obsolete.
A style specifier for |
Am |
Value of the |
AmPm |
Obsolete.
Field number for |
April |
Value of the |
August |
Value of the |
Bc |
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 |
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. (Inherited from Calendar) |
FirstDayOfWeek |
Gets what the first day of the week is; e. (Inherited from Calendar) |
GregorianChange |
Gets the Gregorian Calendar change date. -or- Sets the |
Handle |
The handle to the underlying Android instance. (Inherited from Object) |
IsTimeSet |
True if then the value of |
IsWeekDateSupported |
Returns |
JniIdentityHashCode | (Inherited from Object) |
JniPeerMembers | |
Lenient |
Tells whether date/time interpretation is to be lenient. (Inherited from Calendar) |
MinimalDaysInFirstWeek |
Gets what the minimal days required in the first week of the year are; e. (Inherited from Calendar) |
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. (Inherited from Calendar) |
TimeZone |
Gets the time zone. -or- Sets the time zone with the given time zone value. (Inherited from Calendar) |
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 the specified (signed) 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. (Inherited from Calendar) |
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. (Inherited from Calendar) |
ComputeFields() |
Converts the time value (millisecond offset from the Epoch) to calendar field values. |
ComputeTime() |
Converts calendar field values to the time value (millisecond offset from the Epoch). |
Dispose() | (Inherited from Object) |
Dispose(Boolean) | (Inherited from Object) |
Equals(Object) |
Indicates whether some other object is "equal to" this one. (Inherited from Object) |
From(ZonedDateTime) |
Obtains an instance of |
Get(CalendarField) |
Returns the value of the given calendar field. (Inherited from Calendar) |
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 |
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) |
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. (Inherited from Calendar) |
IsLeapYear(Int32) |
Determines if the given year is a leap year. |
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. (Inherited from Calendar) |
Set(CalendarField, Int32) |
Sets the given calendar field to the given value. (Inherited from Calendar) |
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() | (Inherited from Calendar) |
ToString() |
Returns a string representation of the object. (Inherited from Object) |
ToZonedDateTime() |
Converts this object to a |
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) | (Inherited from Calendar) |
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) |