GregorianCalendar is a concrete subclass of
Calendar
and provides the standard calendar used by most of the world.
The standard (Gregorian) calendar has 2 eras, BC and AD.
This implementation handles a single discontinuity, which corresponds
by default to the date the Gregorian calendar was instituted (October 15,
1582 in some countries, later in others). This cutover date may be changed
by the caller.
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.
The current implementation handles dates in a wide range, from
4716 BC up to 5000000 AD. Dates outside of that range
will throw an IllegalArgumentException. This range should be broadened
in the future.
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 savings 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
Sets the GregorianCalendar change date. This is the point when the
switch from Julian dates to Gregorian dates occurred. Default is
00:00:00 local time, October 15, 1582. Previous to this time and date
will be Julian dates.
Gets the Gregorian Calendar change date. This is the point when the
switch from Julian dates to Gregorian dates occurred. Default is
00:00:00 local time, October 15, 1582. Previous to
this time and date will be Julian dates.
Overrides Calendar
Converts UTC as milliseconds to time field values.
The time is not
recomputed first; to recompute the time, then the fields, call the
complete method.
Overrides Calendar
Compares the time field records.
Equivalent to comparing result of conversion to UTC.
Please see Calendar.equals for descriptions on parameters and
the return value.
Overrides Calendar
Compares the time field records.
Equivalent to comparing result of conversion to UTC.
Please see Calendar.before for descriptions on parameters and
the return value.
Overrides Calendar
Compares the time field records.
Equivalent to comparing result of conversion to UTC.
Please see Calendar.after for descriptions on parameters and
the return value.
Returns minimum value for the given field.
e.g. for Gregorian DAY_OF_MONTH, 1
Please see Calendar.getMinimum for descriptions on parameters and
the return value.
Returns maximum value for the given field.
e.g. for Gregorian DAY_OF_MONTH, 31
Please see Calendar.getMaximum for descriptions on parameters and
the return value.
Returns highest minimum value for the given field if varies.
Otherwise same as getMinimum(). For Gregorian, no difference.
Please see Calendar.getGreatestMinimum for descriptions on parameters
and the return value.
Returns lowest maximum value for the given field if varies.
Otherwise same as getMaximum(). For Gregorian DAY_OF_MONTH, 28
Please see Calendar.getLeastMaximum for descriptions on parameters and
the return value.