加载中...
The Wayback Machine - https://sup1a9wrlpyh5li9ro.vcoronado.top/web/20090225064638/http://developer.android.com:80/reference/java/util/Calendar.html
public abstract class

Calendar

extends Object
implements Serializable Cloneable Comparable<T>
java.lang.Object
   ↳ java.util.Calendar
Known Direct Subclasses

Class Overview

Calendar is an abstract class which provides the conversion between Dates and integer calendar fields, such as the month, year or minute. Subclasses of this class implement a specific calendar type, such as the gregorian calendar.

Summary

Constants
int AM
int AM_PM
int APRIL
int AUGUST
int DATE
int DAY_OF_MONTH
int DAY_OF_WEEK
int DAY_OF_WEEK_IN_MONTH
int DAY_OF_YEAR
int DECEMBER
int DST_OFFSET
int ERA
int FEBRUARY
int FIELD_COUNT
int FRIDAY
int HOUR
int HOUR_OF_DAY
int JANUARY
int JULY
int JUNE
int MARCH
int MAY
int MILLISECOND
int MINUTE
int MONDAY
int MONTH
int NOVEMBER
int OCTOBER
int PM
int SATURDAY
int SECOND
int SEPTEMBER
int SUNDAY
int THURSDAY
int TUESDAY
int UNDECIMBER
int WEDNESDAY
int WEEK_OF_MONTH
int WEEK_OF_YEAR
int YEAR
int ZONE_OFFSET
Fields
protected boolean areFieldsSet Set to true when the calendar fields have been set from the time, set to false when a field is changed and the fields must be recomputed.
protected int[] fields An integer array of calendar fields.
protected boolean[] isSet
protected boolean isTimeSet Set to true when the time has been set, set to false when a field is changed and the time must be recomputed.
protected long time The time in milliseconds since January 1, 1970.
Protected Constructors
Calendar()
Initializes this Calendar instance using the default TimeZone and Locale.
Calendar(TimeZone timezone, Locale locale)
Initializes this Calendar instance using the specified TimeZone and Locale.
Public Methods
abstract void add(int field, int value)
Adds the specified amount to a Calendar field.
boolean after(Object calendar)
Returns if the Date specified by this Calendar instance is after the Date specified by the parameter.
boolean before(Object calendar)
Returns if the Date specified by this Calendar instance is before the Date specified by the parameter.
final void clear()
Clears all of the fields of this Calendar.
final void clear(int field)
Clears the specified field to zero.
Object clone()
Returns a new Calendar with the same properties.
int compareTo(Calendar anotherCalendar)
Compares the times of the two Calendars, which represent the milliseconds from the January 1, 1970 00:00:00.000 GMT (Gregorian).
boolean equals(Object object)
Compares the specified object to this Calendar and answer if they are equal.
int get(int field)
Gets the value of the specified field after computing the field values from the time if required.
int getActualMaximum(int field)
Gets the maximum value of the specified field for the current date.
int getActualMinimum(int field)
Gets the minimum value of the specified field for the current date.
synchronized static Locale[] getAvailableLocales()
Gets the list of installed Locales which support Calendar.
int getFirstDayOfWeek()
Gets the first day of the week for this Calendar.
abstract int getGreatestMinimum(int field)
Gets the greatest minimum value of the specified field.
synchronized static Calendar getInstance(TimeZone timezone, Locale locale)
Constructs a new instance of the Calendar subclass appropriate for the specified Locale.
synchronized static Calendar getInstance()
Constructs a new instance of the Calendar subclass appropriate for the default Locale.
synchronized static Calendar getInstance(TimeZone timezone)
Constructs a new instance of the Calendar subclass appropriate for the default Locale, using the specified TimeZone.
synchronized static Calendar getInstance(Locale locale)
Constructs a new instance of the Calendar subclass appropriate for the specified Locale.
abstract int getLeastMaximum(int field)
Gets the smallest maximum value of the specified field.
abstract int getMaximum(int field)
Gets the greatest maximum value of the specified field.
int getMinimalDaysInFirstWeek()
Gets the minimal days in the first week of the year.
abstract int getMinimum(int field)
Gets the smallest minimum value of the specified field.
final Date getTime()
Gets the time of this Calendar as a Date object.
long getTimeInMillis()
Computes the time from the fields if required and returns the time.
TimeZone getTimeZone()
Gets the timezone of this Calendar.
int hashCode()
Returns an integer hash code for the receiver.
boolean isLenient()
Returns if this Calendar accepts field values which are outside the valid range for the field.
final boolean isSet(int field)
Returns if the specified field is set.
void roll(int field, int value)
Adds the specified amount the specified field and wrap the value of the field when it goes beyond the maximum or minimum value for the current date.
abstract void roll(int field, boolean increment)
Increment or decrement the specified field and wrap the value of the field when it goes beyond the maximum or minimum value for the current date.
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.
void set(int field, int value)
Sets a field to the specified value.
final void set(int year, int month, int day)
Sets the year, month and day of the month fields.
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.
void setFirstDayOfWeek(int value)
Sets the first day of the week for this Calendar.
void setLenient(boolean value)
Sets this Calendar to accept field values which are outside the valid range for the field.
void setMinimalDaysInFirstWeek(int value)
Sets the minimal days in the first week of the year.
final void setTime(Date date)
Sets the time of this Calendar.
void setTimeInMillis(long milliseconds)
Sets the time of this Calendar.
void setTimeZone(TimeZone timezone)
Sets the timezone used by this Calendar.
String toString()
Returns the string representation of this Calendar.
Protected Methods
void complete()
Computes the time from the fields if the time has not already been set.
abstract void computeFields()
Computes the Calendar fields from the time.
abstract void computeTime()
Computes the time from the Calendar fields.
final int internalGet(int field)
Gets the value of the specified field without recomputing.
[Expand]
Inherited Methods
From class java.lang.Object
From interface java.lang.Comparable

