java.util
public abstract class Calendar extends Object implements Serializable, Cloneable, Comparable<Calendar>
Date
objects and a set of
integer fields which represent YEAR
,
MONTH
, DAY
, etc. The Date
object represents a time in milliseconds since the Epoch. getInstance
. You can even provide
a locale or a timezone. getInstance
returns currently
a GregorianCalendar
for the current date. Date
-Object, you can create
a new Calendar with getInstance()
,
clear()
all fields, set(int,int)
the
fields you need and convert it with getTime()
. Date
-object to the Calendar
representation, create a new Calendar, assign the
Date
-Object with setTime()
, and read the
fields with get(int)
. month + day_of_month month + week_of_month + day_of_week month + day_of_week_of_month + day_of_week day_of_year day_of_week + week_of_yearThe hour_of_day-field takes precedence over the ampm and hour_of_ampm fields.
java.text.DateFormat
class. roll
ing fields (that means increase/decrease a
specific field by one, propagating overflows), or
add
ing/substracting a fixed amount to a field.Date
,
GregorianCalendar
,
TimeZone
,
DateFormat
,
Serialized FormModifier and Type | Field and Description |
---|---|
static int |
ALL_STYLES
A style specifier for
getDisplayNames(int,int,Locale)
stating that names should be returned in both long and short variants. |
static int |
AM
Useful constant for 12-hour clock.
|
static int |
AM_PM
Constant representing the part of the day for 12-hour clock.
|
static int |
APRIL
Constant representing April.
|
protected boolean |
areFieldsSet
Tells if the fields have a valid value.
|
static int |
AUGUST
Constant representing August.
|
static int |
DATE
Constant representing the day time field, synonym for DAY_OF_MONTH.
|
static int |
DAY_OF_MONTH
Constant representing the day time field.
|
static int |
DAY_OF_WEEK
Constant representing the day of week time field.
|
static int |
DAY_OF_WEEK_IN_MONTH
Constant representing the day-of-week-in-month field.
|
static int |
DAY_OF_YEAR
Constant representing the day of year time field.
|
static int |
DECEMBER
Constant representing December.
|
static int |
DST_OFFSET
Constant representing the daylight saving time offset in
milliseconds.
|
static int |
ERA
Constant representing the era time field.
|
static int |
FEBRUARY
Constant representing February.
|
static int |
FIELD_COUNT
Number of time fields.
|
protected int[] |
fields
The time fields.
|
static int |
FRIDAY
Constant representing Friday.
|
static int |
HOUR
Constant representing the hour time field for 12-hour clock.
|
static int |
HOUR_OF_DAY
Constant representing the hour of day time field for 24-hour clock.
|
protected boolean[] |
isSet
The flags which tell if the fields above have a value.
|
protected boolean |
isTimeSet
Tells if the above field has a valid value.
|
static int |
JANUARY
Constant representing January.
|
static int |
JULY
Constant representing July.
|
static int |
JUNE
Constant representing June.
|
static int |
LONG
A style specifier for
getDisplayName(int,int,Locale)
and getDisplayNames(int,int,Locale) stating that names
should be returned in their long variant if applicable. |
static int |
MARCH
Constant representing March.
|
static int |
MAY
Constant representing May.
|
static int |
MILLISECOND
Constant representing the millisecond time field.
|
static int |
MINUTE
Constant representing the minute of hour time field.
|
static int |
MONDAY
Constant representing Monday.
|
static int |
MONTH
Constant representing the month time field.
|
static int |
NOVEMBER
Constant representing November.
|
static int |
OCTOBER
Constant representing October.
|
static int |
PM
Useful constant for 12-hour clock.
|
static int |
SATURDAY
Constant representing Saturday.
|
static int |
SECOND
Constant representing the second time field.
|
static int |
SEPTEMBER
Constant representing September.
|
static int |
SHORT
A style specifier for
getDisplayName(int,int,Locale)
and getDisplayNames(int,int,Locale) stating that names
should be returned in their short variant if applicable. |
static int |
SUNDAY
Constant representing Sunday.
|
static int |
THURSDAY
Constant representing Thursday.
|
protected long |
time
The time in milliseconds since the epoch.
|
static int |
TUESDAY
Constant representing Tuesday.
|
static int |
UNDECIMBER
Constant representing Undecimber.
|
static int |
WEDNESDAY
Constant representing Wednesday.
|
static int |
WEEK_OF_MONTH
Constant representing the week of the month time field.
|
static int |
WEEK_OF_YEAR
Constant representing the week of the year field.
|
static int |
YEAR
Constant representing the year time field.
|
static int |
ZONE_OFFSET
Constant representing the time zone offset time field for the
time given in the other fields.
|
Modifier | Constructor and Description |
---|---|
protected |
Calendar()
Constructs a new Calendar with the default time zone and the default
locale.
|
protected |
Calendar(TimeZone zone,
Locale locale)
Constructs a new Calendar with the given time zone and the given
locale.
|
Modifier and Type | Method and Description |
---|---|
abstract void |
add(int field,
int amount)
Adds the specified amount of time to the given time field.
|
boolean |
after(Object o)
Compares the given calendar with this.
|
boolean |
before(Object o)
Compares the given calendar with this.
|
void |
clear()
Clears the values of all the time fields.
|
void |
clear(int field)
Clears the values of the specified time field.
|
Object |
clone()
Return a clone of this object.
|
int |
compareTo(Calendar cal)
Compares the time of two calendar instances.
|
protected void |
complete()
Fills any unset fields in the time field list
|
protected abstract void |
computeFields()
Converts the milliseconds since the epoch UTC
(
time ) to time fields
(fields ). |
protected abstract void |
computeTime()
Converts the time field values (
fields ) to
milliseconds since the epoch UTC (time ). |
boolean |
equals(Object o)
Compares the given calendar with this.
|
int |
get(int field)
Gets the value of the specified field.
|
int |
getActualMaximum(int field)
Gets the actual maximum value that is allowed for the specified field.
|
int |
getActualMinimum(int field)
Gets the actual minimum value that is allowed for the specified field.
|
static Locale[] |
getAvailableLocales()
Gets the set of locales for which a Calendar is available.
|
String |
getDisplayName(int field,
int style,
Locale locale)
Returns a localised textual representation of the current value
of the given field using the specified style.
|
Map<String,Integer> |
getDisplayNames(int field,
int style,
Locale locale)
Returns a map linking all specified textual representations
of the given field to their numerical values.
|
int |
getFirstDayOfWeek()
Gets what the first day of week is.
|
abstract int |
getGreatestMinimum(int field)
Gets the greatest minimum value that is allowed for the specified field.
|
static Calendar |
getInstance()
Creates a calendar representing the actual time, using the default
time zone and locale.
|
static Calendar |
getInstance(Locale locale)
Creates a calendar representing the actual time, using the default
time zone and the given locale.
|
static Calendar |
getInstance(TimeZone zone)
Creates a calendar representing the actual time, using the given
time zone and the default locale.
|
static Calendar |
getInstance(TimeZone zone,
Locale locale)
Creates a calendar representing the actual time, using the given
time zone and locale.
|
abstract int |
getLeastMaximum(int field)
Gets the smallest maximum value that is allowed for the
specified field.
|
abstract int |
getMaximum(int field)
Gets the biggest value that is allowed for the specified field.
|
int |
getMinimalDaysInFirstWeek()
Gets how many days are required in the first week of the year.
|
abstract int |
getMinimum(int field)
Gets the smallest value that is allowed for the specified field.
|
Date |
getTime()
Converts the time represented by this object to a
Date -Object. |
long |
getTimeInMillis()
Returns the time represented by this Calendar.
|
TimeZone |
getTimeZone()
Gets the time zone of this calendar
|
int |
hashCode()
Returns a hash code for this calendar.
|
protected int |
internalGet(int field)
Gets the value of the specified field.
|
boolean |
isLenient()
Tells if the date/time interpretation is lenient.
|
boolean |
isSet(int field)
Determines if the specified field has a valid value.
|
abstract void |
roll(int field,
boolean up)
Rolls the specified time field up or down.
|
void |
roll(int field,
int amount)
Rolls up or down the specified time field by the given amount.
|
void |
set(int field,
int value)
Sets the time field with the given value.
|
void |
set(int year,
int month,
int date)
Sets the fields for year, month, and date
|
void |
set(int year,
int month,
int date,
int hour,
int minute)
Sets the fields for year, month, date, hour, and minute
|
void |
set(int year,
int month,
int date,
int hour,
int minute,
int second)
Sets the fields for year, month, date, hour, and minute
|
void |
setFirstDayOfWeek(int value)
Sets what the first day of week is.
|
void |
setLenient(boolean lenient)
Specifies if the date/time interpretation should be lenient.
|
void |
setMinimalDaysInFirstWeek(int value)
Sets how many days are required in the first week of the year.
|
void |
setTime(Date date)
Sets this Calendar's time to the given Date.
|
void |
setTimeInMillis(long time)
Sets this Calendar's time to the given Time.
|
void |
setTimeZone(TimeZone zone)
Sets the time zone to the specified value.
|
String |
toString()
Returns a string representation of this object.
|
public static final int ERA
public static final int YEAR
public static final int MONTH
public static final int WEEK_OF_YEAR
setFirstDayOfWeek(int)
,
Constant Field Valuespublic static final int WEEK_OF_MONTH
setFirstDayOfWeek(int)
,
Constant Field Valuespublic static final int DATE
public static final int DAY_OF_MONTH
public static final int DAY_OF_YEAR
public static final int DAY_OF_WEEK
public static final int DAY_OF_WEEK_IN_MONTH
public static final int AM_PM
public static final int HOUR
public static final int HOUR_OF_DAY
public static final int MINUTE
public static final int SECOND
public static final int MILLISECOND
public static final int ZONE_OFFSET
public static final int DST_OFFSET
public static final int FIELD_COUNT
public static final int SUNDAY
public static final int MONDAY
public static final int TUESDAY
public static final int WEDNESDAY
public static final int THURSDAY
public static final int FRIDAY
public static final int SATURDAY
public static final int JANUARY
public static final int FEBRUARY
public static final int MARCH
public static final int APRIL
public static final int MAY
public static final int JUNE
public static final int JULY
public static final int AUGUST
public static final int SEPTEMBER
public static final int OCTOBER
public static final int NOVEMBER
public static final int DECEMBER
public static final int UNDECIMBER
public static final int AM
public static final int PM
public static final int ALL_STYLES
getDisplayNames(int,int,Locale)
stating that names should be returned in both long and short variants.SHORT
,
LONG
,
Constant Field Valuespublic static final int SHORT
getDisplayName(int,int,Locale)
and getDisplayNames(int,int,Locale)
stating that names
should be returned in their short variant if applicable.public static final int LONG
getDisplayName(int,int,Locale)
and getDisplayNames(int,int,Locale)
stating that names
should be returned in their long variant if applicable.protected int[] fields
protected boolean[] isSet
protected long time
protected boolean isTimeSet
protected boolean areFieldsSet
protected Calendar()
public static Calendar getInstance()
public static Calendar getInstance(TimeZone zone)
zone
- a time zone (null
not permitted).NullPointerException
- if zone
is null
.public static Calendar getInstance(Locale locale)
locale
- a locale (null
not permitted).NullPointerException
- if locale
is null
.public static Calendar getInstance(TimeZone zone, Locale locale)
zone
- a time zone (null
not permitted).locale
- a locale (null
not permitted).NullPointerException
- if zone
or locale
is null
.public static Locale[] getAvailableLocales()
MissingResourceException
- if locale data couldn't be found.protected abstract void computeTime()
fields
) to
milliseconds since the epoch UTC (time
). Override
this method if you write your own Calendar.protected abstract void computeFields()
time
) to time fields
(fields
). Override this method if you write your
own Calendar.public final Date getTime()
Date
-Object.public final void setTime(Date date)
date
- the date (null
not permitted).NullPointerException
- if date
is null
.public long getTimeInMillis()
public void setTimeInMillis(long time)
time
- the time in milliseconds since the epochpublic int get(int field)
field
- the time field. One of the time field constants.ArrayIndexOutOfBoundsException
- if the field is outside
the valid range. The value of field must be >= 0 and
<= FIELD_COUNT
.protected final int internalGet(int field)
field
- the time field. One of the time field constants.areFieldsSet
or isSet[field]
is false.ArrayIndexOutOfBoundsException
- if the field is outside
the valid range. The value of field must be >= 0 and
<= FIELD_COUNT
.public void set(int field, int value)
field
- the time field. One of the time field constantsvalue
- the value to be set.ArrayIndexOutOfBoundsException
- if field is outside
the valid range. The value of field must be >= 0 and
<= FIELD_COUNT
.public final void set(int year, int month, int date)
year
- the year.month
- the month, one of the constants JANUARY..UNDICEMBER.date
- the day of the monthpublic final void set(int year, int month, int date, int hour, int minute)
year
- the year.month
- the month, one of the constants JANUARY..UNDICEMBER.date
- the day of the monthhour
- the hour of day.minute
- the minute.public final void set(int year, int month, int date, int hour, int minute, int second)
year
- the year.month
- the month, one of the constants JANUARY..UNDICEMBER.date
- the day of the monthhour
- the hour of day.minute
- the minute.second
- the second.public final void clear()
public final void clear(int field)
field
- the time field. One of the time field constants.ArrayIndexOutOfBoundsException
- if field is outside
the valid range. The value of field must be >= 0 and
<= FIELD_COUNT
.public final boolean isSet(int field)
ArrayIndexOutOfBoundsException
- if the field is outside
the valid range. The value of field must be >= 0 and
<= FIELD_COUNT
.protected void complete()
public boolean equals(Object o)
equals
in class Object
o
- the object to that we should compare.Object.hashCode()
public int hashCode()
hashCode
in class Object
hashCode()
Object.equals(Object)
,
System.identityHashCode(Object)
public boolean before(Object o)
o
- the object to that we should compare.ClassCastException
- if o is not an calendar.public boolean after(Object o)
o
- the object to that we should compare.ClassCastException
- if o is not an calendar.public abstract void add(int field, int amount)
field
- the time field. One of the time field constants.amount
- the amount of time.ArrayIndexOutOfBoundsException
- if the field is outside
the valid range. The value of field must be >= 0 and
<= FIELD_COUNT
.public abstract void roll(int field, boolean up)
field
- the time field. One of the time field constants.up
- the direction, true for up, false for down.ArrayIndexOutOfBoundsException
- if the field is outside
the valid range. The value of field must be >= 0 and
<= FIELD_COUNT
.public void roll(int field, int amount)
roll(int, boolean)
for the specified amount.
Subclasses should override this method to do more intuitiv things.field
- the time field. One of the time field constants.amount
- the amount to roll by, positive for rolling up,
negative for rolling down.ArrayIndexOutOfBoundsException
- if the field is outside
the valid range. The value of field must be >= 0 and
<= FIELD_COUNT
.public void setTimeZone(TimeZone zone)
zone
- the new time zonepublic TimeZone getTimeZone()
public void setLenient(boolean lenient)
lenient
- true, if the date should be interpreted linient,
false if it should be interpreted strict.public boolean isLenient()
public void setFirstDayOfWeek(int value)
value
- the first day of week. One of SUNDAY to SATURDAY.public int getFirstDayOfWeek()
public void setMinimalDaysInFirstWeek(int value)
value
- the minimal days required in the first week.public int getMinimalDaysInFirstWeek()
setMinimalDaysInFirstWeek(int)
public abstract int getMinimum(int field)
field
- the time field. One of the time field constants.public abstract int getMaximum(int field)
field
- the time field. One of the time field constants.public abstract int getGreatestMinimum(int field)
field
- the time field. One of the time field constants.public abstract int getLeastMaximum(int field)
field
- the time field. One of the time field constants.public int getActualMinimum(int field)
field
- the time field. One of the time field constants.ArrayIndexOutOfBoundsException
- if the field is outside
the valid range. The value of field must be >= 0 and
<= FIELD_COUNT
.public int getActualMaximum(int field)
field
- the time field. One of the time field constants.ArrayIndexOutOfBoundsException
- if the field is outside
the valid range. The value of field must be >= 0 and
<= FIELD_COUNT
.public int compareTo(Calendar cal)
compareTo
in interface Comparable<Calendar>
cal
- the calendar to which the time should be compared.cal
- the calendar to compare this instance with.cal
, or more than 0 if the time of this calendar is after
that of cal
.NullPointerException
- if cal
is null.IllegalArgumentException
- if either calendar has fields set to
invalid values.public String toString()
toString
in class Object
Object.getClass()
,
Object.hashCode()
,
Class.getName()
,
Integer.toHexString(int)
public String getDisplayName(int field, int style, Locale locale)
null
is returned. If one does exist,
then the value is obtained from get(int)
and converted
appropriately. For example, if the MONTH
field is
requested, then get(MONTH)
is called. This is then
converted to a textual representation based on its value and
the style requested; if the LONG
style is requested
and the returned value is 11
from a
GregorianCalendar
implementation, then "December"
is returned. By default, a textual representation is available
for all fields which have an applicable value obtainable from
DateFormatSymbols
.field
- the calendar field whose textual representation should
be obtained.style
- the style to use; either LONG
or SHORT
.locale
- the locale to use for translation.null
if none is applicable.IllegalArgumentException
- if field
or style
or invalid, or the calendar is non-lenient
and has invalid values.NullPointerException
- if locale
is null
.public Map<String,Integer> getDisplayNames(int field, int style, Locale locale)
LONG
is specified and the German locale, then the map will
contain "Montag" to MONDAY
, "Dienstag" to
TUESDAY
, "Mittwoch" to WEDNESDAY
and
so on. The default implementation uses the values returned
by DateFormatSymbols
so, for example, the style
ALL_STYLES
and the field MONTH
will return
a map filled with the values returned from
DateFormatSymbols.getMonths()
and
DateFormatSymbols.getShortMonths()
. If there are
no textual representations for a given field (usually because
it is purely numeric, such as the year in the
GregorianCalendar
), null
is returned.field
- the calendar field whose textual representation should
be obtained.style
- the style to use; either LONG
, SHORT
or ALL_STYLES
.locale
- the locale to use for translation.null
if none is applicable.IllegalArgumentException
- if field
or style
or invalid, or the calendar is non-lenient
and has invalid values.NullPointerException
- if locale
is null
.