Java Reference
In-Depth Information
Gregorian Calendars
The Gregorian calendar is the calendar generally in use today in the western world, and is represented
by an object of the GregorianCalendar class. A GregorianCalendar object encapsulates time
zone information, as well as date and time data. There are no less than seven constructors for
GregorianCalendar objects, from the default that creates a calendar with the current date and time
in the default locale for your machine, through to a constructor where you can specify the year, month,
day, hour, minute, and second. The default suits most situations.
You can create a calendar with a statement such as:
GregorianCalendar calendar = new GregorianCalendar();
This will be set to the current instant in time, and you can retrieve this as a Date object by calling the
getTime() method for the calendar:
Date now = calendar.getTime();
You can create a GregorianCalendar object encapsulating a specific date and/or time with the
following constructors:
GregorianCalendar(int year, int month, int day)
GregorianCalendar(int year, int month, int day, int hour, int minute)
GregorianCalendar(int year, int month, int day,
int hour, int minute, int second)
The day argument is the day within the month, so the value can be from 1 to 28, 29, 30, or 31,
depending on the month and whether it's a leap year or not. The month value is zero-based so January
is 0 and December is 11.
The GregorianCalendar class is derived from the abstract Calendar class from which it inherits a
large number of methods and static constants for use with these methods. The constants includes month
values with the names JANUARY to DECEMBER so you could create a calendar object with the statement:
GregorianCalendar calendar = new GregorianCalendar(1967, Calendar.MARCH, 10);
The time zone and locale will be the default for the computer on which this statement executes. If you
want to specify a time zone there is a GregorianCalendar constructor that accepts an argument of
type TimeZone . The TimeZone class is also defined in the java.util package. You can get the
default TimeZone object by calling the static getDefault() method, but if you are going to the
trouble of specifying a time zone, you probably want something other than the default. To create a
particular time zone you need to know its ID. This is a string specifying a region or country plus a
location. For instance, here are some examples of time zone IDs:
Europe/
Stockholm
Asia/Novosibirsk
Pacific/Guam
America/Chicago
Antarctica/
Palmer
Atlantic/
South _ Georgia
Africa/Accra
Indian/Comoro
Search WWH ::




Custom Search