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()
.
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 proleptic 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.
Values calculated for the WEEK_OF_YEAR
field range from 1 to 53. The first week of a
calendar year is the earliest seven day period starting on getFirstDayOfWeek()
that contains at
least 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
. 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 setFirstDayOfWeek()
and
setMinimalDaysInFirstWeek()
.
A week year is in sync with a
WEEK_OF_YEAR
cycle. All weeks between the first and last
weeks (inclusive) have the same week year 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.
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.
The clear
method sets calendar field(s)
undefined. GregorianCalendar
uses the following
default value for each calendar field if its value is undefined.
Field | Default Value |
---|---|
ERA
|
AD
|
YEAR
|
1970
|
MONTH
|
JANUARY
|
DAY_OF_MONTH
|
1
|
DAY_OF_WEEK
|
the first day of week
|
WEEK_OF_MONTH
|
0
|
DAY_OF_WEEK_IN_MONTH
|
1
|
AM_PM
|
AM
|
HOUR, HOUR_OF_DAY, MINUTE, SECOND, MILLISECOND
|
0
|
Example:
// 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
TimeZone
Modifier and Type | Field and Description |
---|---|
public static final int | AD
Value of the |
public static final int | BC
Value of the |
pack-priv static final int | |
private transient long | cachedFixedDate
The fixed date corresponding to gdate. |
private transient BaseCalendar | calsys
The CalendarSystem used to calculate the date in cdate. |
private transient BaseCalendar. | cdate
Reference to either gdate or a JulianCalendar.Date instance. |
pack-priv static final int | |
pack-priv static final long | |
private static final int | |
private static final int | |
private static final Gregorian | |
private transient BaseCalendar. | gdate
gdate always has a sun.util.calendar.Gregorian.Date instance to avoid overhead of creating it. |
private long | gregorianCutover
The point at which the Gregorian calendar rules are used, measured in milliseconds from the standard epoch. |
private transient long | gregorianCutoverDate
The fixed date of the gregorianCutover. |
private transient int | gregorianCutoverYear
The normalized year of the gregorianCutover in Gregorian, with 0 representing 1 BCE, -1 representing 2 BCE, etc. |
private transient int | gregorianCutoverYearJulian
The normalized year of the gregorianCutover in Julian, with 0 representing 1 BCE, -1 representing 2 BCE, etc. |
private static JulianCalendar | |
private static Era[] | |
pack-priv static final int[] | |
pack-priv static final int[] | |
pack-priv static final int[] | |
pack-priv static final int[] | |
pack-priv static final int[] | |
private static final long | |
private static final int | |
private static final int | |
private static final int | |
private static final long | |
private transient int[] | originalFields
Temporary storage for saving original fields[] values in non-lenient mode. |
pack-priv static final long | |
private transient int[] | zoneOffsets
Temporary int[2] to get time zone offsets. |
Access | Constructor and Description |
---|---|
public | GregorianCalendar()
Constructs a default |
public | GregorianCalendar(TimeZone
the given time zone. zone)Constructs a |
public | GregorianCalendar(Locale
the given locale. aLocale)Constructs a |
public | GregorianCalendar(TimeZone
the given time zone. zone, Locale the given locale. aLocale)Constructs a |
public | GregorianCalendar(int
the value used to set the year, int YEAR calendar field in the calendar.the value used to set the month, int MONTH calendar field in the calendar.
Month value is 0-based. e.g., 0 for January.the value used to set the dayOfMonth)DAY_OF_MONTH calendar field in the calendar.Constructs a |
public | GregorianCalendar(int
the value used to set the year, int YEAR calendar field in the calendar.the value used to set the month, int MONTH calendar field in the calendar.
Month value is 0-based. e.g., 0 for January.the value used to set the dayOfMonth, int DAY_OF_MONTH calendar field in the calendar.the value used to set the hourOfDay, int HOUR_OF_DAY calendar field
in the calendar.the value used to set the minute)MINUTE calendar field
in the calendar.Constructs a |
public | GregorianCalendar(int
the value used to set the year, int YEAR calendar field in the calendar.the value used to set the month, int MONTH calendar field in the calendar.
Month value is 0-based. e.g., 0 for January.the value used to set the dayOfMonth, int DAY_OF_MONTH calendar field in the calendar.the value used to set the hourOfDay, int HOUR_OF_DAY calendar field
in the calendar.the value used to set the minute, int MINUTE calendar field
in the calendar.the value used to set the second)SECOND calendar field
in the calendar.Constructs a GregorianCalendar with the given date and time set for the default time zone with the default locale. |
pack-priv | GregorianCalendar(int
the value used to set the year, int YEAR calendar field in the calendar.the value used to set the month, int MONTH calendar field in the calendar.
Month value is 0-based. e.g., 0 for January.the value used to set the dayOfMonth, int DAY_OF_MONTH calendar field in the calendar.the value used to set the hourOfDay, int HOUR_OF_DAY calendar field
in the calendar.the value used to set the minute, int MINUTE calendar field
in the calendar.the value used to set the second, int SECOND calendar field
in the calendar.the value used to set the millis)MILLISECOND calendar fieldConstructs a |
pack-priv | GregorianCalendar(TimeZone
the given time zone zone, Locale the given locale locale, boolean the flag requesting an empty instance flag)Constructs an empty GregorianCalendar. |
Modifier and Type | Method and Description |
---|---|
private int | |
public void | add(int
the calendar field. field, int the amount of date or time to be added to the field. amount)Implements abstract java. Adds the specified (signed) amount of time to the given calendar field, based on the calendar's rules. |
public Object | |
protected void | computeFields()
Implements abstract java. Converts the time value (millisecond offset from the Epoch) to calendar field values. |
private int | Returns: a new field mask that indicates what field values have actually been set.a bit mask to specify which fields to change
the setting state. fieldMask, int a bit mask to specify which time zone offset
fields to be used for time calculations tzMask)This computeFields implements the conversion from UTC (millisecond offset from the Epoch) to calendar field values. |
protected void | computeTime()
Implements abstract java. Converts calendar field values to the time value (millisecond offset from the Epoch). |
private boolean | dayInMinWeek(int day, int startDay, int endDay)
Given the first day and last day of a week, this method determines if the specified day exists in the minimum week. |
public boolean | Returns: true if this object is equal to obj ;
false otherwise.the object to compare with. obj)Overrides java. Compares this |
public static GregorianCalendar | Returns: the gregorian calendar representing the same point on the time-line as the zoned date-time providedthe zoned date-time object to convert zdt)Obtains an instance of |
public int | Returns: the maximum of the given field for the time value of thisGregorianCalendar the calendar field field)Overrides java. Returns the maximum value that this calendar field could have,
taking into consideration the given time value and the current
values of the
|
public int | Returns: the minimum of the given field for the time value of thisGregorianCalendar the calendar field field)Overrides java. Returns the minimum value that this calendar field could have,
taking into consideration the given time value and the current
values of the
|
private BaseCalendar. | getCalendarDate(long
the fixed date fd)Returns a CalendarDate produced from the specified fixed date. |
public String | Returns: "gregory" Overrides java. Returns |
private long | |
private BaseCalendar | getCutoverCalendarSystem()
Returns the calendar system for dates before the cutover date in the cutover year. |
private long | Returns: the fixed datethe CalendarSystem to be used for the date calculation cal, int the normalized year number, with 0 indicating the
year 1 BCE, -1 indicating 2 BCE, etc. year, int the calendar fields to be used for the date calculation fieldMask)Computes the fixed date under either the Gregorian or the Julian calendar, using the given year and the specified calendar fields. |
private long | getFixedDateJan1(BaseCalendar.
the date for which the first day of the year is
calculated. The date has to be in the cut-over year (Gregorian
or Julian). date,the fixed date representation of the date fixedDate)Returns the fixed date of the first day of the year (usually January 1) before the specified date. |
private long | getFixedDateMonth1(BaseCalendar.
the date for which the first day of the month is
calculated. The date has to be in the cut-over year (Gregorian
or Julian). date,the fixed date representation of the date fixedDate)Returns the fixed date of the first date of the month (usually the 1st of the month) before the specified date. |
public int | Returns: the highest minimum value for the given calendar field.the calendar field. field)Implements abstract java. Returns the highest minimum value for the given calendar field
of this |
public final Date | Returns: the Gregorian cutover date for thisGregorianCalendar object.Gets the Gregorian Calendar change date. |
private BaseCalendar. | |
private static synchronized BaseCalendar | |
private BaseCalendar. | |
public int | Returns: the lowest maximum value for the given calendar field.the calendar field field)Implements abstract java. Returns the lowest maximum value for the given calendar field
of this |
public int | Returns: the maximum value for the given calendar field.the calendar field. field)Implements abstract java. Returns the maximum value for the given calendar field of this
|
public int | Returns: the minimum value for the given calendar field.the calendar field. field)Implements abstract java. Returns the minimum value for the given calendar field of this
|
private GregorianCalendar | |
private static int | getRolledValue(int value, int amount, int min, int max)
Returns the new value after 'roll'ing the specified value and amount. |
public TimeZone | |
private int | Returns: the number of weeks of the given periodthe fixed date of the first day of the period fixedDay1, long the fixed date of the last day of the period fixedDate)Returns the number of weeks in a period between fixedDay1 and fixedDate. |
public int | Returns: the number of weeks in the week year.Overrides java. Returns the number of weeks in the week year
represented by this |
public int | getWeekYear()
Overrides java. Returns the week year represented by this
|
private long | |
public int | hashCode()
Overrides java. Generates the hash code for this |
private int | |
private boolean | isCutoverYear(int normalizedYear)
Determines if the specified year (normalized) is the Gregorian cutover year. |
private boolean | Returns: true if the first week of the current year is minimum
and the DAY_OF_WEEK does not exist in that weekReturns |
public boolean | Returns: true if the given year is a leap year; false otherwise.the given year. year)Determines if the given year is a leap year. |
public final boolean | Returns: true (always)Overrides java. Returns |
private int | |
private int | monthLength(int month)
Returns the length of the specified month in the year provided by internalGet(YEAR). |
private void | pinDayOfMonth()
After adjustments such as add(MONTH), add(YEAR), we don't want the month to jump around. |
private void | |
public void | roll(int
the time field. field, boolean indicates if the value of the specified calendar field is to be
rolled up or rolled down. Use up)true if rolling up, false otherwise.Implements abstract java. Adds or subtracts (up/down) a single unit of time on the given time field without changing larger fields. |
public void | roll(int
the calendar field. field, int the signed amount to add to amount)field .Overrides java. Adds a signed amount to the specified calendar field without changing larger fields. |
public void | setGregorianChange(Date
the given Gregorian cutover date. date)Sets the |
private void | |
public void | setTimeZone(TimeZone
the given time zone. zone)Overrides java. Sets the time zone with the given time zone value. |
public void | setWeekDate(int
the week year weekYear, int the week number based on weekOfYear, int dayOfWeek)weekYear Overrides java. Sets this |
public ZonedDateTime | Returns: a zoned date-time representing the same point on the time-line as this gregorian calendarConverts this object to a |
private int | |
private int |