|
Chapter 17 The java.util Package |
|
GregorianCalendar
Name
GregorianCalendar
- Class Name:
-
java.util.GregorianCalendar
- Superclass:
-
java.util.Calendar
- Immediate Subclasses:
-
None
- Interfaces Implemented:
-
None
- Availability:
-
New as of JDK 1.1
The GregorianCalendar class
is a subclass of the abstract
Calendar class. GregorianCalendar
provides an implementation of the calendar that much of the world uses.
GregorianCalendar has two eras,
BC and AD.
GregorianCalendar provides both Gregorian and
Julian dates, depending on the date that is represented by the
object. The Gregorian calendar was instituted in October 15, 1582, so
any dates before this cut-off time are represented as Julian
dates. Some countries switched from the Julian and the Gregorian
calendar after that date, however. The cutoff date can be changed
using the setGregorianChange() method. When using
Julian dates, be aware that this class does not account for the fact
that the Julian calendar used March 25 as the beginning of the
year. You will have to adjust the year on Julian dates that fall
between January 1 and March 24.
You can find a fascinating discussion of the history of Western
calendars at
http://barroom.visionsystems.com/serendipity/date/jul_greg.html.
public class java.util.GregorianCalendar extends java.util.Calendar {
// Constants
public final static int AD;
public final static int BC;
// Constructors
public GregorianCalendar();
public GregorianCalendar(TimeZone zone);
public GregorianCalendar(Locale aLocale);
public GregorianCalendar(TimeZone zone, Locale aLocale);
public GregorianCalendar(int year, int month, int date);
public GregorianCalendar(int year, int month, int date,
int hour, int minute);
public GregorianCalendar(int year, int month, int date,
int hour, int minute, int second);
// Instance Methods
public void add(int field, int amount);
public boolean after(Object when);
public boolean before(Object when);
public Object clone();
public boolean equals(Object obj);
public int getGreatestMinimum(int field);
public final Date getGregorianChange();
public int getLeastMaximum(int field);
public int getMaximum(int field);
public int getMinimum(int field);
public synchronized int hashCode();
public boolean isLeapYear(int year);
public void roll(int field, boolean up);
public void setGregorianChange(Date date);
// Protected Instance Methods
protected void computeFields();
protected void computeTime();
}
- Description
-
A constant value that represents the AD era,
which stands for anno Domini, Latin for
"the year of the Lord". People who do not
want to measure years with a Christian connotation
call this era CE the Common Era.
- Description
-
A constant value that represents the BC era,
which stands for before Christ, before the
birth of Christ. People who do not want to measure years with a Christian
connotation call this era BCE, which stands for Before the Common Era.
- Description
-
This constructor creates a GregorianCalendar
that represents the current time using the system's default time
zone and locale. The default time zone is that returned by TimeZone.getDefault().
The default locale is that returned by Locale.getDefault().
- Parameters
-
- zone
-
The TimeZone
to use.
- Description
-
This constructor creates a GregorianCalendar
that represents the current time using the supplied time zone and the default
locale. The default locale is that returned by Locale.getDefault().
- Parameters
-
- aLocale
-
The Locale
to use.
- Description
-
This constructor creates a GregorianCalendar
that represents the current time using the supplied locale and the default
time zone. The default time zone is that returned by TimeZone.getDefault().
- Parameters
-
- zone
-
The TimeZone
to use.
- aLocale
-
The Locale
to use.
- Description
-
This constructor creates a GregorianCalendar
that represents the current time using the supplied time zone and locale.
- Parameters
-
- year
-
The value for the
year field.
- month
-
The value for the
month field, where 0 represents the first month.
- date
-
The value for the
day-of-the-month field.
- Description
-
This constructor creates a GregorianCalendar
that represents the given date in the default time zone and locale. The
default time zone is that returned by TimeZone.getDefault().
The default locale is that returned by Locale.getDefault().
- Parameters
-
- year
-
The value for the
year field.
- month
-
The value for the
month field, where 0 represents the first month.
- date
-
The value for the
day-of-the-month field.
- hour
-
The value for the
hour field.
- minute
-
The value for the
minute field.
- Description
-
This constructor creates a GregorianCalendar
that represents the given date and time in the default time zone and locale.
The default time zone is that returned by TimeZone.getDefault().
The default locale is that returned by Locale.getDefault().
- Parameters
-
- year
-
The value for the
year field.
- month
-
The value for the
month field, where 0 represents the first month.
- date
-
The value for the
day-of-the-month field.
- hour
-
The value for the
hour field.
- minute
-
The value for the
minute field.
- second
-
The value for the
second field.
- Description
-
This constructor creates a GregorianCalendar
that represents the given data and time in the default time zone and locale.
The default time zone is that returned by TimeZone.getDefault().
The default locale is that returned by Locale.getDefault().
- Parameters
-
- field
-
The time field to
be modified.
- amount
-
The amount to add
to the specified field value. This value can be negative.
- Throws
-
- IllegalArgumentException
-
If field is not a valid time field.
- Overrides
-
Calendar.add()
- Description
-
This method adds the given amount to the specified time field. For example,
you can compute a date 90 days beyond the current date of this GregorianCalendar
by calling add(Calendar.DATE,
90).
- Parameters
-
- when
-
The object to compare
to this GregorianCalendar.
- Returns
-
true if this object is after
when; false
otherwise.
- Overrides
-
Calendar.after()
- Description
-
This method returns true if
when is a GregorianCalendar
whose value falls before the value of this GregorianCalendar.
- Parameters
-
- when
-
The object to compare
to this GregorianCalendar.
- Returns
-
true if this object is before
when; false
otherwise.
- Overrides
-
Calendar.before()
- Description
-
This method returns true if
when is a GregorianCalendar
whose value falls after the value of this GregorianCalendar.
- Returns
-
A copy of this GregorianCalendar.
- Overrides
-
Calendar.clone()
- Description
-
This method creates a copy of this GregorianCalendar
and returns it. In other words, the returned GregorianCalendar
has the same time field values and raw time value as this GregorianCalendar.
- Parameters
-
- when
-
The object to be compared
with this object.
- Returns
-
true if the objects are equal;
false if they are not.
- Overrides
-
Calendar.equals()
- Description
-
This method returns true if
when is an instance of GregorianCalendar,
and it contains the same value as the object this method is associated
with.
- Parameters
-
- field
-
A time field constant.
- Returns
-
The highest minimum value for the given time field.
- Overrides
-
Calendar.getGreatestMinimum()
- Description
-
This method returns the highest minimum value for the given time field,
if the field has a range of minimum values. If the field has only one
minimum value, this method is equivalent to getMinimum().
All of the fields in GregorianCalendar
have only one minimum value.
- Returns
-
The date this GregorianCalendar
uses as the change date between the Julian and Gregorian calendars.
- Description
-
By default, GregorianCalendar considers midnight
local time, October 15, 1582, to be the date when the Gregorian
calendar was adopted. This value can be changed using
setGregorianChange().
- Parameters
-
- field
-
A time field constant.
- Returns
-
The lowest maximum value for the given time field.
- Overrides
-
Calendar.getLeastMaximum()
- Description
-
This method returns the lowest maximum value for the given time field,
if the field has a range of maximum values. If the field has only one
maximum value, this method is equivalent to getMaximum().
For example, for a GregorianCalendar,
the lowest maximum value of DATE_OF_MONTH
is 28.
- Parameters
-
- field
-
A time field constant.
- Returns
-
The maximum value for the given time field.
- Overrides
-
Calendar.getMaximum()
- Description
-
This method returns the maximum value for the given time field. For example,
for a GregorianCalendar, the
maximum value of DATE_OF_MONTH
is 31.
- Parameters
-
- field
-
A time field constant.
- Returns
-
The minimum value for the given time field.
- Overrides
-
Calendar.getMinimum()
- Description
-
This method returns the minimum value for the given time field. For example,
for a GregorianCalendar, the
minimum value of DATE_OF_MONTH
is 1.
- Returns
-
A hashcode for this GregorianCalendar.
- Overrides
-
Object.hashCode()
- Description
-
This method returns a hashcode for this object.
- Parameters
-
- year
-
The year to test.
- Returns
-
true if the given year is a
leap year; false otherwise.
- Description
-
This method returns a boolean
value that indicates whether or not the specified year is a leap year.
Leap years are those years that are divisible by
4, except those that are divisible by 100, unless they are divisible by
400. For example, 1900 is not a leap year because it is divisible by 100
but not by 400. The year 2000 is a leap year.
- Parameters
-
- field
-
The time field to
be adjusted.
- up
-
A boolean
value that indicates if the given field should be incremented.
- Throws
-
- IllegalArgumentException
-
If field is not a valid time field.
- Overrides
-
Calendar.roll()
- Description
-
This method adds or subtracts one time unit from the
given time field. For example, to increase the current date by one
day, you can call roll(GregorianCalendar.DATE,
true).
The method maintains the field being rolled within its valid range.
For example, in a calendar system that uses hours and minutes to
measure time, rolling the minutes up from 59 sets that field to 0.
By the same token, rolling that field down from 0 sets it to 59.
The roll() method does not adjust the value
of any other field than the one specified by its field
argument. In particular, for calendar systems that have months
with different numbers of days, it may be necessary to adjust the
month and also year when the day of the month is rolled up. For
example, calling roll(GregorianCalendar.DAY_OF_MONTH,
true) on a GregorianCalendar
that represents December 31, 1996 changes the date to December 1, 1996.
In addition, calling roll() may make the
fields inconsistent. For example, calling
roll(GregorianCalendar.MONTH,
true) on a GregorianCalendar
that represents January 31, 1997 changes the date to February
31, 1997. It is the responsibility of the caller of
roll() to adjust the other fields.
- Parameters
-
- date
-
A Date
object that represents the new time value.
- Description
-
This method sets the date that this GregorianCalendar
uses as the change date between the Julian and Gregorian calendars. The
default is midnight local time, October 15, 1582. This is the date that
Pope Gregory instituted the calendar in many Catholic countries in Europe.
Most Catholic countries followed within a few years. Protestant England
and America did not adopt the new calendar until September 14, 1752.
- Overrides
-
Calendar.computeFields()
- Description
-
This method calculates the time fields of this GregorianCalendar
from its raw time value.
- Overrides
-
Calendar.computeTime()
- Description
-
This method calculates the raw time value of this GregorianCalendar
from its time field values.
Calendar,
Cloneable,
Date,
IllegalArgumentException,
Locale,
Serializable,
TimeZone
|