Constants

public static final int AM

Constant Value: 0 (0x00000000)

public static final int AM_PM

Constant Value: 9 (0x00000009)

public static final int APRIL

Constant Value: 3 (0x00000003)

public static final int AUGUST

Constant Value: 7 (0x00000007)

public static final int DATE

Constant Value: 5 (0x00000005)

public static final int DAY_OF_MONTH

Constant Value: 5 (0x00000005)

public static final int DAY_OF_WEEK

Constant Value: 7 (0x00000007)

public static final int DAY_OF_WEEK_IN_MONTH

Constant Value: 8 (0x00000008)

public static final int DAY_OF_YEAR

Constant Value: 6 (0x00000006)

public static final int DECEMBER

Constant Value: 11 (0x0000000b)

public static final int DST_OFFSET

Constant Value: 16 (0x00000010)

public static final int ERA

Constant Value: 0 (0x00000000)

public static final int FEBRUARY

Constant Value: 1 (0x00000001)

public static final int FIELD_COUNT

Constant Value: 17 (0x00000011)

public static final int FRIDAY

Constant Value: 6 (0x00000006)

public static final int HOUR

Constant Value: 10 (0x0000000a)

public static final int HOUR_OF_DAY

Constant Value: 11 (0x0000000b)

public static final int JANUARY

Constant Value: 0 (0x00000000)

public static final int JULY

Constant Value: 6 (0x00000006)

public static final int JUNE

Constant Value: 5 (0x00000005)

public static final int MARCH

Constant Value: 2 (0x00000002)

public static final int MAY

Constant Value: 4 (0x00000004)

public static final int MILLISECOND

Constant Value: 14 (0x0000000e)

public static final int MINUTE

Constant Value: 12 (0x0000000c)

public static final int MONDAY

Constant Value: 2 (0x00000002)

public static final int MONTH

Constant Value: 2 (0x00000002)

public static final int NOVEMBER

Constant Value: 10 (0x0000000a)

public static final int OCTOBER

Constant Value: 9 (0x00000009)

public static final int PM

Constant Value: 1 (0x00000001)

public static final int SATURDAY

Constant Value: 7 (0x00000007)

public static final int SECOND

Constant Value: 13 (0x0000000d)

public static final int SEPTEMBER

Constant Value: 8 (0x00000008)

public static final int SUNDAY

Constant Value: 1 (0x00000001)

public static final int THURSDAY

Constant Value: 5 (0x00000005)

public static final int TUESDAY

Constant Value: 3 (0x00000003)

public static final int UNDECIMBER

Constant Value: 12 (0x0000000c)

public static final int WEDNESDAY

Constant Value: 4 (0x00000004)

