java.util.Calendar

This java tutorial focuses on the usage of the Calendar class of java.util package. We will be covering the basic usage of Calendar class until the most advanced features of this class. The Calendar class is helpful in dealing with dates in java. It is a must to learn the usage of Calendar because this is the most important class of Java in dealing with Dates. If you are still using Date as your main way of dealing with time, you might want to note that this class is already deprecated to give way to Calendar class.

Our approach is to show each example for each Calendar methods. But before digging in we will show first on how to use the Calendar class.

Calendar Class Basics

The Calendar class is an abstract class that provides methods for converting between a specific instant in time and a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and so on, and for manipulating the calendar fields, such as getting the date of the next week.

The calendar class provides additional methods and fields for this class to be able to  build a concrete calendar facility outside of java.util package by implementing this class.

To create a calendar object, it is required to call it via Calendar.getInstance(). This is not the usual stuff wherein you instantiate a new object by keyword new, instead we have to do this:

Calendar cal = Calendar.getInstance();

In order to modify the values of the Calendar object, set methods are readily available.

Calendar Compatibility

Requires JDK 1.1

 Calendar Methods

The following are the Calendar 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. To add just put unsigned value on the amount method argument. To subtract, the amount method argument should be negative number.
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 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. This is a static method thus its required that it should be statically called for example Calendar.getInstance().
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 the value of time in milliseconds stored on this calendar object.
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() this method just return the hashcode value of this Calendar object.
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.