Class Calendar
- java.lang.Object
-
- java.util.Calendar
-
- All Implemented Interfaces:
Serializable,Cloneable,Comparable<Calendar>
- Direct Known Subclasses:
GregorianCalendar
public abstract class Calendar extends Object implements Serializable, Cloneable, Comparable<Calendar>
Calendaris an abstract base class for converting between aDateobject and a set of integer fields such asYEAR,MONTH,DAY,HOUR, and so on. (ADateobject represents a specific instant in time with millisecond precision. SeeDatefor information about theDateclass.)Subclasses of
Calendarinterpret aDateaccording to the rules of a specific calendar system.Like other locale-sensitive classes,
Calendarprovides a class method,getInstance, for getting a default instance of this class for general use.Calendar'sgetInstancemethod returns a calendar whose locale is based on system settings and whose time fields have been initialized with the current date and time:Calendar rightNow = Calendar.getInstance()
A
Calendarobject 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).Calendardefines the range of values returned by certain fields, as well as their meaning. For example, the first month of the year has valueMONTH==JANUARYfor all calendars. Other values are defined by the concrete subclass, such asERAandYEAR. See individual field documentation and subclass documentation for details.When a
Calendaris lenient, it accepts a wider range of field values than it produces. For example, a lenientGregorianCalendarinterpretsMONTH==JANUARY,DAY_OF_MONTH== 32 as February 1. A non-lenientGregorianCalendarthrows an exception when given out-of-range field settings. When calendars recompute field values for return byget(), they normalize them. For example, aGregorianCalendaralways producesDAY_OF_MONTHvalues between 1 and the length of the month.Calendardefines a locale-specific seven day week using two parameters: the first day of the week and the minimal days in first week (from 1 to 7). These numbers are taken from the locale resource data when aCalendaris constructed. They may also be specified explicitly through the API.When setting or getting the
WEEK_OF_MONTHorWEEK_OF_YEARfields,Calendarmust determine the first week of the month or year as a reference point. The first week of a month or year is defined as the earliest seven day period beginning ongetFirstDayOfWeek()and containing at leastgetMinimalDaysInFirstWeek()days of that month or year. Weeks numbered ..., -1, 0 precede the first week; weeks numbered 2, 3,... follow it. Note that the normalized numbering returned byget()may be different. For example, a specificCalendarsubclass may designate the week before week 1 of a year as week n of the previous year.When computing a
Datefrom time fields, two special circumstances may arise: there may be insufficient information to compute theDate(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. If fields conflict, the calendar will give preference to fields set more recently. For example, when determining the day, the calendar will look for one of the following combinations of fields. The most recent combination, as determined by the most recently set single field, will be used.
For the time of day:MONTH + DAY_OF_MONTH MONTH + WEEK_OF_MONTH + DAY_OF_WEEK MONTH + DAY_OF_WEEK_IN_MONTH + DAY_OF_WEEK DAY_OF_YEAR DAY_OF_WEEK + WEEK_OF_YEAR
HOUR_OF_DAY AM_PM + HOUR
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 form a sequence of three consecutive minutes in time.
- Although historically not precise, midnight also belongs to "am", and noon belongs to "pm", so on the same day, we have 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
DateFormatto format dates.Field manipulation methods
Calendarfields can be changed using three methods:set(),add(), androll().set(f, value)changes fieldftovalue. In addition, it sets an internal member variable to indicate that fieldfhas been changed. Although fieldfis changed immediately, the calendar's milliseconds is not recomputed until the next call toget(),getTime(), orgetTimeInMillis()is made. Thus, multiple calls toset()do not trigger multiple, unnecessary computations. As a result of changing a field usingset(), other fields may also change, depending on the field, the field value, and the calendar system. In addition,get(f)will not necessarily returnvalueafter the fields have been recomputed. The specifics are determined by the concrete calendar class.Example: Consider a
GregorianCalendaroriginally set to August 31, 1999. Callingset(Calendar.MONTH, Calendar.SEPTEMBER)sets the calendar to September 31, 1999. This is a temporary internal representation that resolves to October 1, 1999 ifgetTime()is then called. However, a call toset(Calendar.DAY_OF_MONTH, 30)before the call togetTime()sets the calendar to September 30, 1999, since no recomputation occurs afterset()itself.add(f, delta)addsdeltato fieldf. This is equivalent to callingset(f, get(f) + delta)with two adjustments:Add rule 1. The value of field
fafter the call minus the value of fieldfbefore the call isdelta, modulo any overflow that has occurred in fieldf. Overflow occurs when a field value exceeds its range and, as a result, the next larger field is incremented or decremented and the field value is adjusted back into its range.Add rule 2. If a smaller field is expected to be invariant, but it is impossible for it to be equal to its prior value because of changes in its minimum or maximum after field
fis changed, then its value is adjusted to be as close as possible to its expected value. A smaller field represents a smaller unit of time.HOURis a smaller field thanDAY_OF_MONTH. No adjustment is made to smaller fields that are not expected to be invariant. The calendar system determines what fields are expected to be invariant.In addition, unlike
set(),add()forces an immediate recomputation of the calendar's milliseconds and all fields.Example: Consider a
GregorianCalendaroriginally set to August 31, 1999. Callingadd(Calendar.MONTH, 13)sets the calendar to September 30, 2000. Add rule 1 sets theMONTHfield to September, since adding 13 months to August gives September of the next year. SinceDAY_OF_MONTHcannot be 31 in September in aGregorianCalendar, add rule 2 sets theDAY_OF_MONTHto 30, the closest possible value. Although it is a smaller field,DAY_OF_WEEKis not adjusted by rule 2, since it is expected to change when the month changes in aGregorianCalendar.roll(f, delta)addsdeltato fieldfwithout changing larger fields. This is equivalent to callingadd(f, delta)with the following adjustment:Roll rule. Larger fields are unchanged after the call. A larger field represents a larger unit of time.
DAY_OF_MONTHis a larger field thanHOUR.Example: Consider a
GregorianCalendaroriginally set to August 31, 1999. Callingroll(Calendar.MONTH, 8)sets the calendar to April 30, 1999. Add rule 1 sets theMONTHfield to April. Using aGregorianCalendar, theDAY_OF_MONTHcannot be 31 in the month April. Add rule 2 sets it to the closest possible value, 30. Finally, the roll rule maintains theYEARfield value of 1999.Example: Consider a
GregorianCalendaroriginally set to Sunday June 6, 1999. Callingroll(Calendar.WEEK_OF_MONTH, -1)sets the calendar to Tuesday June 1, 1999, whereas callingadd(Calendar.WEEK_OF_MONTH, -1)sets the calendar to Sunday May 30, 1999. This is because the roll rule imposes an additional constraint: TheMONTHmust not change when theWEEK_OF_MONTHis rolled. Taken together with add rule 1, the resultant date must be between Tuesday June 1 and Saturday June 5. According to add rule 2, theDAY_OF_WEEK, an invariant when changing theWEEK_OF_MONTH, is set to Tuesday, the closest possible value to Sunday (where Sunday is the first day of the week).Usage model. To motivate the behavior of
add()androll(), consider a user interface component with increment and decrement buttons for the month, day, and year, and an underlyingGregorianCalendar. If the interface reads January 31, 1999 and the user presses the month increment button, what should it read? If the underlying implementation usesset(), it might read March 3, 1999. A better result would be February 28, 1999. Furthermore, if the user presses the month increment button again, it should read March 31, 1999, not March 28, 1999. By saving the original date and using eitheradd()orroll(), depending on whether larger fields should be affected, the user interface can behave as most users will intuitively expect.Note: You should always use
rollandaddrather than attempting to perform arithmetic operations directly on the fields of a Calendar. It is quite possible for Calendar subclasses to have fields with non-linear behavior, for example missing months or days during non-leap years. The subclasses' add and roll methods will take this into account, while simple arithmetic manipulations may give invalid results.- See Also:
Date,GregorianCalendar,TimeZone, Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static intALL_STYLESRequests bothSHORTandLONGstyles in the map returned bygetDisplayNames(int, int, java.util.Locale).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.protected booleanareFieldsSetTrue iff the values infields[]correspond totime.static intAUGUSTValue of theMONTHfield indicating the eighth month of the year.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.static intDST_OFFSETField number forgetandsetindicating the daylight savings offset from theZONE_OFFSETin milliseconds.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.static intFIELD_COUNTThis is the total number of fields in this calendar.protected int[]fieldsContains broken-down field values for the current value oftimeifareFieldsSetis true, or stale data corresponding to some previous value otherwise.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.protected boolean[]isSetWhether the corresponding element infield[]has been set.protected booleanisTimeSetWhethertimecorresponds to the values infields[].static intJANUARYValue of theMONTHfield indicating the first month of the year.static intJULYValue of theMONTHfield indicating the seventh month of the year.static intJUNEValue of theMONTHfield indicating the sixth month of the year.static intLONGRequests long names (such as "January") fromgetDisplayName(int, int, java.util.Locale)orgetDisplayNames(int, int, java.util.Locale).static intMARCHValue of theMONTHfield indicating the third month of the year.static intMAYValue of theMONTHfield indicating the fifth month of the year.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.static intOCTOBERValue of theMONTHfield indicating the tenth month of the year.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.static intSHORTRequests short names (such as "Jan") fromgetDisplayName(int, int, java.util.Locale)orgetDisplayNames(int, int, java.util.Locale).static intSUNDAYValue of theDAY_OF_WEEKfield indicating Sunday.static intTHURSDAYValue of theDAY_OF_WEEKfield indicating Thursday.protected longtimeA time in milliseconds since January 1, 1970.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.static intZONE_OFFSETField number forgetandsetindicating the raw (non-DST) offset from GMT in milliseconds.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract voidadd(int field, int value)Adds the given amount to aCalendarfield.booleanafter(Object calendar)Returns whether theDaterepresented by thisCalendarinstance is after theDaterepresented by the parameter.booleanbefore(Object calendar)Returns whether theDaterepresented by thisCalendarinstance is before theDaterepresented by the parameter.voidclear()Clears the values of all the time fields, marking them all unset and assigning them all a value of zero.voidclear(int field)Clears the value in the given time field, marking it unset and assigning it a value of zero.Objectclone()Returns a partially deep copy of thisCalendar; all fields from from theCalendarclass are cloned (deep copy) but fields from subclasses aren't (shallow copy).intcompareTo(Calendar anotherCalendar)Compares the time represented by thisCalendarto that represented by the givenCalendar.protected voidcomplete()Computes the time from the fields if the time has not already been set.protected abstract voidcomputeFields()Computes theCalendarfields fromtime.protected abstract voidcomputeTime()Computestimefrom the Calendar fields.booleanequals(Object object)Compares the given object to thisCalendarand returns whether they are equal.intget(int field)Returns the value of the given field after computing the field values by callingcomplete()first.intgetActualMaximum(int field)Returns the maximum value of the given field for the current date.intgetActualMinimum(int field)Returns the minimum value of the given field for the current date.static Locale[]getAvailableLocales()Returns an array of locales for which customCalendarinstances are available.StringgetDisplayName(int field, int style, Locale locale)Returns a human-readable string for the value offieldusing the given style and locale.Map<String,Integer>getDisplayNames(int field, int style, Locale locale)Returns a map of human-readable strings to corresponding values, for the given field, style, and locale.intgetFirstDayOfWeek()Returns the first day of the week for thisCalendar.abstract intgetGreatestMinimum(int field)Returns the greatest minimum value of the given field.static CalendargetInstance()Constructs a new instance of theCalendarsubclass appropriate for the defaultLocaleand defaultTimeZone, set to the current date and time.static CalendargetInstance(Locale locale)Constructs a new instance of theCalendarsubclass appropriate for the givenLocaleand defaultTimeZone, set to the current date and time.static CalendargetInstance(TimeZone timezone)Constructs a new instance of theCalendarsubclass appropriate for the defaultLocaleand givenTimeZone, set to the current date and time.static CalendargetInstance(TimeZone timezone, Locale locale)Constructs a new instance of theCalendarsubclass appropriate for the givenLocaleand givenTimeZone, set to the current date and time.abstract intgetLeastMaximum(int field)Returns the smallest maximum value of the given field.abstract intgetMaximum(int field)Returns the greatest maximum value of the given field.intgetMinimalDaysInFirstWeek()Returns the minimal days in the first week of the year.abstract intgetMinimum(int field)Returns the smallest minimum value of the given field. this returns the smallest value thatgetcan return for the given field.DategetTime()Returns the time of thisCalendaras aDateobject.longgetTimeInMillis()Returns the time represented by thisCalendar, recomputing the time from its fields if necessary.TimeZonegetTimeZone()Returns the time zone used by thisCalendar.inthashCode()Returns an integer hash code for this object.protected intinternalGet(int field)Returns the value of the given field without recomputing.booleanisLenient()Tests whether thisCalendaraccepts field values which are outside the valid range for the field.booleanisSet(int field)Tests whether the given field is set.abstract voidroll(int field, boolean increment)Increment or decrement the given field and wrap the value of the field when it goes beyond the maximum or minimum value for the current date.voidroll(int field, int value)Adds the given amount to the given field and wraps the value of the field when it goes beyond the maximum or minimum value for the current date.voidset(int field, int value)Sets the given field to the given value.voidset(int year, int month, int day)Sets the year, month, and day of the month fields.voidset(int year, int month, int day, int hourOfDay, int minute)Sets the year, month, day of the month, hour of day, and minute fields.voidset(int year, int month, int day, int hourOfDay, int minute, int second)Sets the year, month, day of the month, hour of day, minute, and second fields.voidsetFirstDayOfWeek(int value)Sets the first day of the week for thisCalendar.voidsetLenient(boolean value)Sets whether thisCalendaraccepts field values which are outside the valid range for the field.voidsetMinimalDaysInFirstWeek(int value)Sets the minimal days in the first week of the year.voidsetTime(Date date)Sets the time of thisCalendar.voidsetTimeInMillis(long milliseconds)Sets the time of thisCalendarto the given Unix time.voidsetTimeZone(TimeZone timezone)Sets theTimeZoneused by this Calendar.StringtoString()Returns a string representation of thisCalendar, showing which fields are set.
-
-
-
Field Detail
-
areFieldsSet
protected boolean areFieldsSet
True iff the values infields[]correspond totime. Despite the name, this is effectively "are the values in fields[] up-to-date?" ---fields[]may contain non-zero values andisSet[]may containtruevalues even whenareFieldsSetis false. Accessing the fields viagetwill ensure the fields are up-to-date.
-
fields
protected int[] fields
Contains broken-down field values for the current value oftimeifareFieldsSetis true, or stale data corresponding to some previous value otherwise. Accessing the fields viagetwill ensure the fields are up-to-date. The array length is alwaysFIELD_COUNT.
-
isSet
protected boolean[] isSet
Whether the corresponding element infield[]has been set. Initially, these are all false. The first time the fields are computed, these are set to true and remain set even if the data becomes stale: you must checkareFieldsSetif you want to know whether the value is up-to-date. Note thatisSetis not a safe alternative to accessing this array directly, and will likewise return stale data! The array length is alwaysFIELD_COUNT.
-
isTimeSet
protected boolean isTimeSet
Whethertimecorresponds to the values infields[]. If false,timeis out-of-date with respect to changesfields[]. Accessing the time viagetTimeInMilliswill always return the correct value.
-
time
protected long time
A time in milliseconds since January 1, 1970. SeeisTimeSet. Accessing the time viagetTimeInMilliswill always return the correct value.
-
JANUARY
public static final int JANUARY
Value of theMONTHfield indicating the first month of the year.- See Also:
- Constant Field Values
-
FEBRUARY
public static final int FEBRUARY
Value of theMONTHfield indicating the second month of the year.- See Also:
- Constant Field Values
-
MARCH
public static final int MARCH
Value of theMONTHfield indicating the third month of the year.- See Also:
- Constant Field Values
-
APRIL
public static final int APRIL
Value of theMONTHfield indicating the fourth month of the year.- See Also:
- Constant Field Values
-
MAY
public static final int MAY
Value of theMONTHfield indicating the fifth month of the year.- See Also:
- Constant Field Values
-
JUNE
public static final int JUNE
Value of theMONTHfield indicating the sixth month of the year.- See Also:
- Constant Field Values
-
JULY
public static final int JULY
Value of theMONTHfield indicating the seventh month of the year.- See Also:
- Constant Field Values
-
AUGUST
public static final int AUGUST
Value of theMONTHfield indicating the eighth month of the year.- See Also:
- Constant Field Values
-
SEPTEMBER
public static final int SEPTEMBER
Value of theMONTHfield indicating the ninth month of the year.- See Also:
- Constant Field Values
-
OCTOBER
public static final int OCTOBER
Value of theMONTHfield indicating the tenth month of the year.- See Also:
- Constant Field Values
-
NOVEMBER
public static final int NOVEMBER
Value of theMONTHfield indicating the eleventh month of the year.- See Also:
- Constant Field Values
-
DECEMBER
public static final int DECEMBER
Value of theMONTHfield indicating the twelfth month of the year.- 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
-
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
-
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 isJANUARY; 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;8through15correspond 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. 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
-
ZONE_OFFSET
public static final int ZONE_OFFSET
Field number forgetandsetindicating the raw (non-DST) offset from GMT in milliseconds. Equivalent toTimeZone.getRawOffset().To determine the total offset from GMT at the time represented by this calendar, you will need to add the
ZONE_OFFSETandDST_OFFSETfields.- See Also:
- Constant Field Values
-
DST_OFFSET
public static final int DST_OFFSET
Field number forgetandsetindicating the daylight savings offset from theZONE_OFFSETin milliseconds. Equivalent toTimeZone.getDSTSavings()if the represented time falls inside DST, or 0 otherwise.To determine the total offset from GMT at the time represented by this calendar, you will need to add the
ZONE_OFFSETandDST_OFFSETfields.- See Also:
- Constant Field Values
-
FIELD_COUNT
public static final int FIELD_COUNT
This is the total number of fields in this calendar.- 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
-
ALL_STYLES
public static final int ALL_STYLES
Requests bothSHORTandLONGstyles in the map returned bygetDisplayNames(int, int, java.util.Locale).- Since:
- 1.6
- See Also:
- Constant Field Values
-
SHORT
public static final int SHORT
Requests short names (such as "Jan") fromgetDisplayName(int, int, java.util.Locale)orgetDisplayNames(int, int, java.util.Locale).- Since:
- 1.6
- See Also:
- Constant Field Values
-
LONG
public static final int LONG
Requests long names (such as "January") fromgetDisplayName(int, int, java.util.Locale)orgetDisplayNames(int, int, java.util.Locale).- Since:
- 1.6
- See Also:
- Constant Field Values
-
-
Method Detail
-
add
public abstract void add(int field, int value)Adds the given amount to aCalendarfield.- Parameters:
field- theCalendarfield to modify.value- the amount to add to the field.- Throws:
IllegalArgumentException- iffieldisDST_OFFSETorZONE_OFFSET.
-
after
public boolean after(Object calendar)
Returns whether theDaterepresented by thisCalendarinstance is after theDaterepresented by the parameter. The comparison is not dependent on the time zones of theCalendar.- Parameters:
calendar- theCalendarinstance to compare.- Returns:
truewhen this Calendar is after calendar,falseotherwise.- Throws:
IllegalArgumentException- if the time is not set and the time cannot be computed from the current field values.
-
before
public boolean before(Object calendar)
Returns whether theDaterepresented by thisCalendarinstance is before theDaterepresented by the parameter. The comparison is not dependent on the time zones of theCalendar.- Parameters:
calendar- theCalendarinstance to compare.- Returns:
truewhen this Calendar is before calendar,falseotherwise.- Throws:
IllegalArgumentException- if the time is not set and the time cannot be computed from the current field values.
-
clear
public final void clear()
Clears the values of all the time fields, marking them all unset and assigning them all a value of zero. The actual field values will be determined the next time the fields are accessed.
-
clear
public final void clear(int field)
Clears the value in the given time field, marking it unset and assigning it a value of zero. The actual field value will be determined the next time the field is accessed.
-
clone
public Object clone()
Returns a partially deep copy of thisCalendar; all fields from from theCalendarclass are cloned (deep copy) but fields from subclasses aren't (shallow copy).
-
complete
protected void complete()
Computes the time from the fields if the time has not already been set. Computes the fields from the time if the fields are not already set.- Throws:
IllegalArgumentException- if the time is not set and the time cannot be computed from the current field values.
-
computeFields
protected abstract void computeFields()
Computes theCalendarfields fromtime.
-
computeTime
protected abstract void computeTime()
Computestimefrom the Calendar fields.- Throws:
IllegalArgumentException- if the time cannot be computed from the current field values.
-
equals
public boolean equals(Object object)
Compares the given object to thisCalendarand returns whether they are equal. The object must be an instance ofCalendarand have the same properties.- Overrides:
equalsin classObject- Parameters:
object- the object to compare this instance with.- Returns:
trueif the given object is equal to thisCalendar,falseotherwise.- See Also:
Object.hashCode()
-
get
public int get(int field)
Returns the value of the given field after computing the field values by callingcomplete()first.- Throws:
IllegalArgumentException- if the fields are not set, the time is not set, and the time cannot be computed from the current field values.ArrayIndexOutOfBoundsException- if the field is not inside the range of possible fields. The range is starting at 0 up toFIELD_COUNT.
-
getActualMaximum
public int getActualMaximum(int field)
Returns the maximum value of the given field for the current date. For example, the maximum number of days in the current month.
-
getActualMinimum
public int getActualMinimum(int field)
Returns the minimum value of the given field for the current date.
-
getAvailableLocales
public static Locale[] getAvailableLocales()
Returns an array of locales for which customCalendarinstances are available.Note that Android does not support user-supplied locale service providers.
-
getFirstDayOfWeek
public int getFirstDayOfWeek()
Returns the first day of the week for thisCalendar.
-
getGreatestMinimum
public abstract int getGreatestMinimum(int field)
Returns the greatest minimum value of the given field. This is the biggest value thatgetActualMinimumcan return for any possible time.
-
getInstance
public static Calendar getInstance()
Constructs a new instance of theCalendarsubclass appropriate for the defaultLocaleand defaultTimeZone, set to the current date and time.
-
getInstance
public static Calendar getInstance(Locale locale)
Constructs a new instance of theCalendarsubclass appropriate for the givenLocaleand defaultTimeZone, set to the current date and time.
-
getInstance
public static Calendar getInstance(TimeZone timezone)
Constructs a new instance of theCalendarsubclass appropriate for the defaultLocaleand givenTimeZone, set to the current date and time.
-
getInstance
public static Calendar getInstance(TimeZone timezone, Locale locale)
Constructs a new instance of theCalendarsubclass appropriate for the givenLocaleand givenTimeZone, set to the current date and time.
-
getLeastMaximum
public abstract int getLeastMaximum(int field)
Returns the smallest maximum value of the given field. This is the smallest value thatgetActualMaximum()can return for any possible time.
-
getMaximum
public abstract int getMaximum(int field)
Returns the greatest maximum value of the given field. This returns the biggest value thatgetcan return for the given field.
-
getMinimalDaysInFirstWeek
public int getMinimalDaysInFirstWeek()
Returns the minimal days in the first week of the year.
-
getMinimum
public abstract int getMinimum(int field)
Returns the smallest minimum value of the given field. this returns the smallest value thatgetcan return for the given field.
-
getTime
public final Date getTime()
Returns the time of thisCalendaras aDateobject.- Throws:
IllegalArgumentException- if the time is not set and the time cannot be computed from the current field values.
-
getTimeInMillis
public long getTimeInMillis()
Returns the time represented by thisCalendar, recomputing the time from its fields if necessary.- Throws:
IllegalArgumentException- if the time is not set and the time cannot be computed from the current field values.
-
getTimeZone
public TimeZone getTimeZone()
Returns the time zone used by thisCalendar.
-
hashCode
public int hashCode()
Description copied from class:ObjectReturns an integer hash code for this object. By contract, any two objects for whichObject.equals(java.lang.Object)returnstruemust return the same hash code value. This means that subclasses ofObjectusually override both methods or neither method.Note that hash values must not change over time unless information used in equals comparisons also changes.
See Writing a correct
hashCodemethod if you intend implementing your ownhashCodemethod.- Overrides:
hashCodein classObject- Returns:
- this object's hash code.
- See Also:
Object.equals(java.lang.Object)
-
internalGet
protected final int internalGet(int field)
Returns the value of the given field without recomputing.
-
isLenient
public boolean isLenient()
Tests whether thisCalendaraccepts field values which are outside the valid range for the field.
-
isSet
public final boolean isSet(int field)
Tests whether the given field is set. Note that the interpretation of "is set" is somewhat technical. In particular, it does not mean that the field's value is up to date. If you want to know whether a field contains an up-to-date value, you must also checkareFieldsSet, making this method somewhat useless unless you're a subclass, in which case you can access theisSetarray directly.A field remains "set" from the first time its value is computed until it's cleared by one of the
clearmethods. Thus "set" does not mean "valid". You probably want to callget-- which will update fields as necessary -- rather than try to make use of this method.
-
roll
public void roll(int field, int value)Adds the given amount to the given field and wraps the value of the field when it goes beyond the maximum or minimum value for the current date. Other fields will be adjusted as required to maintain a consistent date.
-
roll
public abstract void roll(int field, boolean increment)Increment or decrement the given field and wrap the value of the field when it goes beyond the maximum or minimum value for the current date. Other fields will be adjusted as required to maintain a consistent date.
-
set
public void set(int field, int value)Sets the given field to the given value.
-
set
public final void set(int year, int month, int day)Sets the year, month, and day of the month fields. Other fields are not changed; callclear()first if this is not desired. The month value is 0-based, so it may be clearer to use a constant likeJANUARY.
-
set
public final void set(int year, int month, int day, int hourOfDay, int minute)Sets the year, month, day of the month, hour of day, and minute fields. Other fields are not changed; callclear()first if this is not desired. The month value is 0-based, so it may be clearer to use a constant likeJANUARY.
-
set
public final void set(int year, int month, int day, int hourOfDay, int minute, int second)Sets the year, month, day of the month, hour of day, minute, and second fields. Other fields are not changed; callclear()first if this is not desired. The month value is 0-based, so it may be clearer to use a constant likeJANUARY.
-
setFirstDayOfWeek
public void setFirstDayOfWeek(int value)
Sets the first day of the week for thisCalendar. The value should be a day of the week such asMONDAY.
-
setLenient
public void setLenient(boolean value)
Sets whether thisCalendaraccepts field values which are outside the valid range for the field.
-
setMinimalDaysInFirstWeek
public void setMinimalDaysInFirstWeek(int value)
Sets the minimal days in the first week of the year.
-
setTime
public final void setTime(Date date)
Sets the time of thisCalendar.
-
setTimeInMillis
public void setTimeInMillis(long milliseconds)
Sets the time of thisCalendarto the given Unix time. SeeDatefor more about what this means.
-
setTimeZone
public void setTimeZone(TimeZone timezone)
Sets theTimeZoneused by this Calendar.
-
toString
public String toString()
Returns a string representation of thisCalendar, showing which fields are set.
-
compareTo
public int compareTo(Calendar anotherCalendar)
Compares the time represented by thisCalendarto that represented by the givenCalendar.- Specified by:
compareToin interfaceComparable<Calendar>- Parameters:
anotherCalendar- the object to compare to this instance.- Returns:
- 0 if the times of the two
Calendars are equal, -1 if the time of thisCalendaris before the other one, 1 if the time of thisCalendaris after the other one. - Throws:
NullPointerException- if the argument is null.IllegalArgumentException- if the argument does not include a valid time value.
-
getDisplayName
public String getDisplayName(int field, int style, Locale locale)
Returns a human-readable string for the value offieldusing the given style and locale. If no string is available, returns null. The value is retrieved by invokingget(field).For example,
getDisplayName(MONTH, SHORT, Locale.US)will return "Jan" whilegetDisplayName(MONTH, LONG, Locale.US)will return "January".- Parameters:
field- the fieldstyle-SHORTorLONGlocale- the locale- Returns:
- the display name, or null
- Throws:
NullPointerException- iflocale == nullIllegalArgumentException- iffieldorstyleis invalid- Since:
- 1.6
-
getDisplayNames
public Map<String,Integer> getDisplayNames(int field, int style, Locale locale)
Returns a map of human-readable strings to corresponding values, for the given field, style, and locale. Returns null if no strings are available.For example,
getDisplayNames(MONTH, ALL_STYLES, Locale.US)would contain mappings from "Jan" and "January" toJANUARY, and so on.- Parameters:
field- the fieldstyle-SHORT,LONG, orALL_STYLESlocale- the locale- Returns:
- the display name, or null
- Throws:
NullPointerException- iflocale == nullIllegalArgumentException- iffieldorstyleis invalid- Since:
- 1.6
-
-