Class Calendar
java.lang.Object
java.util.Calendar
Calendar is an abstract base class for converting between a Date object and a set of integer fields such as YEAR, MONTH, DAY, HOUR, and so on. (A Date object represents a specific instant in time with millisecond precision. See Date for information about the Date class.)
Subclasses of Calendar interpret a Date according to the rules of a specific calendar system.
Like other locale-sensitive classes, Calendar provides a class method, getInstance, for getting a generally useful object of this type.
A Calendar object can produce all the time field values needed to implement the date-time formatting for a particular language and calendar style (for example, Japanese-Gregorian, Japanese-Traditional).
When computing a Date from time fields, there may be insufficient information to compute the Date (such as only year and month but no day in the month).
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. Note: The ambiguity in interpretation of what day midnight belongs to, is resolved as so: midnight "belongs" to the following day. 23:59 on Dec 31, 1969 00:00 on Jan 1, 1970. 12:00 PM is midday, and 12:00 AM is midnight. 11:59 PM on Jan 1 12:00 AM on Jan 2 12:01 AM on Jan 2. 11:59 AM on Mar 10 12:00 PM on Mar 10 12:01 PM on Mar 10. 24:00 or greater are invalid. Hours greater than 12 are invalid in AM/PM mode. Setting the time will never change the date.
If equivalent times are entered in AM/PM or 24 hour mode, equality will be determined by the actual time rather than the entered time.
This class has been subset for J2ME based on the JDK 1.3 Calendar class. Many methods and variables have been pruned, and other methods simplified, in an effort to reduce the size of this class.
Version: CLDC 1.1 02/01/2002 (based on JDK 1.3) See Also:Date, TimeZone
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intValue of the AM_PM field indicating the period of the day from midnight to just before noon.static final intField number for get and set indicating whether the HOUR is before or after noon.static final intValue of the MONTH field indicating the fourth month of the year.static final intValue of the MONTH field indicating the eighth month of the year.static final intField number for get and set indicating the day of the month.static final intField number for get and set indicating the day of the month.static final intField number for get and set indicating the day of the week.static final intField number forgetandsetindicating the ordinal number of the day of the week within the current month.static final intValue of the MONTH field indicating the twelfth month of the year.static final intValue of the MONTH field indicating the second month of the year.protected int[]The field values for the currently set time for this calendar.static final intValue of the DAY_OF_WEEK field indicating Friday.static final intField number for get and set indicating the hour of the morning or afternoon.static final intField number for get and set indicating the hour of the day.protected boolean[]The flags which tell if a specified time field for the calendar is set.static final intValue of the MONTH field indicating the first month of the year.static final intValue of the MONTH field indicating the seventh month of the year.static final intValue of the MONTH field indicating the sixth month of the year.static final intValue of the MONTH field indicating the third month of the year.static final intValue of the MONTH field indicating the fifth month of the year.static final intField number for get and set indicating the millisecond within the second.static final intField number for get and set indicating the minute within the hour.static final intValue of the DAY_OF_WEEK field indicating Monday.static final intField number for get and set indicating the month.static final intValue of the MONTH field indicating the eleventh month of the year.static final intValue of the MONTH field indicating the tenth month of the year.static final intValue of the AM_PM field indicating the period of the day from noon to just before midnight.static final intValue of the DAY_OF_WEEK field indicating Saturday.static final intField number for get and set indicating the second within the minute.static final intValue of the MONTH field indicating the ninth month of the year.static final intValue of the DAY_OF_WEEK field indicating Sunday.static final intValue of the DAY_OF_WEEK field indicating Thursday.protected longThe currently set time for this calendar, expressed in milliseconds after January 1, 1970, 0:00:00 GMT.static final intValue of the DAY_OF_WEEK field indicating Tuesday.static final intValue of the DAY_OF_WEEK field indicating Wednesday.static final intField number forgetandsetindicating the week number within the current month.static final intField number forgetandsetindicating the week number within the current year.static final intField number for get and set indicating the year. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedCalendar()Constructs a Calendar with the default time zone. -
Method Summary
Modifier and TypeMethodDescriptionfinal voidadd(int field, int value) Adds the specified amount to aCalendarfield.booleanCompares the time field records.booleanCompares the time field records.protected abstract voidConverts the current millisecond time value time to field values in fields[].protected abstract voidConverts the current field values in fields[] to the millisecond time value time.booleanCompares this calendar to the specified object.final intget(int field) Gets the value for a given time field.static CalendarGets a calendar using the default time zone.static CalendargetInstance(TimeZone zone) Gets a calendar using the specified time zone.final DategetTime()Gets this Calendar's current time.protected longGets this Calendar's current time as a long expressed in milliseconds after January 1, 1970, 0:00:00 GMT (the epoch).Gets the time zone.final voidset(int field, int value) Sets the time field with the given value.final voidSets this Calendar's current time with the given Date.protected voidsetTimeInMillis(long millis) Sets this Calendar's current time from the given long value.voidsetTimeZone(TimeZone value) Sets the time zone with the given time zone value.
-
Field Details
-
AM
public static final int AMValue of the AM_PM field indicating the period of the day from midnight to just before noon. See Also:Constant Field Values- See Also:
-
AM_PM
public static final int AM_PMField number for get and set indicating whether the HOUR is before or after noon. E.g., at 10:04:15.250 PM the AM_PM is PM. See Also:AM, PM, HOUR, Constant Field Values- See Also:
-
APRIL
public static final int APRILValue of the MONTH field indicating the fourth month of the year. See Also:Constant Field Values- See Also:
-
AUGUST
public static final int AUGUSTValue of the MONTH field indicating the eighth month of the year. See Also:Constant Field Values- See Also:
-
WEEK_OF_YEAR
public static final int WEEK_OF_YEARField 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.- See Also:
-
WEEK_OF_MONTH
public static final int WEEK_OF_MONTHField 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.- See Also:
-
DATE
public static final int DATEField number for get and set indicating the day of the month. This is a synonym for DAY_OF_MONTH. See Also:DAY_OF_MONTH, Constant Field Values- See Also:
-
DAY_OF_MONTH
public static final int DAY_OF_MONTHField number for get and set indicating the day of the month. This is a synonym for DATE. See Also:DATE, Constant Field Values- See Also:
-
DAY_OF_WEEK
public static final int DAY_OF_WEEKField number for get and set indicating the day of the week. See Also:Constant Field Values- See Also:
-
DAY_OF_WEEK_IN_MONTH
public static final int DAY_OF_WEEK_IN_MONTHField 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:
-
DECEMBER
public static final int DECEMBERValue of the MONTH field indicating the twelfth month of the year. See Also:Constant Field Values- See Also:
-
FEBRUARY
public static final int FEBRUARYValue of the MONTH field indicating the second month of the year. See Also:Constant Field Values- See Also:
-
fields
protected int[] fieldsThe field values for the currently set time for this calendar. -
FRIDAY
public static final int FRIDAYValue of the DAY_OF_WEEK field indicating Friday. See Also:Constant Field Values- See Also:
-
HOUR
public static final int HOURField number for get and set indicating the hour of the morning or afternoon. HOUR is used for the 12-hour clock. E.g., at 10:04:15.250 PM the HOUR is 10. See Also:AM_PM, HOUR_OF_DAY, Constant Field Values- See Also:
-
HOUR_OF_DAY
public static final int HOUR_OF_DAYField number for get and set indicating the hour of the day. HOUR_OF_DAY is used for the 24-hour clock. E.g., at 10:04:15.250 PM the HOUR_OF_DAY is 22. See Also:Constant Field Values- See Also:
-
isSet
protected boolean[] isSetThe flags which tell if a specified time field for the calendar is set. This is an array of FIELD_COUNT booleans, -
JANUARY
public static final int JANUARYValue of the MONTH field indicating the first month of the year. See Also:Constant Field Values- See Also:
-
JULY
public static final int JULYValue of the MONTH field indicating the seventh month of the year. See Also:Constant Field Values- See Also:
-
JUNE
public static final int JUNEValue of the MONTH field indicating the sixth month of the year. See Also:Constant Field Values- See Also:
-
MARCH
public static final int MARCHValue of the MONTH field indicating the third month of the year. See Also:Constant Field Values- See Also:
-
MAY
public static final int MAYValue of the MONTH field indicating the fifth month of the year. See Also:Constant Field Values- See Also:
-
MILLISECOND
public static final int MILLISECONDField number for get and set indicating the millisecond within the second. E.g., at 10:04:15.250 PM the MILLISECOND is 250. See Also:Constant Field Values- See Also:
-
MINUTE
public static final int MINUTEField number for get and set indicating the minute within the hour. E.g., at 10:04:15.250 PM the MINUTE is 4. See Also:Constant Field Values- See Also:
-
MONDAY
public static final int MONDAYValue of the DAY_OF_WEEK field indicating Monday. See Also:Constant Field Values- See Also:
-
MONTH
public static final int MONTHField number for get and set indicating the month. This is a calendar-specific value. See Also:Constant Field Values- See Also:
-
NOVEMBER
public static final int NOVEMBERValue of the MONTH field indicating the eleventh month of the year. See Also:Constant Field Values- See Also:
-
OCTOBER
public static final int OCTOBERValue of the MONTH field indicating the tenth month of the year. See Also:Constant Field Values- See Also:
-
PM
public static final int PMValue of the AM_PM field indicating the period of the day from noon to just before midnight. See Also:Constant Field Values- See Also:
-
SATURDAY
public static final int SATURDAYValue of the DAY_OF_WEEK field indicating Saturday. See Also:Constant Field Values- See Also:
-
SECOND
public static final int SECONDField number for get and set indicating the second within the minute. E.g., at 10:04:15.250 PM the SECOND is 15. See Also:Constant Field Values- See Also:
-
SEPTEMBER
public static final int SEPTEMBERValue of the MONTH field indicating the ninth month of the year. See Also:Constant Field Values- See Also:
-
SUNDAY
public static final int SUNDAYValue of the DAY_OF_WEEK field indicating Sunday. See Also:Constant Field Values- See Also:
-
THURSDAY
public static final int THURSDAYValue of the DAY_OF_WEEK field indicating Thursday. See Also:Constant Field Values- See Also:
-
time
protected long timeThe currently set time for this calendar, expressed in milliseconds after January 1, 1970, 0:00:00 GMT. -
TUESDAY
public static final int TUESDAYValue of the DAY_OF_WEEK field indicating Tuesday. See Also:Constant Field Values- See Also:
-
WEDNESDAY
public static final int WEDNESDAYValue of the DAY_OF_WEEK field indicating Wednesday. See Also:Constant Field Values- See Also:
-
YEAR
public static final int YEARField number for get and set indicating the year. This is a calendar-specific value. See Also:Constant Field Values- See Also:
-
-
Constructor Details
-
Calendar
protected Calendar()Constructs a Calendar with the default time zone. See Also:TimeZone.getDefault()
-
-
Method Details
-
after
Compares the time field records. Equivalent to comparing result of conversion to UTC. -
before
Compares the time field records. Equivalent to comparing result of conversion to UTC. -
computeFields
protected abstract void computeFields()Converts the current millisecond time value time to field values in fields[]. This allows you to sync up the time field values with a new time that is set for the calendar. -
computeTime
protected abstract void computeTime()Converts the current field values in fields[] to the millisecond time value time. -
equals
-
get
public final int get(int field) Gets the value for a given time field. -
getInstance
Gets a calendar using the default time zone. -
getInstance
-
getTime
Gets this Calendar's current time. -
getTimeInMillis
protected long getTimeInMillis()Gets this Calendar's current time as a long expressed in milliseconds after January 1, 1970, 0:00:00 GMT (the epoch). -
getTimeZone
Gets the time zone. -
set
public final void set(int field, int value) Sets the time field with the given value. -
add
public final void add(int field, int value) Adds the specified amount to aCalendarfield.- Parameters:
field- theCalendarfield to modify.value- the amount to add to the field.- Throws:
IllegalArgumentException- iffieldisDST_OFFSETorZONE_OFFSET.
-
setTime
Sets this Calendar's current time with the given Date. Note: Calling setTime() with Date(Long.MAX_VALUE) or Date(Long.MIN_VALUE) may yield incorrect field values from get(). -
setTimeInMillis
protected void setTimeInMillis(long millis) Sets this Calendar's current time from the given long value. -
setTimeZone
Sets the time zone with the given time zone value.
-