THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1
publicGregorianCalendar(int year, int month, int date)

Equivalent to GregorianCalendar(year,month,date,0,0,0)
that is, midnight on the given date (which is considered to be the start of the
day).

publicGregorianCalendar(Locale locale)

Creates a GregorianCalendar object that represents the current time in
the default time zone with the given locale.

publicGregorianCalendar(TimeZone timeZone)

Creates a GregorianCalendar object that represents the current time in
the given timeZone with the default locale.

publicGregorianCalendar(TimeZone zone, Locale locale)

Creates a GregorianCalendar object that represents the current time in
the given timeZone with the given locale.

In addition to the methods it inherits from Calendar, GregorianCalendar provides an isLeapYear
method that returns TRue if the passed in year is a leap year in that calendar.


The Gregorian calendar was preceded by the Julian calendar in many places. In a GregorianCalendar
object, the default date at which this change happened is midnight local time on October 15, 1582. This is
when the first countries switched, but others changed later. The getGregorianChange method returns the
time the calendar is currently using for the change as a Date. You can set a calendar's change-over time by
using setGregorianChange with a Date object.


The SimpleTimeZone class is a concrete subclass of TimeZone that expresses values for Gregorian
calendars. It does not handle historical complexities, but instead projects current practices onto all times. For
historical dates that precede the use of daylight saving time, for example, you will want to use a calendar with
a time zone you have selected that ignores daylight saving time. For future dates, SimpleTimeZone is
probably as good a guess as any.


24.5. Formatting and Parsing Dates and Times


Date and time formatting is a separate issue from calendars, although they are closely related. Formatting is
localized in a different way. Not only are the names of days and months different in different locales that
share the same calendar, but also the order in which a dates' components are expressed changes. In the United
States it is customary in short dates to put the month before the date, so that July 5 is written as 7/5. In many
European countries the date comes first, so 5 July becomes 5/7 or 5.7 or ...


In the previous sections the word "date" meant a number of milliseconds since the epoch, which could be
interpreted as year, month, day-of-month, hours, minutes, and seconds information. When dealing with the
formatting classes you must distinguish between dates, which deal with year, month, and day-of-month
information, and times, which deal with hours, minutes, and seconds.


Date and time formatting issues are text issues, so the classes for formatting are in the java.text
packagethough the java.util.Formatter class (see page 624) also supports some localized date
formatting as you shall see. The Date2 program on page 695 is simple because it does not localize its output.
If you want localization, you need a DateFormat object.

Free download pdf