Calendar class

The calendar field values can be set by calling the set methods.

Constants for AM/PM

static int AM
Indicating the period of the day from midnight to just before noon.
static int AM_PM
Field number for get and set indicating whether the HOUR is before or after noon.
static int PM
Indicating the period of the day from noon to just before midnight.

Constants for Date

static int DAY_OF_MONTH
For get and set indicating the day of the month.
static int DAY_OF_WEEK
For get and set indicating the day of the week.
static int DAY_OF_WEEK_IN_MONTH
For get and set indicating the ordinal number of the day of the week within the current month.
static int DAY_OF_YEAR
For get and set indicating the day number within the current year.
static int HOUR
For get and set indicating the hour of the morning or afternoon.
static int HOUR_OF_DAY
For get and set indicating the hour of the day.
static int DATE
For get and set indicating the day of the month.
static int ERA
For get and set indicating the era, e.g., AD or BC in the Julian calendar.
static int MILLISECOND
For get and set indicating the millisecond within the second.
static int MINUTE
For get and set indicating the minute within the hour.
static int MONTH
For get and set indicating the month.
static int SECOND
For get and set indicating the second within the minute.
static int WEEK_OF_MONTH
For get and set indicating the week number within the current month.
static int WEEK_OF_YEAR
For get and set indicating the week number within the current year.
static int YEAR
For get and set indicating the year.

Constants for Month

static int JANUARY
first month
static int FEBRUARY
second month
static int MARCH
third month
static int APRIL
fourth month
static int MAY
fifth month
static int JUNE
sixth month
static int JULY
seventh month
static int AUGUST
eighth month
static int SEPTEMBER
ninth month
static int OCTOBER
tenth month
static int NOVEMBER
eleventh month
static int DECEMBER
twelfth month
static int UNDECIMBER
Value of the MONTH field indicating the thirteenth month of the year.

Constants for WeekDay

static int MONDAY
Monday.
static int TUESDAY
Tuesday.
static int WEDNESDAY
Wednesday.
static int THURSDAY
Thursday.
static int FRIDAY
Friday.
static int SATURDAY
Saturday.
static int SUNDAY
Sunday.

Constants for display name format

static int LONG
For getDisplayName and getDisplayNames indicating a long name, such as "January".
static int SHORT
For getDisplayName and getDisplayNames indicating a short name, such as "Jan".
static int ALL_STYLES
For getDisplayNames indicating names in all styles, such as "January" and "Jan".

Constants for Timezone

static int DST_OFFSET
Field number for get and set indicating the daylight savings offset in milliseconds.
static int FIELD_COUNT
The number of distinct fields recognized by get and set.
static int ZONE_OFFSET
Field number for get and set indicating the raw offset from GMT in milliseconds.

Create new Calendar instance

static Calendar getInstance()
Gets a calendar using the default time zone and locale.
static Calendar getInstance(Locale aLocale)
Gets a calendar using the default time zone and specified locale.
static Calendar getInstance(TimeZone zone)
Gets a calendar using the specified time zone and default locale.
static Calendar getInstance(TimeZone zone, Locale aLocale)
Gets a calendar with the specified time zone and locale.

Get field value from Calendar

int get(int field)
Returns the value of the given calendar field.
int getActualMaximum(int field)
Returns the maximum value that the specified calendar field could have, given the time value of this Calendar.
int getActualMinimum(int field)
Returns the minimum value that the specified calendar field could have, given the time value of this Calendar.
int getFirstDayOfWeek()
Gets what the first day of the week is; e.g., SUNDAY in the U.S., MONDAY in France.
int 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, this method returns 1.
Date getTime()
Returns a Date object representing this Calendar's time value (millisecond offset from the Epoch").
long getTimeInMillis()
Returns this Calendar's time value in milliseconds.
TimeZone getTimeZone()
Gets the time zone.

Clear a field or all fields

void clear()
Sets all the calendar field values and the time value (millisecond offset from the Epoch) of this Calendar undefined.
void clear(int field)
Sets the given calendar field value and the time value (millisecond offset from the Epoch) of this Calendar undefined.

Compare to Calendar

boolean after(Object when)
Returns whether this Calendar represents a time after the time represented by the specified Object.
boolean before(Object when)
Returns whether this Calendar represents a time before the time represented by the specified Object.
int compareTo(Calendar anotherCalendar)
Compares the time values (millisecond offsets from the Epoch) represented by two Calendar objects.
boolean equals(Object obj)
Compares this Calendar to the specified Object.

Calendar and display name

static Locale[] getAvailableLocales()
Returns an array of all locales for which the getInstance methods of this class can return localized instances.
String getDisplayName(int field, int style, Locale locale)
Returns the string representation of the calendar field value in the given style and locale.
Map<String,Integer> getDisplayNames(int field, int style, Locale locale)
Returns a Map containing all names of the calendar field in the given style and locale and their corresponding field values.

Is lenient

boolean isLenient()
Tells whether date/time interpretation is to be lenient.

Add/set value to a field and get new calendar value

void add(int field, int amount)
Adds or subtracts the specified amount of time to the given calendar field, based on the calendar's rules.
void roll(int field, int amount)
Adds the specified (signed) amount to the specified calendar field without changing larger fields.
void set(int field, int value)
Sets the given calendar field to the given value.
void set(int year, int month, int date)
Sets the values for the calendar fields YEAR, MONTH, and DAY_OF_MONTH.
void set(int year, int month, int date, int hourOfDay, int minute)
Sets the values for the calendar fields YEAR, MONTH, DAY_OF_MONTH, HOUR_OF_DAY, and MINUTE.
void set(int year, int month, int date, int hourOfDay, int minute, int second)
Sets the values for the fields YEAR, MONTH, DAY_OF_MONTH, HOUR, MINUTE, and SECOND.
void setFirstDayOfWeek(int value)
Sets what the first day of the week is; e.g., SUNDAY in the U.S., MONDAY in France.
void setLenient(boolean lenient)
Specifies whether or not date/time interpretation is to be lenient.
void setMinimalDaysInFirstWeek(int value)
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 this method with value 1.
void setTime(Date date)
Sets this Calendar's time with the given Date.
void setTimeInMillis(long millis)
Sets this Calendar's current time from the given long value.
void setTimeZone(TimeZone value)
Sets the time zone with the given time zone value.
boolean isSet(int field)
Determines if the given calendar field has a value set, including cases that the value has been set by internal fields calculations triggered by a get method call.

Convert Calendar value to string value

String toString()
Return a string representation of this calendar.

Revised from Open JDK source code

Home 
  Java Book 
    Essential Classes  

Calendar:
  1. Calendar class
  2. Constants value in Calendar
  3. Create new Calendar instance
  4. Get field value from Calendar
  5. Compare two Calendar values
  6. Calendar and display name
  7. Is lenient
  8. Add/set value to a field and get new calendar value
  9. Convert Calendar value to string value