Calendar is an abstract base class for converting between
a Date object and a set of integer fields such as
YEAR, MONTH, DAY, HOUR,
and so on. (A Date object represents a specific instant in
time with millisecond precision. See
java.util.Date
for information about the Date class.)
Subclasses of Calendar interpret a Date
according to the rules of a specific calendar system. The JDK
provides one concrete subclass of Calendar:
GregorianCalendar. Future subclasses could represent
the various types of lunar calendars in use in many parts of the world.
Like other locale-sensitive classes, Calendar provides a
class method, getInstance, for getting a generally useful
object of this type. Calendar's getInstance method
returns a GregorianCalendar object whose
time fields have been initialized with the current date and time:
Calendar rightNow = Calendar.getInstance();
A Calendar object can produce all the time field values
needed to implement the date-time formatting for a particular language
and calendar style (for example, Japanese-Gregorian, Japanese-Traditional).
When computing a Date from time fields, two special circumstances
may arise: there may be insufficient information to compute the
Date (such as only year and month but no day in the month),
or there may be inconsistent information (such as "Tuesday, July 15, 1996"
-- July 15, 1996 is actually a Monday).
Insufficient information. The calendar will use default
information to specify the missing fields. This may vary by calendar; for
the Gregorian calendar, the default for a field is the same as that of the
start of the epoch: i.e., YEAR = 1970, MONTH = JANUARY, DATE = 1, etc.
Inconsistent information. The calendar will give preference
to the combinations of fields in the following order (and disregard other
inconsistent information). For the day of the year:
Note: for some non-Gregorian calendars, different
fields may be necessary for complete disambiguation. For example, a full
specification of the historial Arabic astronomical calendar requires year,
month, day-of-month and day-of-week in some cases.
Note: There are certain possible ambiguities in
interpretation of certain singular times, which are resolved in the
following ways:
24:00:00 "belongs" to the following day. That is,
23:59 on Dec 31, 1969 < 24:00 on Jan 1, 1970 < 24:01:00 on Jan 1, 1970
Although historically not precise, midnight also belongs to "am",
and noon belongs to "pm", so on the same day,
12:00 am (midnight) < 12:01 am, and 12:00 pm (noon) < 12:01 pm
The date or time format strings are not part of the definition of a
calendar, as those must be modifiable or overridable by the user at
runtime. Use java.text.DateFormat
to format dates.
Calendar provides an API for field "rolling", where fields
can be incremented or decremented, but wrap around. For example, rolling the
month up in the date "September 12, 1996" results in "October 12, 1996".
Calendar also provides a date arithmetic function for
adding the specified (signed) amount of time to a particular time field.
For example, subtracting 5 days from the date "September 12, 1996" results
in "September 7, 1996".
Gets what the minimal days required in the first week of the year are;
e.g., if the first week is defined as one that contains the first day
of the first month of a year, getMinimalDaysInFirstWeek returns 1.
Sets what the minimal days required in the first week of the year are;
For example, if the first week is defined as one that contains the first
day of the first month of a year, call the method with value 1.
Sets the time zone with the given time zone value.
ERA
public static final int ERA
Useful constant for date and time. Used in time fields.
ERA is calendar specific.
YEAR
public static final int YEAR
Useful constant for date and time. Used in time fields.
MONTH
public static final int MONTH
Useful constant for date and time. Used in time fields.
WEEK_OF_YEAR
public static final int WEEK_OF_YEAR
Useful constant for date and time. Used in time fields.
WEEK_OF_MONTH
public static final int WEEK_OF_MONTH
Useful constant for date and time. Used in time fields.
DATE
public static final int DATE
Useful constant for date and time. Used in time fields.
This is a synonym for DAY_OF_MONTH.
DAY_OF_MONTH
public static final int DAY_OF_MONTH
Useful constant for date and time. Used in time fields.
This is a synonym for DATE.
DAY_OF_YEAR
public static final int DAY_OF_YEAR
Useful constant for date and time. Used in time fields.
DAY_OF_WEEK
public static final int DAY_OF_WEEK
Useful constant for date and time. Used in time fields.
DAY_OF_WEEK_IN_MONTH
public static final int DAY_OF_WEEK_IN_MONTH
Useful constant for date and time. Used in time fields.
AM_PM
public static final int AM_PM
Useful constant for date and time. Used in time fields.
HOUR
public static final int HOUR
Useful constant for date and time. Used in time fields.
HOUR is used for the 12-hour clock.
HOUR_OF_DAY
public static final int HOUR_OF_DAY
Useful constant for date and time. Used in time fields.
HOUR_OF_DAY is used for the 24-hour clock.
MINUTE
public static final int MINUTE
Useful constant for date and time. Used in time fields.
SECOND
public static final int SECOND
Useful constant for date and time. Used in time fields.
MILLISECOND
public static final int MILLISECOND
Useful constant for date and time. Used in time fields.
ZONE_OFFSET
public static final int ZONE_OFFSET
Useful constant for date and time. Used in time fields.
DST_OFFSET
public static final int DST_OFFSET
Useful constant for date and time. Used in time fields.
FIELD_COUNT
public static final int FIELD_COUNT
Useful constant for date and time.
FIELD_COUNT is used for the time field array creation.
SUNDAY
public static final int SUNDAY
Useful constant for days of week. Used in GregorianCalendar.
MONDAY
public static final int MONDAY
Useful constant for days of week. Used in GregorianCalendar.
TUESDAY
public static final int TUESDAY
Useful constant for days of week. Used in GregorianCalendar.
WEDNESDAY
public static final int WEDNESDAY
Useful constant for days of week. Used in GregorianCalendar.
THURSDAY
public static final int THURSDAY
Useful constant for days of week. Used in GregorianCalendar.
FRIDAY
public static final int FRIDAY
Useful constant for days of week. Used in GregorianCalendar.
SATURDAY
public static final int SATURDAY
Useful constant for days of week. Used in GregorianCalendar.
JANUARY
public static final int JANUARY
Useful constant for month. Used in GregorianCalendar.
Note: Calendar month is now 0-based.
FEBRUARY
public static final int FEBRUARY
Useful constant for month. Used in GregorianCalendar.
MARCH
public static final int MARCH
Useful constant for month. Used in GregorianCalendar.
APRIL
public static final int APRIL
Useful constant for month. Used in GregorianCalendar.
MAY
public static final int MAY
Useful constant for month. Used in GregorianCalendar.
JUNE
public static final int JUNE
Useful constant for month. Used in GregorianCalendar.
JULY
public static final int JULY
Useful constant for month. Used in GregorianCalendar.
AUGUST
public static final int AUGUST
Useful constant for month. Used in GregorianCalendar.
SEPTEMBER
public static final int SEPTEMBER
Useful constant for month. Used in GregorianCalendar.
OCTOBER
public static final int OCTOBER
Useful constant for month. Used in GregorianCalendar.
NOVEMBER
public static final int NOVEMBER
Useful constant for month. Used in GregorianCalendar.
DECEMBER
public static final int DECEMBER
Useful constant for month. Used in GregorianCalendar.
UNDECIMBER
public static final int UNDECIMBER
Useful constant for month. Used in GregorianCalendar.
UNDECIMBER is an artifical name. This 13th month is for lunar
calendars.
AM
public static final int AM
Useful constant for hour in 12-hour clock. Used in GregorianCalendar.
PM
public static final int PM
Useful constant for hour in 12-hour clock. Used in GregorianCalendar.
fields
protected int fields[]
The time fields containing values into which the millis is computed.
isSet
protected boolean isSet[]
The flags which tell if a specified time field for the calendar is set.
A new object has no fields set. After the first call to a method
which generates the fields, they all remain set after that.
time
protected long time
The current time set for the calendar.
isTimeSet
protected boolean isTimeSet
The flag which indicates if the current time is set for the calendar.
The time is made invalid by the user setting an individual field.
areFieldsSet
protected boolean areFieldsSet
True if the fields are in sync with the currently set time of this Calendar.
If false, then the next attempt to get the value of a field will
force a recomputation of all fields from the current value of the time
field.
This should really be named areFieldsInSync, but the old name is retained
for backward compatibility.
Calendar
protected Calendar()
Constructs a Calendar with the default time zone as returned
by TimeZone.getDefault(), and the default locale.
Converts UTC as milliseconds to time field values.
This allows you to sync up the time field values with
a new time that is set for the calendar. The time is not
recomputed first; to recompute the time, then the fields, call the
complete method.
value - the value to be set for the given time field.
set
public final void set(int year,
int month,
int date)
Sets the values for the fields year, month, and date.
Previous values of other fields are retained. If this is not desired,
call clear first.
Parameters:
year - the value used to set the YEAR time field.
month - the value used to set the MONTH time field.
Month value is 0-based. e.g., 0 for January.
date - the value used to set the DATE time field.
set
public final void set(int year,
int month,
int date,
int hour,
int minute)
Sets the values for the fields year, month, date, hour, and minute.
Previous values of other fields are retained. If this is not desired,
call clear first.
Parameters:
year - the value used to set the YEAR time field.
month - the value used to set the MONTH time field.
Month value is 0-based. e.g., 0 for January.
date - the value used to set the DATE time field.
hour - the value used to set the HOUR_OF_DAY time field.
minute - the value used to set the MINUTE time field.
set
public final void set(int year,
int month,
int date,
int hour,
int minute,
int second)
Sets the values for the fields year, month, date, hour, minute, and second.
Previous values of other fields are retained. If this is not desired,
call clear first.
Parameters:
year - the value used to set the YEAR time field.
month - the value used to set the MONTH time field.
Month value is 0-based. e.g., 0 for January.
date - the value used to set the DATE time field.
hour - the value used to set the HOUR_OF_DAY time field.
minute - the value used to set the MINUTE time field.
second - the value used to set the SECOND time field.
Compares the time field records.
Equivalent to comparing result of conversion to UTC.
Parameters:
when - the Calendar to be compared with this Calendar.
Returns:
true if the current time of this Calendar is after
the time of Calendar when; false otherwise.
add
public abstract void add(int field,
int amount)
Date Arithmetic function.
Adds the specified (signed) amount of time to the given time field,
based on the calendar's rules. For example, to subtract 5 days from
the current time of the calendar, you can achieve it by calling:
add(Calendar.DATE, -5).
Parameters:
field - the time field.
amount - the amount of date or time to be added to the field.
roll
public abstract void roll(int field,
boolean up)
Time Field Rolling function.
Rolls (up/down) a single unit of time on the given time field. For
example, to roll the current date up by one day, you can achieve it
by calling:
roll(Calendar.DATE, true).
When rolling on the year or Calendar.YEAR field, it will roll the year
value in the range between 1 and the value returned by calling
getMaximum(Calendar.YEAR).
When rolling on the month or Calendar.MONTH field, other fields like
date might conflict and, need to be changed. For instance,
rolling the month on the date 01/31/96 will result in 03/02/96.
When rolling on the hour-in-day or Calendar.HOUR_OF_DAY field, it will
roll the hour value in the range between 0 and 23, which is zero-based.
Parameters:
field - the time field.
up - indicates if the value of the specified time field is to be
rolled up or rolled down. Use true if rolling up, false otherwise.
the time zone object associated with this calendar.
setLenient
public void setLenient(boolean lenient)
Specify whether or not date/time interpretation is to be lenient. With
lenient interpretation, a date such as "February 942, 1996" will be
treated as being equivalent to the 941st day after February 1, 1996.
With strict interpretation, such dates will cause an exception to be
thrown.
Sets what the minimal days required in the first week of the year are;
For example, if the first week is defined as one that contains the first
day of the first month of a year, call the method with value 1. If it
must be a full week, use value 7.
Parameters:
value - the given minimal days required in the first week
of the year.
Gets what the minimal days required in the first week of the year are;
e.g., if the first week is defined as one that contains the first day
of the first month of a year, getMinimalDaysInFirstWeek returns 1. If
the minimal days required must be a full week, getMinimalDaysInFirstWeek
returns 7.
Returns:
the minimal days required in the first week of the year.
getMinimum
public abstract int getMinimum(int field)
Gets the minimum value for the given time field.
e.g., for Gregorian DAY_OF_MONTH, 1.
Parameters:
field - the given time field.
Returns:
the minimum value for the given time field.
getMaximum
public abstract int getMaximum(int field)
Gets the maximum value for the given time field.
e.g. for Gregorian DAY_OF_MONTH, 31.