Package java.util
Class Calendar
- java.lang.Object
-
- java.util.Calendar
-
- All Implemented Interfaces:
Cloneable
- Direct Known Subclasses:
GregorianCalendar
public abstract class Calendar extends Object implements Cloneable
-
-
Field Summary
Fields Modifier and Type Field Description static intAMValue of theAM_PMfield indicating the period of the day from midnight to just before noon.static intAM_PMField number forgetandsetindicating whether theHOURis before or after noon.static intAPRILValue of theMONTHfield indicating the fourth month of the year in the Gregorian and Julian calendars.static intAUGUSTValue of theMONTHfield indicating the eighth month of the year in the Gregorian and Julian calendars.static intDATEField number forgetandsetindicating the day of the month.static intDAY_OF_MONTHField number forgetandsetindicating the day of the month.static intDAY_OF_WEEKField number forgetandsetindicating the day of the week.static intDAY_OF_WEEK_IN_MONTHField number forgetandsetindicating the ordinal number of the day of the week within the current month.static intDAY_OF_YEARField number forgetandsetindicating the day number within the current year.static intDECEMBERValue of theMONTHfield indicating the twelfth month of the year in the Gregorian and Julian calendars.static intERAField number forgetandsetindicating the era, e.g., AD or BC in the Julian calendar.static intFEBRUARYValue of theMONTHfield indicating the second month of the year in the Gregorian and Julian calendars.static intFRIDAYValue of theDAY_OF_WEEKfield indicating Friday.static intHOURField number forgetandsetindicating the hour of the morning or afternoon.static intHOUR_OF_DAYField number forgetandsetindicating the hour of the day.static intJANUARYValue of theMONTHfield indicating the first month of the year in the Gregorian and Julian calendars.static intJULYValue of theMONTHfield indicating the seventh month of the year in the Gregorian and Julian calendars.static intJUNEValue of theMONTHfield indicating the sixth month of the year in the Gregorian and Julian calendars.static intMARCHValue of theMONTHfield indicating the third month of the year in the Gregorian and Julian calendars.static intMAYValue of theMONTHfield indicating the fifth month of the year in the Gregorian and Julian calendars.static intMILLISECONDField number forgetandsetindicating the millisecond within the second.static intMINUTEField number forgetandsetindicating the minute within the hour.static intMONDAYValue of theDAY_OF_WEEKfield indicating Monday.static intMONTHField number forgetandsetindicating the month.static intNOVEMBERValue of theMONTHfield indicating the eleventh month of the year in the Gregorian and Julian calendars.static intOCTOBERValue of theMONTHfield indicating the tenth month of the year in the Gregorian and Julian calendars.static intPMValue of theAM_PMfield indicating the period of the day from noon to just before midnight.static intSATURDAYValue of theDAY_OF_WEEKfield indicating Saturday.static intSECONDField number forgetandsetindicating the second within the minute.static intSEPTEMBERValue of theMONTHfield indicating the ninth month of the year in the Gregorian and Julian calendars.static intSUNDAYValue of theDAY_OF_WEEKfield indicating Sunday.static intTHURSDAYValue of theDAY_OF_WEEKfield indicating Thursday.static intTUESDAYValue of theDAY_OF_WEEKfield indicating Tuesday.static intUNDECIMBERValue of theMONTHfield indicating the thirteenth month of the year.static intWEDNESDAYValue of theDAY_OF_WEEKfield indicating Wednesday.static intWEEK_OF_MONTHField number forgetandsetindicating the week number within the current month.static intWEEK_OF_YEARField number forgetandsetindicating the week number within the current year.static intYEARField number forgetandsetindicating the year.
-
Constructor Summary
Constructors Modifier Constructor Description protectedCalendar()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract voidadd(int field, int value)booleanafter(Object when)Returns whether thisCalendarrepresents a time after the time represented by the specifiedObject.booleanbefore(Object when)Returns whether thisCalendarrepresents a time before the time represented by the specifiedObject.abstract voidclear()abstract Objectclone()intcompareTo(Calendar anotherCalendar)Compares the time values (millisecond offsets from the Epoch) represented by twoCalendarobjects.booleanequals(Object obj)Compares thisCalendarto the specifiedObject.abstract intget(int field)intgetActualMaximum(int field)intgetActualMinimum(int field)intgetFirstDayOfWeek()static CalendargetInstance()intgetMinimalDaysInFirstWeek()abstract DategetTime()abstract longgetTimeInMillis()Returns this Calendar's time value in milliseconds.booleanisLenient()Tells whether date/time interpretation is to be lenient.abstract voidset(int field, int value)Sets the given calendar field to the given value.voidsetFirstDayOfWeek(int newFirstDayOfWeek)voidsetLenient(boolean lenient)Specifies whether or not date/time interpretation is to be lenient.voidsetMinimalDaysInFirstWeek(int newMinimalDaysInFirstWeek)abstract voidsetTime(Date date)Sets this Calendar's time with the givenDate.abstract voidsetTimeInMillis(long millis)Sets this Calendar's current time from the given long value.
-
-
-
Field Detail
-
ERA
public static final int ERA
Field number forgetandsetindicating the era, e.g., AD or BC in the Julian calendar. This is a calendar-specific value; see subclass documentation.
-
YEAR
public static final int YEAR
Field number forgetandsetindicating the year. This is a calendar-specific value; see subclass documentation.- See Also:
- Constant Field Values
-
MONTH
public static final int MONTH
Field number forgetandsetindicating the month. This is a calendar-specific value. The first month of the year in the Gregorian and Julian calendars isJANUARYwhich is 0; the last depends on the number of months in a year.
-
WEEK_OF_YEAR
public static final int WEEK_OF_YEAR
Field number forgetandsetindicating the week number within the current year. The first week of the year, as defined bygetFirstDayOfWeek()andgetMinimalDaysInFirstWeek(), has value 1. Subclasses define the value ofWEEK_OF_YEARfor days before the first week of the year.
-
WEEK_OF_MONTH
public static final int WEEK_OF_MONTH
Field number forgetandsetindicating the week number within the current month. The first week of the month, as defined bygetFirstDayOfWeek()andgetMinimalDaysInFirstWeek(), has value 1. Subclasses define the value ofWEEK_OF_MONTHfor days before the first week of the month.
-
DATE
public static final int DATE
Field number forgetandsetindicating the day of the month. This is a synonym forDAY_OF_MONTH. The first day of the month has value 1.- See Also:
DAY_OF_MONTH, Constant Field Values
-
DAY_OF_MONTH
public static final int DAY_OF_MONTH
Field number forgetandsetindicating the day of the month. This is a synonym forDATE. The first day of the month has value 1.- See Also:
DATE, Constant Field Values
-
DAY_OF_YEAR
public static final int DAY_OF_YEAR
Field number forgetandsetindicating the day number within the current year. The first day of the year has value 1.- See Also:
- Constant Field Values
-
DAY_OF_WEEK
public static final int DAY_OF_WEEK
Field number forgetandsetindicating the day of the week. This field takes valuesSUNDAY,MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY, andSATURDAY.
-
DAY_OF_WEEK_IN_MONTH
public static final int DAY_OF_WEEK_IN_MONTH
Field number forgetandsetindicating the ordinal number of the day of the week within the current month. Together with theDAY_OF_WEEKfield, this uniquely specifies a day within a month. UnlikeWEEK_OF_MONTHandWEEK_OF_YEAR, this field's value does not depend ongetFirstDayOfWeek()orgetMinimalDaysInFirstWeek().DAY_OF_MONTH 1through7always correspond toDAY_OF_WEEK_IN_MONTH 1;8through14correspond toDAY_OF_WEEK_IN_MONTH 2, and so on.DAY_OF_WEEK_IN_MONTH 0indicates the week beforeDAY_OF_WEEK_IN_MONTH 1. Negative values count back from the end of the month, so the last Sunday of a month is specified asDAY_OF_WEEK = SUNDAY, DAY_OF_WEEK_IN_MONTH = -1. Because negative values count backward they will usually be aligned differently within the month than positive values. For example, if a month has 31 days,DAY_OF_WEEK_IN_MONTH -1will overlapDAY_OF_WEEK_IN_MONTH 5and the end of4.- See Also:
DAY_OF_WEEK,WEEK_OF_MONTH, Constant Field Values
-
AM_PM
public static final int AM_PM
Field number forgetandsetindicating whether theHOURis before or after noon. E.g., at 10:04:15.250 PM theAM_PMisPM.- See Also:
AM,PM,HOUR, Constant Field Values
-
HOUR
public static final int HOUR
Field number forgetandsetindicating the hour of the morning or afternoon.HOURis used for the 12-hour clock (0 - 11). Noon and midnight are represented by 0, not by 12. E.g., at 10:04:15.250 PM theHOURis 10.- See Also:
AM_PM,HOUR_OF_DAY, Constant Field Values
-
HOUR_OF_DAY
public static final int HOUR_OF_DAY
Field number forgetandsetindicating the hour of the day.HOUR_OF_DAYis used for the 24-hour clock. E.g., at 10:04:15.250 PM theHOUR_OF_DAYis 22.- See Also:
HOUR, Constant Field Values
-
MINUTE
public static final int MINUTE
Field number forgetandsetindicating the minute within the hour. E.g., at 10:04:15.250 PM theMINUTEis 4.- See Also:
- Constant Field Values
-
SECOND
public static final int SECOND
Field number forgetandsetindicating the second within the minute. E.g., at 10:04:15.250 PM theSECONDis 15.- See Also:
- Constant Field Values
-
MILLISECOND
public static final int MILLISECOND
Field number forgetandsetindicating the millisecond within the second. E.g., at 10:04:15.250 PM theMILLISECONDis 250.- See Also:
- Constant Field Values
-
SUNDAY
public static final int SUNDAY
Value of theDAY_OF_WEEKfield indicating Sunday.- See Also:
- Constant Field Values
-
MONDAY
public static final int MONDAY
Value of theDAY_OF_WEEKfield indicating Monday.- See Also:
- Constant Field Values
-
TUESDAY
public static final int TUESDAY
Value of theDAY_OF_WEEKfield indicating Tuesday.- See Also:
- Constant Field Values
-
WEDNESDAY
public static final int WEDNESDAY
Value of theDAY_OF_WEEKfield indicating Wednesday.- See Also:
- Constant Field Values
-
THURSDAY
public static final int THURSDAY
Value of theDAY_OF_WEEKfield indicating Thursday.- See Also:
- Constant Field Values
-
FRIDAY
public static final int FRIDAY
Value of theDAY_OF_WEEKfield indicating Friday.- See Also:
- Constant Field Values
-
SATURDAY
public static final int SATURDAY
Value of theDAY_OF_WEEKfield indicating Saturday.- See Also:
- Constant Field Values
-
JANUARY
public static final int JANUARY
Value of theMONTHfield indicating the first month of the year in the Gregorian and Julian calendars.- See Also:
- Constant Field Values
-
FEBRUARY
public static final int FEBRUARY
Value of theMONTHfield indicating the second month of the year in the Gregorian and Julian calendars.- See Also:
- Constant Field Values
-
MARCH
public static final int MARCH
Value of theMONTHfield indicating the third month of the year in the Gregorian and Julian calendars.- See Also:
- Constant Field Values
-
APRIL
public static final int APRIL
Value of theMONTHfield indicating the fourth month of the year in the Gregorian and Julian calendars.- See Also:
- Constant Field Values
-
MAY
public static final int MAY
Value of theMONTHfield indicating the fifth month of the year in the Gregorian and Julian calendars.- See Also:
- Constant Field Values
-
JUNE
public static final int JUNE
Value of theMONTHfield indicating the sixth month of the year in the Gregorian and Julian calendars.- See Also:
- Constant Field Values
-
JULY
public static final int JULY
Value of theMONTHfield indicating the seventh month of the year in the Gregorian and Julian calendars.- See Also:
- Constant Field Values
-
AUGUST
public static final int AUGUST
Value of theMONTHfield indicating the eighth month of the year in the Gregorian and Julian calendars.- See Also:
- Constant Field Values
-
SEPTEMBER
public static final int SEPTEMBER
Value of theMONTHfield indicating the ninth month of the year in the Gregorian and Julian calendars.- See Also:
- Constant Field Values
-
OCTOBER
public static final int OCTOBER
Value of theMONTHfield indicating the tenth month of the year in the Gregorian and Julian calendars.- See Also:
- Constant Field Values
-
NOVEMBER
public static final int NOVEMBER
Value of theMONTHfield indicating the eleventh month of the year in the Gregorian and Julian calendars.- See Also:
- Constant Field Values
-
DECEMBER
public static final int DECEMBER
Value of theMONTHfield indicating the twelfth month of the year in the Gregorian and Julian calendars.- See Also:
- Constant Field Values
-
UNDECIMBER
public static final int UNDECIMBER
Value of theMONTHfield indicating the thirteenth month of the year. AlthoughGregorianCalendardoes not use this value, lunar calendars do.- See Also:
- Constant Field Values
-
AM
public static final int AM
Value of theAM_PMfield indicating the period of the day from midnight to just before noon.- See Also:
- Constant Field Values
-
PM
public static final int PM
Value of theAM_PMfield indicating the period of the day from noon to just before midnight.- See Also:
- Constant Field Values
-
-
Method Detail
-
getTime
public abstract Date getTime()
- Returns:
- a
Daterepresenting the time value. - See Also:
setTime(Date),getTimeInMillis()
-
setTime
public abstract void setTime(Date date)
Sets this Calendar's time with the givenDate.Note: Calling
setTime()withDate(Long.MAX_VALUE)orDate(Long.MIN_VALUE)may yield incorrect field values fromget().- Parameters:
date- the given Date.- See Also:
getTime(),setTimeInMillis(long)
-
getTimeInMillis
public abstract long getTimeInMillis()
Returns this Calendar's time value in milliseconds.- Returns:
- the current time as UTC milliseconds from the epoch.
- See Also:
getTime(),setTimeInMillis(long)
-
setTimeInMillis
public abstract void setTimeInMillis(long millis)
Sets this Calendar's current time from the given long value.- Parameters:
millis- the new time in UTC milliseconds from the epoch.- See Also:
setTime(Date),getTimeInMillis()
-
get
public abstract int get(int field)
-
set
public abstract void set(int field, int value)Sets the given calendar field to the given value. The value is not interpreted by this method regardless of the leniency mode.- Parameters:
field- the given calendar field.value- the value to be set for the given calendar field.- Throws:
ArrayIndexOutOfBoundsException- if the specified field is out of range (field < 0 || field >= FIELD_COUNT). in non-lenient mode.- See Also:
#set(int,int,int),#set(int,int,int,int,int),#set(int,int,int,int,int,int),get(int)
-
add
public abstract void add(int field, int value)
-
clear
public abstract void clear()
-
getFirstDayOfWeek
public int getFirstDayOfWeek()
-
setFirstDayOfWeek
public void setFirstDayOfWeek(int newFirstDayOfWeek)
-
getMinimalDaysInFirstWeek
public int getMinimalDaysInFirstWeek()
-
setMinimalDaysInFirstWeek
public void setMinimalDaysInFirstWeek(int newMinimalDaysInFirstWeek)
-
getInstance
public static Calendar getInstance()
-
before
public boolean before(Object when)
Returns whether thisCalendarrepresents a time before the time represented by the specifiedObject. This method is equivalent to:
if and only ifcompareTo(when) < 0
whenis aCalendarinstance. Otherwise, the method returnsfalse.- Parameters:
when- theObjectto be compared- Returns:
trueif the time of thisCalendaris before the time represented bywhen;falseotherwise.- See Also:
compareTo(Calendar)
-
after
public boolean after(Object when)
Returns whether thisCalendarrepresents a time after the time represented by the specifiedObject. This method is equivalent to:
if and only ifcompareTo(when) > 0
whenis aCalendarinstance. Otherwise, the method returnsfalse.- Parameters:
when- theObjectto be compared- Returns:
trueif the time of thisCalendaris after the time represented bywhen;falseotherwise.- See Also:
compareTo(Calendar)
-
equals
public boolean equals(Object obj)
Compares thisCalendarto the specifiedObject. The result istrueif and only if the argument is aCalendarobject of the same calendar system that represents the same time value (millisecond offset from the Epoch) under the sameCalendarparameters as this object.The
Calendarparameters are the values represented by theisLenient,getFirstDayOfWeek,getMinimalDaysInFirstWeekandgetTimeZonemethods. If there is any difference in those parameters between the twoCalendars, this method returnsfalse.Use the
compareTomethod to compare only the time values.
-
compareTo
public int compareTo(Calendar anotherCalendar)
Compares the time values (millisecond offsets from the Epoch) represented by twoCalendarobjects.- Parameters:
anotherCalendar- theCalendarto be compared.- Returns:
- the value
0if the time represented by the argument is equal to the time represented by thisCalendar; a value less than0if the time of thisCalendaris before the time represented by the argument; and a value greater than0if the time of thisCalendaris after the time represented by the argument. - Throws:
NullPointerException- if the specifiedCalendarisnull.IllegalArgumentException- if the time value of the specifiedCalendarobject can't be obtained due to any invalid calendar values.- Since:
- 1.5
-
setLenient
public void setLenient(boolean lenient)
Specifies 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 (non-lenient) interpretation, such dates will cause an exception to be thrown. The default is lenient.- Parameters:
lenient-trueif the lenient mode is to be turned on;falseif it is to be turned off.- See Also:
isLenient(),DateFormat.setLenient(boolean)
-
isLenient
public boolean isLenient()
Tells whether date/time interpretation is to be lenient.- Returns:
trueif the interpretation mode of this calendar is lenient;falseotherwise.- See Also:
setLenient(boolean)
-
getActualMaximum
public int getActualMaximum(int field)
-
getActualMinimum
public int getActualMinimum(int field)
-
-