public static final int WEEK_OF_MONTH

Constant Value: 4 (0x00000004)

public static final int WEEK_OF_YEAR

Constant Value: 3 (0x00000003)

public static final int YEAR

Constant Value: 1 (0x00000001)

public static final int ZONE_OFFSET

Constant Value: 15 (0x0000000f)

Fields

protected boolean areFieldsSet

Set to true when the calendar fields have been set from the time, set to false when a field is changed and the fields must be recomputed.

protected int[] fields

An integer array of calendar fields.

protected boolean[] isSet

protected boolean isTimeSet

Set to true when the time has been set, set to false when a field is changed and the time must be recomputed.

protected long time

The time in milliseconds since January 1, 1970.

Protected Constructors

protected Calendar ()

Initializes this Calendar instance using the default TimeZone and Locale.

protected Calendar (TimeZone timezone, Locale locale)

Initializes this Calendar instance using the specified TimeZone and Locale.

Parameters
timezone the timezone
locale the locale

Public Methods

public abstract void add (int field, int value)

Adds the specified amount to a Calendar field.

Parameters
field the Calendar field to modify
value the amount to add to the field
Throws
IllegalArgumentException when the specified field is DST_OFFSET or ZONE_OFFSET.

public boolean after (Object calendar)

Returns if the Date specified by this Calendar instance is after the Date specified by the parameter. The comparison is not dependent on the time zones of the Calendars.

Parameters
calendar the Calendar instance to compare
Returns
  • true when this Calendar is after calendar, false otherwise
Throws
IllegalArgumentException when the time is not set and the time cannot be computed from the current field values

public boolean before (Object calendar)

Returns if the Date specified by this Calendar instance is before the Date specified by the parameter. The comparison is not dependent on the time zones of the Calendars.

Parameters
calendar the Calendar instance to compare
Returns
  • true when this Calendar is before calendar, false otherwise
Throws
IllegalArgumentException when the time is not set and the time cannot be computed from the current field values

public final void clear ()

Clears all of the fields of this Calendar. All fields are initialized to zero.

public final void clear (int field)

Clears the specified field to zero.

Parameters
field the field to clear

public Object clone ()

Returns a new Calendar with the same properties.

Returns
  • a shallow copy of this Calendar
See Also

public int compareTo (Calendar anotherCalendar)

Compares the times of the two Calendars, which represent the milliseconds from the January 1, 1970 00:00:00.000 GMT (Gregorian).

Parameters
anotherCalendar another calendar that is compared with.
Returns
  • 0 if the times of the two calendar are equal, -1 if the time of this calendar is before the other one, 1 if the time of this calendar is after the other one.
Throws
NullPointerException if the argument of calendar is null.
IllegalArgumentException if the argument of the calendar does not include a valid time value.

public boolean equals (Object object)

Compares the specified object to this Calendar and answer if they are equal. The object must be an instance of Calendar and have the same properties.

Parameters
object the object to compare with this object
Returns
  • true if the specified object is equal to this Calendar, false otherwise

public int get (int field)

Gets the value of the specified field after computing the field values from the time if required.

Parameters
field the field
Returns
  • the value of the specified field
Throws
IllegalArgumentException when the fields are not set, the time is not set, and the time cannot be computed from the current field values

public int getActualMaximum (int field)

Gets the maximum value of the specified field for the current date.

Parameters
field the field
Returns
  • the maximum value of the specified field

public int getActualMinimum (int field)

Gets the minimum value of the specified field for the current date.

Parameters
field the field
Returns
  • the minimum value of the specified field

public static synchronized Locale[] getAvailableLocales ()

Gets the list of installed Locales which support Calendar.

Returns
  • an array of Locale

public int getFirstDayOfWeek ()

Gets the first day of the week for this Calendar.

Returns
  • a Calendar day of the week

public abstract int getGreatestMinimum (int field)

Gets the greatest minimum value of the specified field.

Parameters
field the field
Returns
  • the greatest minimum value of the specified field

public static synchronized Calendar getInstance (TimeZone timezone, Locale locale)

Constructs a new instance of the Calendar subclass appropriate for the specified Locale.

Parameters
timezone the timezone to use
locale the locale to use
Returns
  • a Calendar subclass instance set to the current date and time in the specified timezone

