GeezerVison is showing docs.oracle.com — Calendar (Java Platform SE 8 )
Java™ Platform Standard Ed. 8
compact1, compact2, compact3
java.util
Class Calendar
-
-
Nested Class Summary
Nested Classes
| Modifier and Type |
Class |
Description |
static class |
Calendar.Builder |
Calendar.Builder is used for creating a Calendar from
various date-time parameters.
|
-
Field Summary
Fields
| Modifier and Type |
Field |
Description |
static int |
ALL_STYLES |
A style specifier for getDisplayNames indicating names in all styles, such as
"January" and "Jan".
|
static int |
AM |
Value of the AM_PM field 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 |
APRIL |
Value of the MONTH field indicating the
fourth month of the year in the Gregorian and Julian calendars.
|
protected boolean |
areFieldsSet |
True if fields[] are in sync with the currently set time.
|
static int |
AUGUST |
Value of the MONTH field indicating the
eighth month of the year in the Gregorian and Julian calendars.
|
static int |
DATE |
Field number for get and set indicating the
day of the month.
|
static int |
DAY_OF_MONTH |
Field number for get and set indicating the
day of the month.
|
static int |
DAY_OF_WEEK |
Field number for get and set indicating the day
of the week.
|
static int |
DAY_OF_WEEK_IN_MONTH |
Field number for get and set indicating the
ordinal number of the day of the week within the current month.
|
static int |
DAY_OF_YEAR |
Field number for get and set indicating the day
number within the current year.
|
static int |
DECEMBER |
Value of the MONTH field indicating the
twelfth month of the year in the Gregorian and Julian calendars.
|
static int |
DST_OFFSET |
Field number for get and set indicating the
daylight saving offset in milliseconds.
|
static int |
ERA |
Field number for get and set indicating the
era, e.g., AD or BC in the Julian calendar.
|
static int |
FEBRUARY |
Value of the MONTH field indicating the
second month of the year in the Gregorian and Julian calendars.
|
static int |
FIELD_COUNT |
The number of distinct fields recognized by get and set.
|
protected int[] |
fields |
The calendar field values for the currently set time for this calendar.
|
static int |
FRIDAY |
|
static int |
HOUR |
Field number for get and set indicating the
hour of the morning or afternoon.
|
static int |
HOUR_OF_DAY |
Field number for get and set indicating the
hour of the day.
|
protected boolean[] |
isSet |
The flags which tell if a specified calendar field for the calendar is set.
|
protected boolean |
isTimeSet |
True if then the value of time is valid.
|
static int |
JANUARY |
Value of the MONTH field indicating the
first month of the year in the Gregorian and Julian calendars.
|
static int |
JULY |
Value of the MONTH field indicating the
seventh month of the year in the Gregorian and Julian calendars.
|
static int |
JUNE |
Value of the MONTH field indicating the
sixth month of the year in the Gregorian and Julian calendars.
|
static int |
LONG |
|
static int |
LONG_FORMAT |
|
static int |
LONG_STANDALONE |
|
static int |
MARCH |
Value of the MONTH field indicating the
third month of the year in the Gregorian and Julian calendars.
|
static int |
MAY |
Value of the MONTH field indicating the
fifth month of the year in the Gregorian and Julian calendars.
|
static int |
MILLISECOND |
Field number for get and set indicating the
millisecond within the second.
|
static int |
MINUTE |
Field number for get and set indicating the
minute within the hour.
|
static int |
MONDAY |
|
static int |
MONTH |
Field number for get and set indicating the
month.
|
static int |
NARROW_FORMAT |
|
static int |
NARROW_STANDALONE |
|
static int |
NOVEMBER |
Value of the MONTH field indicating the
eleventh month of the year in the Gregorian and Julian calendars.
|
static int |
OCTOBER |
Value of the MONTH field indicating the
tenth month of the year in the Gregorian and Julian calendars.
|
static int |
PM |
Value of the AM_PM field indicating the
period of the day from noon to just before midnight.
|
static int |
SATURDAY |
|
static int |
SECOND |
Field number for get and set indicating the
second within the minute.
|
static int |
SEPTEMBER |
Value of the MONTH field indicating the
ninth month of the year in the Gregorian and Julian calendars.
|
static int |
SHORT |
|
static int |
SHORT_FORMAT |
|
static int |
SHORT_STANDALONE |
A style specifier for getDisplayName and getDisplayNames indicating a short name used independently,
such as a month abbreviation as calendar headers.
|
static int |
SUNDAY |
|
static int |
THURSDAY |
|
protected long |
time |
The currently set time for this calendar, expressed in milliseconds after
January 1, 1970, 0:00:00 GMT.
|
static int |
TUESDAY |
|
static int |
UNDECIMBER |
Value of the MONTH field indicating the
thirteenth month of the year.
|
static int |
WEDNESDAY |
|
static int |
WEEK_OF_MONTH |
Field number for get and set indicating the
week number within the current month.
|
static int |
WEEK_OF_YEAR |
Field number for get and set indicating the
week number within the current year.
|
static int |
YEAR |
Field number for get and set indicating the
year.
|
static int |
ZONE_OFFSET |
Field number for get and set
indicating the raw offset from GMT in milliseconds.
|
-
Constructor Summary
Constructors
| Modifier |
Constructor |
Description |
protected |
Calendar() |
Constructs a Calendar with the default time zone
and the default FORMAT
locale.
|
protected |
Calendar(TimeZone zone,
Locale aLocale) |
Constructs a calendar with the specified time zone and locale.
|
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods
| Modifier and Type |
Method |
Description |
abstract 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.
|
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.
|
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.
|
Object |
clone() |
Creates and returns a copy of this object.
|
int |
compareTo(Calendar anotherCalendar) |
Compares the time values (millisecond offsets from the Epoch) represented by two
Calendar objects.
|
protected void |
complete() |
Fills in any unset fields in the calendar fields.
|
protected abstract void |
computeFields() |
Converts the current millisecond time value time
to calendar field values in fields[].
|
protected abstract void |
computeTime() |
Converts the current calendar field values in fields[]
to the millisecond time value
time.
|
boolean |
equals(Object obj) |
Compares this Calendar to the specified
Object.
|
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.
|
static Set<String> |
getAvailableCalendarTypes() |
Returns an unmodifiable Set containing all calendar types
supported by Calendar in the runtime environment.
|
static Locale[] |
getAvailableLocales() |
Returns an array of all locales for which the getInstance
methods of this class can return localized instances.
|
String |
getCalendarType() |
Returns the calendar type of this Calendar.
|
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.
|
int |
getFirstDayOfWeek() |
Gets what the first day of the week is; e.g., SUNDAY in the U.S.,
MONDAY in France.
|
abstract int |
getGreatestMinimum(int field) |
Returns the highest minimum value for the given calendar field
of this 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.
|
abstract int |
getLeastMaximum(int field) |
Returns the lowest maximum value for the given calendar field
of this Calendar instance.
|
abstract int |
getMaximum(int field) |
Returns the maximum value for the given calendar field of this
Calendar instance.
|
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.
|
abstract int |
getMinimum(int field) |
Returns the minimum value for the given calendar field of this
Calendar instance.
|
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.
|
int |
getWeeksInWeekYear() |
Returns the number of weeks in the week year represented by this
Calendar.
|
int |
getWeekYear() |
Returns the week year represented by this Calendar.
|
int |
hashCode() |
Returns a hash code for this calendar.
|
protected int |
internalGet(int field) |
Returns the value of the given calendar field.
|
boolean |
isLenient() |
Tells whether date/time interpretation is to be lenient.
|
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.
|
boolean |
isWeekDateSupported() |
Returns whether this Calendar supports week dates.
|
abstract void |
roll(int field,
boolean up) |
Adds or subtracts (up/down) a single unit of time on the given time
field without changing larger fields.
|
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_OF_DAY, 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.
|
void |
setWeekDate(int weekYear,
int weekOfYear,
int dayOfWeek) |
Sets the date of this Calendar with the the given date
specifiers - week year, week of year, and day of week.
|
Instant |
toInstant() |
Converts this object to an Instant.
|
String |
toString() |
Return a string representation of this calendar.
|
-
-
Field Detail
-
MONTH
public static final int MONTH
Field number for get and set indicating the
month. This is a calendar-specific value. The first month of
the year in the Gregorian and Julian calendars is
JANUARY which is 0; the last depends on the number
of months in a year.
- See Also:
JANUARY,
FEBRUARY,
MARCH,
APRIL,
MAY,
JUNE,
JULY,
AUGUST,
SEPTEMBER,
OCTOBER,
NOVEMBER,
DECEMBER,
UNDECIMBER,
Constant Field Values
-
WEEK_OF_YEAR
public static final int WEEK_OF_YEAR
Field number for get and set indicating the
week number within the current year. The first week of the year, as
defined by getFirstDayOfWeek() and
getMinimalDaysInFirstWeek(), has value 1. Subclasses define
the value of WEEK_OF_YEAR for days before the first week of
the year.
- See Also:
getFirstDayOfWeek(),
getMinimalDaysInFirstWeek(),
Constant Field Values
-
WEEK_OF_MONTH
public static final int WEEK_OF_MONTH
Field number for get and set indicating the
week number within the current month. The first week of the month, as
defined by getFirstDayOfWeek() and
getMinimalDaysInFirstWeek(), has value 1. Subclasses define
the value of WEEK_OF_MONTH for days before the first week of
the month.
- See Also:
getFirstDayOfWeek(),
getMinimalDaysInFirstWeek(),
Constant Field Values
-
DAY_OF_WEEK
public static final int DAY_OF_WEEK
Field number for get and set indicating the day
of the week. This field takes values SUNDAY,
MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, and SATURDAY.
- See Also:
SUNDAY,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
Constant Field Values
-
DAY_OF_WEEK_IN_MONTH
public static final int DAY_OF_WEEK_IN_MONTH
Field number for get and set indicating the
ordinal number of the day of the week within the current month. Together
with the DAY_OF_WEEK field, this uniquely specifies a day
within a month. Unlike WEEK_OF_MONTH and
WEEK_OF_YEAR, this field's value does not depend on
getFirstDayOfWeek() or
getMinimalDaysInFirstWeek(). DAY_OF_MONTH 1
through 7 always correspond to DAY_OF_WEEK_IN_MONTH
1; 8 through 14 correspond to
DAY_OF_WEEK_IN_MONTH 2, and so on.
DAY_OF_WEEK_IN_MONTH 0 indicates the week before
DAY_OF_WEEK_IN_MONTH 1. Negative values count back from the
end of the month, so the last Sunday of a month is specified as
DAY_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 -1 will overlap
DAY_OF_WEEK_IN_MONTH 5 and the end of 4.
- See Also:
DAY_OF_WEEK,
WEEK_OF_MONTH,
Constant Field Values
-
AM_PM
public static final int AM_PM
Field 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
-
HOUR
public static final int HOUR
Field number for get and set indicating the
hour of the morning or afternoon. HOUR is used for the
12-hour clock (0 - 11). Noon and midnight are represented by 0, not by 12.
E.g., at 10:04:15.250 PM the HOUR is 10.
- See Also:
AM_PM,
HOUR_OF_DAY,
Constant Field Values
-
HOUR_OF_DAY
public static final int HOUR_OF_DAY
Field 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:
HOUR,
Constant Field Values
-
MINUTE
public static final int MINUTE
Field 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
-
SECOND
public static final int SECOND
Field 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
-
MILLISECOND
public static final int MILLISECOND
Field 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
-
isSet
protected boolean[] isSet
The flags which tell if a specified calendar field for the calendar is set.
A new object has no fields set. After the first call to a method
which generates the fields, they all remain set after that.
This is an array of FIELD_COUNT booleans, with index values
ERA through DST_OFFSET.
-
time
protected long time
The currently set time for this calendar, expressed in milliseconds after
January 1, 1970, 0:00:00 GMT.
- See Also:
isTimeSet
-
areFieldsSet
protected boolean areFieldsSet
True if fields[] are in sync with the currently set time.
If false, then the next attempt to get the value of a field will
force a recomputation of all fields from the current value of
time.
-
Constructor Detail
-
Calendar
protected Calendar()
Constructs a Calendar with the default time zone
and the default FORMAT
locale.
- See Also:
TimeZone.getDefault()
-
Calendar
protected Calendar(TimeZone zone,
Locale aLocale)
Constructs a calendar with the specified time zone and locale.
- Parameters:
zone - the time zone to use
aLocale - the locale for the week data
-
Method Detail
-
getInstance
public static Calendar getInstance(Locale aLocale)
Gets a calendar using the default time zone and specified locale.
The Calendar returned is based on the current time
in the default time zone with the given locale.
- Parameters:
aLocale - the locale for the week data
- Returns:
- a Calendar.
-
getInstance
public static Calendar getInstance(TimeZone zone,
Locale aLocale)
Gets a calendar with the specified time zone and locale.
The Calendar returned is based on the current time
in the given time zone with the given locale.
- Parameters:
zone - the time zone to use
aLocale - the locale for the week data
- Returns:
- a Calendar.
-
set
public void set(int field,
int value)
Sets the given calendar field to the given value. The value is not
interpreted by this method regardless of the leniency mode.
- Parameters:
field - the given calendar field.
value - the value to be set for the given calendar field.
- Throws:
ArrayIndexOutOfBoundsException - if the specified field is out of range
(field < 0 || field >= FIELD_COUNT).
in non-lenient mode.
- See Also:
set(int,int,int),
set(int,int,int,int,int),
set(int,int,int,int,int,int),
get(int)
-
set
public final 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.
Previous values of other fields are retained. If this is not desired,
call clear() first.
- Parameters:
year - the value used to set the YEAR calendar field.
month - the value used to set the MONTH calendar field.
Month value is 0-based. e.g., 0 for January.
date - the value used to set the DAY_OF_MONTH calendar field.
hourOfDay - the value used to set the HOUR_OF_DAY calendar field.
minute - the value used to set the MINUTE calendar field.
- See Also:
set(int,int),
set(int,int,int),
set(int,int,int,int,int,int)
-
set
public final 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_OF_DAY, MINUTE, and
SECOND.
Previous values of other fields are retained. If this is not desired,
call clear() first.
- Parameters:
year - the value used to set the YEAR calendar field.
month - the value used to set the MONTH calendar field.
Month value is 0-based. e.g., 0 for January.
date - the value used to set the DAY_OF_MONTH calendar field.
hourOfDay - the value used to set the HOUR_OF_DAY calendar field.
minute - the value used to set the MINUTE calendar field.
second - the value used to set the SECOND calendar field.
- See Also:
set(int,int),
set(int,int,int),
set(int,int,int,int,int)
-
isSet
public final 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.
- Parameters:
field - the calendar field to test
- Returns:
true if the given calendar field has a value set;
false otherwise.
-
setLenient
public void setLenient(boolean lenient)
Specifies 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 (non-lenient) interpretation, such dates will cause an exception to be
thrown. The default is lenient.
- Parameters:
lenient - true if the lenient mode is to be turned
on; false if it is to be turned off.
- See Also:
isLenient(),
DateFormat.setLenient(boolean)
-
setFirstDayOfWeek
public void setFirstDayOfWeek(int value)
Sets what the first day of the week is; e.g., SUNDAY in the U.S.,
MONDAY in France.
- Parameters:
value - the given first day of the week.
- See Also:
getFirstDayOfWeek(),
getMinimalDaysInFirstWeek()
-
setMinimalDaysInFirstWeek
public 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. If it
must be a full week, use value 7.
- Parameters:
value - the given minimal days required in the first week
of the year.
- See Also:
getMinimalDaysInFirstWeek()
-
getMinimalDaysInFirstWeek
public 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. If
the minimal days required must be a full week, this method
returns 7.
- Returns:
- the minimal days required in the first week of the year.
- See Also:
setMinimalDaysInFirstWeek(int)
Java™ Platform Standard Ed. 8
Submit a bug or feature For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples. Copyright © 1993, 2026, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.
|