All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class java.util.Calendar

java.lang.Object
   |
   +----java.util.Calendar

public class Calendar
extends Object
implements Serializable, Cloneable
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 java.util.Date for information about the Date class.)

Subclasses of Calendar interpret a Date according to the rules of a specific calendar system. The JDK provides one concrete subclass of Calendar: GregorianCalendar. Future subclasses could represent the various types of lunar calendars in use in many parts of the world.

Like other locale-sensitive classes, Calendar provides a class method, getInstance, for getting a generally useful object of this type. Calendar's getInstance method returns a GregorianCalendar object whose time fields have been initialized with the current date and time:

 Calendar rightNow = Calendar.getInstance();
 

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, two special circumstances may arise: there may be insufficient information to compute the Date (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. The calendar will give preference to the combinations of fields in the following order (and disregard other inconsistent information).

 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-year
 hour-of-day
 ampm + hour-of-ampm
 

Note: for some non-Gregorian calendars, different fields may be necessary for complete disambiguation. For example, a full specification of the historial Arabic astronomical calendar requires year, month, day-of-month and day-of-week in some cases.

Note: There are certain possible ambiguities in interpretation of certain singular times, which are resolved in the following ways:

  1. 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
  2. Although historically not precise, midnight also belongs to "am", and noon belongs to "pm", so on the same day, 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 java.text.DateFormat to format dates.

Calendar provides an API for field "rolling", where fields can be incremented or decremented, but wrap around. For example, rolling the month up in the date "September 12, 1996" results in "October 12, 1996".

Calendar also provides a date arithmetic function for adding the specified (signed) amount of time to a particular time field. For example, subtracting 5 days from the date "September 12, 1996" results in "September 7, 1996".

See Also:
Date, GregorianCalendar, TimeZone, DateFormat

Variable Index

 o AM
Useful constant for hour in 12-hour clock.
 o AM_PM
Useful constant for date and time.
 o APRIL
Useful constant for month.
 o areFieldsSet
The flag which tells if the time fields are set for the calendar.
 o AUGUST
Useful constant for month.
 o DATE
Useful constant for date and time.
 o DAY_OF_MONTH
Useful constant for date and time.
 o DAY_OF_WEEK
Useful constant for date and time.
 o DAY_OF_WEEK_IN_MONTH
Useful constant for date and time.
 o DAY_OF_YEAR
Useful constant for date and time.
 o DECEMBER
Useful constant for month.
 o DST_OFFSET
Useful constant for date and time.
 o ERA
Useful constant for date and time.
 o FEBRUARY
Useful constant for month.
 o FIELD_COUNT
Useful constant for date and time.
 o fields
The time fields containing values into which the millis is computed.
 o FRIDAY
Useful constant for days of week.
 o HOUR
Useful constant for date and time.
 o HOUR_OF_DAY
Useful constant for date and time.
 o isSet
The flags which tell if a specified time field for the calendar is set.
 o isTimeSet
The flag which indicates if the current time is set for the calendar.
 o JANUARY
Useful constant for month.
 o JULY
Useful constant for month.
 o JUNE
Useful constant for month.
 o MARCH
Useful constant for month.
 o MAY
Useful constant for month.
 o MILLISECOND
Useful constant for date and time.
 o MINUTE
Useful constant for date and time.
 o MONDAY
Useful constant for days of week.
 o MONTH
Useful constant for date and time.
 o NOVEMBER
Useful constant for month.
 o OCTOBER
Useful constant for month.
 o PM
Useful constant for hour in 12-hour clock.
 o SATURDAY
Useful constant for days of week.
 o SECOND
Useful constant for date and time.
 o SEPTEMBER
Useful constant for month.
 o SUNDAY
Useful constant for days of week.
 o THURSDAY
Useful constant for days of week.
 o time
The current time set for the calendar.
 o TUESDAY
Useful constant for days of week.
 o UNDECIMBER
Useful constant for month.
 o WEDNESDAY
Useful constant for days of week.
 o WEEK_OF_MONTH
Useful constant for date and time.
 o WEEK_OF_YEAR
Useful constant for date and time.
 o YEAR
Useful constant for date and time.
 o ZONE_OFFSET
Useful constant for date and time.

Constructor Index

 o Calendar()
Constructs a Calendar with the default time zone as returned by TimeZone.getDefault(), and the default locale.
 o Calendar(TimeZone, Locale)
Constructs a Calendar with the given time zone and locale.

Method Index

 o add(int, int)
Date Arithmetic function.
 o after(Object)
Compares the time field records.
 o before(Object)
Compares the time field records.
 o clear()
Clears the values of all the time fields.
 o clear(int)
Clears the value in the given time field.
 o clone()
Overrides Cloneable
 o complete()
Fills in any unset fields in the time field list.
 o computeFields()
Converts UTC as milliseconds to time field values.
 o computeTime()
Converts Calendar's time field values to UTC as milliseconds.
 o equals(Object)
Compares the time field records.
 o get(int)
Gets the value for a given time field.
 o getAvailableLocales()
Gets the set of locales for which Calendars are installed.
 o getFirstDayOfWeek()
Gets what the first day of the week is; e.g., Sunday in US, Monday in France.
 o getGreatestMinimum(int)
Gets the highest minimum value for the given field if varies.
 o getInstance()
Gets a Calendar using the default timezone and locale.
 o getInstance(Locale)
Gets a Calendar using the default timezone and given locale.
 o getInstance(TimeZone)
Gets a Calendar using the given timezone and default locale.
 o getInstance(TimeZone, Locale)
Gets a Calendar using the given timezone and given locale.
 o getLeastMaximum(int)
Gets the lowest maximum value for the given field if varies.
 o getMaximum(int)
Gets the maximum value for the given time field.
 o getMinimalDaysInFirstWeek()
Gets what the minimal days required in the first week of the year are; e.g., if the first week is defined as one that contains the first day of the first month of a year, getMinimalDaysInFirstWeek returns 1.
 o getMinimum(int)
Gets the minimum value for the given time field.
 o getTime()
Gets this Calendar's current time.
 o getTimeInMillis()
Gets this Calendar's current time as a long.
 o getTimeZone()
Gets the time zone.
 o internalGet(int)
Gets the value for a given time field.
 o isLenient()
Tell whether date/time interpretation is to be lenient.
 o isSet(int)
Determines if the given time field has a value set.
 o roll(int, boolean)
Time Field Rolling function.
 o set(int, int)
Sets the time field with the given value.
 o set(int, int, int)
Sets the values for the fields year, month, and date.
 o set(int, int, int, int, int)
Sets the values for the fields year, month, date, hour, and minute.
 o set(int, int, int, int, int, int)
Sets the values for the fields year, month, date, hour, minute, and second.
 o setFirstDayOfWeek(int)
Sets what the first day of the week is; e.g., Sunday in US, Monday in France.
 o setLenient(boolean)
Specify whether or not date/time interpretation is to be lenient.
 o setMinimalDaysInFirstWeek(int)
Sets what the minimal days required in the first week of the year are; For example, if the first week is defined as one that contains the first day of the first month of a year, call the method with value 1.
 o setTime(Date)
Sets this Calendar's current time with the given Date.
 o setTimeInMillis(long)
Sets this Calendar's current time from the given long value.
 o setTimeZone(TimeZone)
Sets the time zone with the given time zone value.

Variables

 o ERA
Properties Public Constant 
   ERA=int 
Useful constant for date and time. Used in time fields. ERA is calendar specific.

 o YEAR
Properties Public Constant 
   YEAR=int 
Useful constant for date and time. Used in time fields.

 o MONTH
Properties Public Constant 
   MONTH=int 
Useful constant for date and time. Used in time fields.

 o WEEK_OF_YEAR
Properties Public Constant 
   WEEK_OF_YEAR=int 
Useful constant for date and time. Used in time fields.

 o WEEK_OF_MONTH
Properties Public Constant 
   WEEK_OF_MONTH=int 
Useful constant for date and time. Used in time fields.

 o DATE
Properties Public Constant 
   DATE=int 
Useful constant for date and time. Used in time fields. DATE is the time field name for day-of-month.

 o DAY_OF_MONTH
Properties Public Constant 
   DAY_OF_MONTH=int 
Useful constant for date and time. Used in time fields. This is a synonym for DATE.

 o DAY_OF_YEAR
Properties Public Constant 
   DAY_OF_YEAR=int 
Useful constant for date and time. Used in time fields.

 o DAY_OF_WEEK
Properties Public Constant 
   DAY_OF_WEEK=int 
Useful constant for date and time. Used in time fields.

 o DAY_OF_WEEK_IN_MONTH
Properties Public Constant 
   DAY_OF_WEEK_IN_MONTH=int 
Useful constant for date and time. Used in time fields.

 o AM_PM
Properties Public Constant 
   AM_PM=int 
Useful constant for date and time. Used in time fields.

 o HOUR
Properties Public Constant 
   HOUR=int 
Useful constant for date and time. Used in time fields. HOUR is used for the 12-hour clock.

 o HOUR_OF_DAY
Properties Public Constant 
   HOUR_OF_DAY=int 
Useful constant for date and time. Used in time fields. HOUR_OF_DAY is used for the 24-hour clock.

 o MINUTE
Properties Public Constant 
   MINUTE=int 
Useful constant for date and time. Used in time fields.

 o SECOND
Properties Public Constant 
   SECOND=int 
Useful constant for date and time. Used in time fields.

 o MILLISECOND
Properties Public Constant 
   MILLISECOND=int 
Useful constant for date and time. Used in time fields.

 o ZONE_OFFSET
Properties Public Constant 
   ZONE_OFFSET=int 
Useful constant for date and time. Used in time fields.

 o DST_OFFSET
Properties Public Constant 
   DST_OFFSET=int 
Useful constant for date and time. Used in time fields.

 o FIELD_COUNT
Properties Public Constant 
   FIELD_COUNT=int 
Useful constant for date and time. FIELD_COUNT is used for the time field array creation.

 o SUNDAY
Properties Public Constant 
   SUNDAY=int 
Useful constant for days of week. Used in GregorianCalendar.

 o MONDAY
Properties Public Constant 
   MONDAY=int 
Useful constant for days of week. Used in GregorianCalendar.

 o TUESDAY
Properties Public Constant 
   TUESDAY=int 
Useful constant for days of week. Used in GregorianCalendar.

 o WEDNESDAY
Properties Public Constant 
   WEDNESDAY=int 
Useful constant for days of week. Used in GregorianCalendar.

 o THURSDAY
Properties Public Constant 
   THURSDAY=int 
Useful constant for days of week. Used in GregorianCalendar.

 o FRIDAY
Properties Public Constant 
   FRIDAY=int 
Useful constant for days of week. Used in GregorianCalendar.

 o SATURDAY
Properties Public Constant 
   SATURDAY=int 
Useful constant for days of week. Used in GregorianCalendar.

 o JANUARY
Properties Public Constant 
   JANUARY=int 
Useful constant for month. Used in GregorianCalendar. Note: Calendar month is now 0-based.

 o FEBRUARY
Properties Public Constant 
   FEBRUARY=int 
Useful constant for month. Used in GregorianCalendar.

 o MARCH
Properties Public Constant 
   MARCH=int 
Useful constant for month. Used in GregorianCalendar.

 o APRIL
Properties Public Constant 
   APRIL=int 
Useful constant for month. Used in GregorianCalendar.

 o MAY
Properties Public Constant 
   MAY=int 
Useful constant for month. Used in GregorianCalendar.

 o JUNE
Properties Public Constant 
   JUNE=int 
Useful constant for month. Used in GregorianCalendar.

 o JULY
Properties Public Constant 
   JULY=int 
Useful constant for month. Used in GregorianCalendar.

 o AUGUST
Properties Public Constant 
   AUGUST=int 
Useful constant for month. Used in GregorianCalendar.

 o SEPTEMBER
Properties Public Constant 
   SEPTEMBER=int 
Useful constant for month. Used in GregorianCalendar.

 o OCTOBER
Properties Public Constant 
   OCTOBER=int 
Useful constant for month. Used in GregorianCalendar.

 o NOVEMBER
Properties Public Constant 
   NOVEMBER=int 
Useful constant for month. Used in GregorianCalendar.

 o DECEMBER
Properties Public Constant 
   DECEMBER=int 
Useful constant for month. Used in GregorianCalendar.

 o UNDECIMBER
Properties Public Constant 
   UNDECIMBER=int 
Useful constant for month. Used in GregorianCalendar. UNDECIMBER is an artifical name. This 13th month is for lunar calendars.

 o AM
Properties Public Constant 
   AM=int 
Useful constant for hour in 12-hour clock. Used in GregorianCalendar.

 o PM
Properties Public Constant 
   PM=int 
Useful constant for hour in 12-hour clock. Used in GregorianCalendar.

 o fields
Properties Inheritable 
   fields[]=int 
The time fields containing values into which the millis is computed.

 o isSet
Properties Inheritable 
   isSet[]=boolean 
The flags which tell if a specified time field for the calendar is set.

 o time
Properties Inheritable 
   time=long 
The current time set for the calendar.

 o isTimeSet
Properties Inheritable 
   isTimeSet=boolean 
The flag which indicates if the current time is set for the calendar.

 o areFieldsSet
Properties Inheritable 
   areFieldsSet=boolean 
The flag which tells if the time fields are set for the calendar.

Constructors

 o Calendar
    method Calendar() Inheritable 
Constructs a Calendar with the default time zone as returned by TimeZone.getDefault(), and the default locale.

See Also:
getDefault
 o Calendar
    method Calendar(zone=TimeZone, -
                    aLocale=Locale) Inheritable 
Constructs a Calendar with the given time zone and locale.

Parameters:
zone - the given time zone.

Methods

 o getInstance
    method getInstance() returns Calendar public  static  protect 
Gets a Calendar using the default timezone and locale.

Returns:
a Calendar.
 o getInstance
    method getInstance(zone=TimeZone) returns Calendar public  static  protect 
Gets a Calendar using the given timezone and default locale.

Parameters:
zone - the given timezone.
Returns:
a Calendar.
 o getInstance
    method getInstance(aLocale=Locale) returns Calendar public  static  protect 
Gets a Calendar using the default timezone and given locale.

Parameters:
aLocale - the given locale.
Returns:
a Calendar.
 o getInstance
    method getInstance(zone=TimeZone, -
                       aLocale=Locale) returns Calendar public  static  protect 
Gets a Calendar using the given timezone and given locale.

Parameters:
zone - the given timezone.
aLocale - the given locale.
Returns:
a Calendar.
 o getAvailableLocales
    method getAvailableLocales() returns Locale[] public  static  protect 
Gets the set of locales for which Calendars are installed.

Returns:
the set of locales for which Calendars are installed.
 o computeTime
    method computeTime()  Inheritable abstract    
Converts Calendar's time field values to UTC as milliseconds.

 o computeFields
    method computeFields()  Inheritable abstract    
Converts UTC as milliseconds to time field values. This allows you to sync up the time field values with a new time that is set for the calendar.

 o getTime
    method getTime() returns Date public   final  
Gets this Calendar's current time.

Returns:
the current time.
 o setTime
    method setTime(date=Date)  public   final  
Sets this Calendar's current time with the given Date.

Parameters:
date - the given Date.
 o getTimeInMillis
    method getTimeInMillis() returns long Inheritable     
Gets this Calendar's current time as a long.

Returns:
the current time as milliseconds from the epoch.
 o setTimeInMillis
    method setTimeInMillis(millis=long)  Inheritable     
Sets this Calendar's current time from the given long value.

Parameters:
date - the new time in milliseconds from the epoch.
 o get
    method get(field=int) returns int public   final  
Gets the value for a given time field.

Parameters:
field - the given time field.
Returns:
the value for the given time field.
 o internalGet
    method internalGet(field=int) returns int Inheritable   final  
Gets the value for a given time field. This is an internal fast time field value getter for the subclasses.

Parameters:
field - the given time field.
Returns:
the value for the given time field.
 o set
    method set(field=int, -
               value=int)  public   final  
Sets the time field with the given value.

Parameters:
field - the given time field.
value - the value to be set for the given time field.
 o set
    method set(year=int, -
               month=int, -
               date=int)  public   final  
Sets the values for the fields year, month, and date.

Parameters:
year - the value used to set the YEAR time field.
month - the value used to set the MONTH time field. Month value is 0-based. e.g., 0 for January.
date - the value used to set the DATE time field.
 o set
    method set(year=int, -
               month=int, -
               date=int, -
               hour=int, -
               minute=int)  public   final  
Sets the values for the fields year, month, date, hour, and minute.

Parameters:
year - the value used to set the YEAR time field.
month - the value used to set the MONTH time field. Month value is 0-based. e.g., 0 for January.
date - the value used to set the DATE time field.
hour - the value used to set the HOUR_OF_DAY time field.
minute - the value used to set the MINUTE time field.
 o set
    method set(year=int, -
               month=int, -
               date=int, -
               hour=int, -
               minute=int, -
               second=int)  public   final  
Sets the values for the fields year, month, date, hour, minute, and second.

Parameters:
year - the value used to set the YEAR time field.
month - the value used to set the MONTH time field. Month value is 0-based. e.g., 0 for January.
date - the value used to set the DATE time field.
hour - the value used to set the HOUR_OF_DAY time field.
minute - the value used to set the MINUTE time field.
second - the value used to set the SECOND time field.
 o clear
    method clear()  public   final  
Clears the values of all the time fields.

 o clear
    method clear(field=int)  public   final  
Clears the value in the given time field.

Parameters:
field - the time field to be cleared.
 o isSet
    method isSet(field=int) returns boolean public   final  
Determines if the given time field has a value set.

Returns:
true if the given time field has a value set; false otherwise.
 o complete
    method complete()  Inheritable     
Fills in any unset fields in the time field list.

 o equals
    method equals(when=Object) returns boolean public abstract    
Compares the time field records. Equivalent to comparing result of conversion to UTC.

Parameters:
when - the Calendar to be compared with this Calendar.
Returns:
true if the current time of this Calendar is equal to the time of Calendar when; false otherwise.
Overrides:
equals in class Object
 o before
    method before(when=Object) returns boolean public abstract    
Compares the time field records. Equivalent to comparing result of conversion to UTC.

Parameters:
when - the Calendar to be compared with this Calendar.
Returns:
true if the current time of this Calendar is before the time of Calendar when; false otherwise.
 o after
    method after(when=Object) returns boolean public abstract    
Compares the time field records. Equivalent to comparing result of conversion to UTC.

Parameters:
when - the Calendar to be compared with this Calendar.
Returns:
true if the current time of this Calendar is after the time of Calendar when; false otherwise.
 o add
    method add(field=int, -
               amount=int)  public abstract    
Date Arithmetic function. Adds the specified (signed) amount of time to the given time field, based on the calendar's rules. For example, to subtract 5 days from the current time of the calendar, you can achieve it by calling:

add(Calendar.DATE, -5).

Parameters:
field - the time field.
amount - the amount of date or time to be added to the field.
 o roll
    method roll(field=int, -
                up=boolean)  public abstract    
Time Field Rolling function. Rolls (up/down) a single unit of time on the given time field. For example, to roll the current date up by one day, you can achieve it by calling:

roll(Calendar.DATE, true). When rolling on the year or Calendar.YEAR field, it will roll the year value in the range between 1 and the value returned by calling getMaximum(Calendar.YEAR). When rolling on the month or Calendar.MONTH field, other fields like date might conflict and, need to be changed. For instance, rolling the month on the date 01/31/96 will result in 03/02/96. When rolling on the hour-in-day or Calendar.HOUR_OF_DAY field, it will roll the hour value in the range between 0 and 23, which is zero-based.

Parameters:
field - the time field.
up - indicates if the value of the specified time field is to be rolled up or rolled down. Use true if rolling up, false otherwise.
 o setTimeZone
    method setTimeZone(value=TimeZone)  public     
Sets the time zone with the given time zone value.

Parameters:
value - the given time zone.
 o getTimeZone
    method getTimeZone() returns TimeZone public     
Gets the time zone.

Returns:
the time zone object associated with this calendar.
 o setLenient
    method setLenient(lenient=boolean)  public     
Specify 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 interpretation, such dates will cause an exception to be thrown.

See Also:
setLenient
 o isLenient
    method isLenient() returns boolean public     
Tell whether date/time interpretation is to be lenient.

 o setFirstDayOfWeek
    method setFirstDayOfWeek(value=int)  public     
Sets what the first day of the week is; e.g., Sunday in US, Monday in France.

Parameters:
value - the given first day of the week.
 o getFirstDayOfWeek
    method getFirstDayOfWeek() returns int public     
Gets what the first day of the week is; e.g., Sunday in US, Monday in France.

Returns:
the first day of the week.
 o setMinimalDaysInFirstWeek
    method setMinimalDaysInFirstWeek(value=int)  public     
Sets what the minimal days required in the first week of the year are; For example, if the first week is defined as one that contains the first day of the first month of a year, call the method with value 1. If it must be a full week, use value 7.

Parameters:
value - the given minimal days required in the first week of the year.
 o getMinimalDaysInFirstWeek
    method getMinimalDaysInFirstWeek() returns int public     
Gets what the minimal days required in the first week of the year are; e.g., if the first week is defined as one that contains the first day of the first month of a year, getMinimalDaysInFirstWeek returns 1. If the minimal days required must be a full week, getMinimalDaysInFirstWeek returns 7.

Returns:
the minimal days required in the first week of the year.
 o getMinimum
    method getMinimum(field=int) returns int public abstract    
Gets the minimum value for the given time field. e.g., for Gregorian DAY_OF_MONTH, 1.

Parameters:
field - the given time field.
Returns:
the minimum value for the given time field.
 o getMaximum
    method getMaximum(field=int) returns int public abstract    
Gets the maximum value for the given time field. e.g. for Gregorian DAY_OF_MONTH, 31.

Parameters:
field - the given time field.
Returns:
the maximum value for the given time field.
 o getGreatestMinimum
    method getGreatestMinimum(field=int) returns int public abstract    
Gets the highest minimum value for the given field if varies. Otherwise same as getMinimum(). For Gregorian, no difference.

Parameters:
field - the given time field.
Returns:
the highest minimum value for the given time field.
 o getLeastMaximum
    method getLeastMaximum(field=int) returns int public abstract    
Gets the lowest maximum value for the given field if varies. Otherwise same as getMaximum(). e.g., for Gregorian DAY_OF_MONTH, 28.

Parameters:
field - the given time field.
Returns:
the lowest maximum value for the given time field.
 o clone
    method clone() returns Object public     
Overrides Cloneable

Overrides:
clone in class Object

All Packages  Class Hierarchy  This Package  Previous  Next  Index