public static synchronized Calendar getInstance ()

Constructs a new instance of the Calendar subclass appropriate for the default Locale.

Returns
  • a Calendar subclass instance set to the current date and time in the default timezone

public static synchronized Calendar getInstance (TimeZone timezone)

Constructs a new instance of the Calendar subclass appropriate for the default Locale, using the specified TimeZone.

Parameters
timezone the timezone to use
Returns
  • a Calendar subclass instance set to the current date and time in the specified timezone

public static synchronized Calendar getInstance (Locale locale)

Constructs a new instance of the Calendar subclass appropriate for the specified Locale.

Parameters
locale the locale to use
Returns
  • a Calendar subclass instance set to the current date and time

public abstract int getLeastMaximum (int field)

Gets the smallest maximum value of the specified field.

Parameters
field the field
Returns
  • the smallest maximum value of the specified field

public abstract int getMaximum (int field)

Gets the greatest maximum value of the specified field.

Parameters
field the field
Returns
  • the greatest maximum value of the specified field

public int getMinimalDaysInFirstWeek ()

Gets the minimal days in the first week of the year.

Returns
  • the minimal days in the first week of the year

public abstract int getMinimum (int field)

Gets the smallest minimum value of the specified field.

Parameters
field the field
Returns
  • the smallest minimum value of the specified field

public final Date getTime ()

Gets the time of this Calendar as a Date object.

Returns
  • a new Date initialized to the time of this Calendar
Throws
IllegalArgumentException when the time is not set and the time cannot be computed from the current field values

public long getTimeInMillis ()

Computes the time from the fields if required and returns the time.

Returns
  • the time of this Calendar
Throws
IllegalArgumentException when the time is not set and the time cannot be computed from the current field values

public TimeZone getTimeZone ()

Gets the timezone of this Calendar.

Returns
  • the timezone used by this Calendar

public int hashCode ()

Returns an integer hash code for the receiver. Objects which are equal answer the same value for this method.

Returns
  • the receiver's hash
See Also

public boolean isLenient ()

Returns if this Calendar accepts field values which are outside the valid range for the field.

Returns
  • true if this Calendar is lenient, false otherwise

public final boolean isSet (int field)

Returns if the specified field is set.

Parameters
field a calendar field
Returns
  • true if the specified field is set, false otherwise

public void roll (int field, int value)

Adds the specified amount the specified 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.

Parameters
field the field to roll
value the amount to add

public abstract void roll (int field, boolean increment)

Increment or decrement the specified 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.

Parameters
field the field to roll
increment true to increment the field, false to decrement

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.

Parameters
year the year
month the month
day the day of the month
hourOfDay the hour of day
minute the minute

public void set (int field, int value)

Sets a field to the specified value.

Parameters
field the Calendar field to modify
value the value

public final void set (int year, int month, int day)

Sets the year, month and day of the month fields.

Parameters
year the year
month the month
day the day of the month

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.

Parameters
year the year
month the month
day the day of the month
hourOfDay the hour of day
minute the minute
second the second

public void setFirstDayOfWeek (int value)

Sets the first day of the week for this Calendar.

Parameters
value a Calendar day of the week

public void setLenient (boolean value)

Sets this Calendar to accept field values which are outside the valid range for the field.

Parameters
value a boolean value

public void setMinimalDaysInFirstWeek (int value)

Sets the minimal days in the first week of the year.

Parameters
value the minimal days in the first week of the year

public final void setTime (Date date)

Sets the time of this Calendar.

Parameters
date a Date object

public void setTimeInMillis (long milliseconds)

Sets the time of this Calendar.

Parameters
milliseconds the time as the number of milliseconds since Jan. 1, 1970

public void setTimeZone (TimeZone timezone)

Sets the timezone used by this Calendar.

Parameters
timezone a TimeZone

public String toString ()

Returns the string representation of this Calendar.

Returns
  • the string representation of this Calendar

Protected Methods

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 when the time is not set and the time cannot be computed from the current field values

protected abstract void computeFields ()

Computes the Calendar fields from the time.

protected abstract void computeTime ()

Computes the time from the Calendar fields.

Throws
IllegalArgumentException when the time cannot be computed from the current field values

protected final int internalGet (int field)

Gets the value of the specified field without recomputing.

Parameters
field the field
Returns
  • the value of the specified field