Published Aug 6, 2023
Contribute to Docs

The Calendar class is an abstract class that represents dates and time. The class has methods for converting between a given moment in time and a number of calendar attributes such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and so on.


Calendar calendar = Calendar.getInstance();

Note: The .getInstance() function creates an object that represents the current date and time.


In the example below, a Calendar object is created using the getInstance() method. Then the set() method is used to set the year, month, date, hour, minute, and second. Finally, the getTime() method is used to get the date. Note that the month is zero-based, so January is 0, February is 1, and so on. Also, the set() overload could have been used that takes all the parameters at once.

import java.util.Date;
import java.util.Calendar;
public class CalendarExample {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, 2023);
calendar.set(Calendar.MONTH, 0);
calendar.set(Calendar.DATE, 8);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
// calendar.set(2023, 0, 8, 0, 0, 0);
Date date = calendar.getTime();

The above code will output:

Sun Jan 08 00:00:00 GMT 2023


There are many fields in the Calendar class. Here are some of the most important ones:

  • YEAR: The field indicating the year.
  • MONTH: The field indicating the month.
  • DAY_OF_MONTH: The field indicating the day of the month.
  • DATE: Synonym for DAY_OF_MONTH.
  • HOUR: The field indicating the hour of the day.
  • MINUTE: The field indicating the minute within the hour.
  • SECOND: The field indicating the second within the minute.
  • MILLISECOND: The field indicating the millisecond within the second.


Methods of the Calendar class include:


Adds or subtracts a specified amount of time to/from the given Calendar field.
Compares whether the current instance of Calendar occurs after the time represented by the specified object.
Returns a boolean based on the whether one Calendar instance is before the other given instance.
A method designed to reset or clear specific fields of a Calendar instance.
Returns a copy of a Calendar object.
Compares a passed Calendar object with an existing Calendar object.
A method to fill in any empty fields of a Calendar instance.
Synchronizes the time of a Calendar object with the set field values.
Used to determine if two Calendar objects are equal.
Returns the value of the given calendar field.
Returns the actual maximum value for a specific calendar field, conditional on the time value of the calendar.
Returns the minimum value allowed for a given calendar field.
A method that is used to get list of all available calendar types in Java.
Returns a map containing all string representations of Calendar field values in the given style and locale.
Returns the minimum value for the given calendar field.
Returns the time value of the given Calendar object.
Returns the time in milliseconds.
Returns the current time-zone of a given Calendar.
Returns the total number of weeks in a week year.
Returns the week year.
Returns the value of a given field.
Returns a boolean identifying a given calendar instance as lenient or not.
Evaluates whether the given calendar field has a value set or not.
Determines if the current Calendar object supports week dates.
Sets whether the date/time interpretation should be lenient or not.
Sets how many minimal days required in the first week of the year.

All contributors

Looking to contribute?

Learn Java on Codecademy