public abstract class Calendar extends Object implements 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
Modifier 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.
|
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.
|
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.
|
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.
|
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 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.
|
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.
|
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()
Gets a calendar using the default time zone and locale.
|
static Calendar |
getInstance(Locale aLocale)
Gets a calendar using the default time zone and specified 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.
|
int |
hashCode()
Returns a hash code for this calendar.
|
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.
|
String |
toString()
Returns a string representation of this object.
|
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 AM
public static final int AM_PM
public static final int APRIL
public static final int AUGUST
public static final int DATE
public static final int DAY_OF_MONTH
public static final int DAY_OF_WEEK
public static final int DAY_OF_WEEK_IN_MONTH
public static final int DAY_OF_YEAR
public static final int DECEMBER
public static final int DST_OFFSET
public static final int ERA
public static final int FEBRUARY
public static final int FIELD_COUNT
public static final int FRIDAY
public static final int HOUR
public static final int HOUR_OF_DAY
public static final int JANUARY
public static final int JULY
public static final int JUNE
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.public static final int MARCH
public static final int MAY
public static final int MILLISECOND
public static final int MINUTE
public static final int MONDAY
public static final int MONTH
public static final int NOVEMBER
public static final int OCTOBER
public static final int PM
public static final int SATURDAY
public static final int SECOND
public static final int SEPTEMBER
public 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 SUNDAY
public static final int THURSDAY
public static final int TUESDAY
public static final int UNDECIMBER
public static final int WEDNESDAY
public static final int WEEK_OF_MONTH
setFirstDayOfWeek(int)
,
Constant Field Valuespublic static final int WEEK_OF_YEAR
setFirstDayOfWeek(int)
,
Constant Field Valuespublic static final int YEAR
public static final int ZONE_OFFSET
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 boolean after(Object o)
o
- the object to that we should compare.ClassCastException
- if o is not an calendar.public boolean before(Object o)
o
- the object to that we should compare.ClassCastException
- if o is not an calendar.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 Object clone()
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 boolean equals(Object o)
public 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
.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 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 getFirstDayOfWeek()
public abstract int getGreatestMinimum(int field)
field
- the time field. One of the time field constants.public static Calendar getInstance()
Calendar
returned is based on the current time
in the default time zone with the default locale.public static Calendar getInstance(Locale aLocale)
Calendar
returned is based on the current time
in the default time zone with the given locale.aLocale
- the locale for the week datapublic abstract int getLeastMaximum(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 int getMinimalDaysInFirstWeek()
setMinimalDaysInFirstWeek(int)
public abstract int getMinimum(int field)
field
- the time field. One of the time field constants.public final Date getTime()
Date
-Object.public long getTimeInMillis()
public int hashCode()
public boolean isLenient()
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
.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 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 void setFirstDayOfWeek(int value)
value
- the first day of week. One of SUNDAY to SATURDAY.public void setLenient(boolean lenient)
lenient
- true, if the date should be interpreted linient,
false if it should be interpreted strict.public void setMinimalDaysInFirstWeek(int value)
value
- the minimal days required in the first week.public final void setTime(Date date)
date
- the date (null
not permitted).NullPointerException
- if date
is null
.public void setTimeInMillis(long time)
time
- the time in milliseconds since the epoch