Java Calendar Class

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Overview

Java's calendar class is an abstract class that gives you access to numerous calendar instances and methods for manipulating dates and times.

To address several issues with java. util.Date class, the java.util.Calendar class was added to Java with JDK 1.4. It did simplify some tasks; for example, it is now simpler to create arbitrary dates with the new GregorianCalendar(2016, Calendar.JUNE, 11) constructor than with the Date class, where the year starts from 1900 and the month starts from zero.

Introduction to Java Calendar Class

The Java Calendar class is an abstract class that offers methods for converting dates between a specific point in time and a collection of calendar fields like MONTH, YEAR, HOUR, and so on. It derives from an Object and implements a Comparable interface. Because it is an Abstract class, we cannot create an instance with a constructor. To instantiate and implement a sub-class, we must utilize the static method Calendar.getInstance().

Declaration

Fields for Java Calendar Class

The fields for the java.util.Calendar class are listed below.

FieldsDetails
static int ALL_STYLESThis specifies names in all styles, including "Jan" and "January" for getDisplayNames.
static int AMThe AM PM field value represents the time of day between midnight and shortly before noon.
static int AM_PMThis is the get and set field number that indicates if the HOUR is before or after noon.
static int APRILIn the Gregorian and Julian calendars, this is value of MONTH field, denoting the fourth month of year.
protected boolean areFieldsSetThis is true if the fields[] match the time currently specified.
static int AUGUSTIn the Gregorian and Julian calendars, this is value of MONTH field, denoting the eigth month of year.
static int DATEThis is set and get field number that indicates the month's day.
static int DAY_OF_MONTHThis is the set and get field number that indicates the day of the month.
static int DAY_OF_WEEKThis is the get and set field number that indicates the weekday.
static int DAY_OF_WEEK_IN_MONTHThis is set and get field number that shows the ordinal number of current month's day of the week.
static int DAY_OF_YEARThis is set and get field number that indicates the current year's day number.
static int DECEMBERIn the Gregorian and Julian calendars, this is value of MONTH field, denoting the twelfth month of year.
static int DST_OFFSETThis is set and get field number that shows the milliseconds of daylight savings time.
static int ERAThis is set and get field number that denotes the Julian calendar era, such as AD or BC.
static int FEBRUARYIn the Gregorian and Julian calendars, this is value of MONTH field, denoting second month of year.
static int FIELD_COUNTnumber of unique fields that get and set recognise.
protected int[] fieldsIt is the current time on the calendar's calendar field values.
static int FRIDAYIt is value of DAY OF WEEK field for Friday.
static int HOURThis is the set and get field number for hour of morning or afternoon.
static int HOUR_OF_DAYThis is the get and set field number that represents the time of day.
protected boolean[] isSetThese are the flags that indicate whether or not a certain calendar field is set for calendar.
protected boolean isTimeSetIf the time value is right, this is true.
static int JANUARYIn the Gregorian and Julian calendars, this is value of MONTH field, denoting first month of year.
static int JULYIn the Gregorian and Julian calendars, this is value of MONTH field, denoting the seventh month of year.
static int JUNEIn the Gregorian and Julian calendars, this is value of MONTH field, denoting the sixth month of year.
static int LONGWhen a long name, such as "January," is supplied, this is the style specifier for getDisplayName and getDisplayNames.
static int MARCHIn the Gregorian and Julian calendars, this is value of MONTH field, denoting the third month of year.
static int MAYIn the Gregorian and Julian calendars, this is value of MONTH field, denoting the fifth month of year.
static int MILLISECONDThis is the get and set field number, which represents a millisecond inside a second.
static int MINUTEThis is the get and set field number that indicates the minute inside an hour.
static int MONDAYThis is the value of the DAY OF WEEK field for Monday.
static int MONTHThis is the field number for the month for get and set.
static int NOVEMBERIn the Gregorian and Julian calendars, this is value of MONTH field, denoting the eleventh month of year..
static int OCTOBERIn the Gregorian and Julian calendars, this is value of MONTH field, denoting the tenth month of year.
static int PMThe AM PM field value represents the time of day between noon and shortly before midnight.
static int SATURDAYThis is the Saturday value for the DAY OF WEEK field.
static int SECONDThis is the get and set field number for the second inside the minute.
static int SEPTEMBERIn the Gregorian and Julian calendars, this is value of MONTH field, denoting the ninth month of year.
static int SHORTWhen a short name, such as "Jan," is supplied, this is the style specifier for getDisplayName and getDisplayNames.
static int SUNDAYThis is the value of the DAY OF WEEK field for Sunday.
static int THURSDAYThis is the Thursday value for the DAY OF WEEK field.
protected long timeAfter January 1, 1970, 0:00:00 GMT, this is the calendar's current time, reported in milliseconds.
static int TUESDAYThis is the value of the DAY OF WEEK field for Tuesday.
static int UNDECIMBERThis is the value for the MONTH field for the thirteenth month of the year..
static int WEDNESDAYThis is the value of the DAY OF WEEK field for Wednesday.
static int WEEK_OF_MONTHThis is the get and set field number that indicates the current month's week number.
static int WEEK_OF_YEAR −This is the Field number for get and set, which returns the week number for the current year.
static int YEARThis is the get and set field number that reflects the year.
static int ZONE_OFFSETThis is the field number used to retrieve and set the raw offset from GMT in milliseconds.

Constructors of Java Calendar Class

ConstructorDescrption
protected Calendar()This  constructor creates a Calendar with the default time zone and locale.
protected Calendar(TimeZone zone, Locale aLocale)This function Constructor creates a calendar using the time zone and locale supplied.

Methods of Java Calendar Class

MethodDescription
abstract void add(int field, int amount)Based on the calendar's rules, this function adds or subtracts the provided amount of time from the supplied calendar field.
boolean after(Object when)This method returns whether this Calendar represents a time after the time represented by the specified Object.
void clear()This method renders this Calendar's calendar field values and time value (millisecond offset from the Epoch) undefined.
Object clone()This function copies this object and returns it.
int compareTo(Calendar anotherCalendar)This function compares two Calendar objects' time values (millisecond offsets from the Epoch).
protected void complete()This function populates any calendar fields that have unset values.
protected abstract void computeFields()The current millisecond time value time is converted to calendar field values in fields[].
protected abstract void computeTime()The current calendar field values in fields[] are converted to the millisecond time value time using this procedure.
boolean equals(Object obj)This function compares this Calendar to the Object given.
int get(int field)This function returns the value of the calendar field specified.
int getActualMaximum(int field)Given the time value of this Calendar, this function returns the greatest value that the specified calendar field might have.
int getActualMinimum(int field)Given the time value of this Calendar, this function returns the smallest value that the specified calendar field might have.
static Locale[] getAvailableLocales()This method produces an array containing all locales for which this class's getInstance methods may provide localised instances.
String getDisplayName(int field, int style, Locale locale)The method returns a string representation of the calendar field value in the style and location specified.
Map<String,Integer> getDisplayNames(int field, int style, Locale locale)This function produces a Map containing all calendar field names in the provided style and location, as well as their associated field values.
int getFirstDayOfWeek()This approach determines the first day of the week, such as SUNDAY in the United States and MONDAY in France.
abstract int getGreatestMinimum(int field)This function returns the Calendar instance's maximum minimum value for the provided calendar field.
static Calendar getInstance()This function retrieves a calendar based on the current time zone and location.
static Calendar getInstance(Locale aLocale)This function retrieves a calendar using the default time zone and locale.
static Calendar getInstance(TimeZone zone)This function retrieves a calendar based on the time zone and locale given.
static Calendar getInstance(TimeZone zone, Locale aLocaleThis function returns a calendar with the time zone and locale supplied.
abstract int getLeastMaximum(int field)This method returns the Calendar instance's lowest maximum value for the provided calendar field.
abstract int getMaximum(int field)This method returns the maximum value for the provided Calendar instance's calendar property.
int getMinimalDaysInFirstWeek()This function returns the minimum number of days necessary in the first week of the year; for example, if the first week is defined as one that includes the first day of the first month of the year, this method returns 1.
abstract int getMinimum(int field)This method returns the smallest value for the provided Calendar instance's calendar property.
Date getTime()This function returns a Date object with the time value of this Calendar (millisecond offset from the Epoch").
long getTimeInMillis()This function returns the time value of this Calendar in milliseconds.
TimeZone getTimeZone()The time zone is obtained using this way.
int hashCode()This technique This calendar's hash code is returned.
protected int internalGet(int field)This function returns the value of the calendar field specified.
boolean isLenient()This technique indicates if date/time interpretation should be lax.
boolean isSet(int field)This method detects if a value has been set for the supplied calendar field, including circumstances where the value has been set by internal field computations triggered by a get method call.
abstract void roll(int field, boolean up)This method adds or subtracts (up/down) a single unit of time from the provided time field while leaving the bigger fields alone.
void roll(int field, int amount)This method inserts the provided (signed) amount to the chosen calendar field while leaving the bigger fields alone.
void set(int field, int value)This method changes the value of the specified calendar field.
void set(int year, int month, int date)This method configures the calendar fields YEAR, MONTH, and DAY OF MONTH.
void set(int year, int month, int date, int hourOfDay, int minute)This method populates the calendar fields YEAR, MONTH, DAY OF MONTH, HOUR OF DAY, and MINUTE with values.
void set(int year, int month, int date, int hourOfDay, int minute, int second)This method populates the fields YEAR, MONTH, DAY OF MONTH, HOUR, MINUTE, and SECOND with values.
void setFirstDayOfWeek(int value)This method determines the first day of the week, such as SUNDAY in the United States and MONDAY in France.
void setLenient(boolean lenient)This method indicates whether date/time interpretation should be lax or strict.
void setMinimalDaysInFirstWeek(int value)This method specifies the minimum number of days necessary in the first week of the year; for example, if the first week is defined as one that includes the first day of the first month of the year, call this method with value.
void setTime(Date date)This function sets the time on this Calendar to the supplied Date.
void setTimeInMillis(long millis)This method sets the current time of this Calendar to the specified long value.
void setTimeZone(TimeZone value)This method sets the time zone to the specified value.
String toString()This function returns the calendar's string representation.

Java Calendar Class Examples

Example1

In this example we are going to see how to get instance of calendar class using getinstance() method and get current date and time using getTime() method.

Output

Code Explanation: In above example we have used getInstance() method to get the Instance of calendar class. getTime() is used to get the current date and time.

Example2

In this example we are going to create instance of calendar class and use add() method.

Output

Code Explanation: In Above example we have created a instance of calendar class using getinstance() method. We have used add() method to with field parameter as Calender.DATE and amount parameter as -15 this will allow us to get 15 days before current time.

Example 3

In this example we are going to use getMaximum() method.

Output

Code Explanation: In this example we have used getMaximum() to get the maximum value of days in week and weeks in year.

Conclusion

  • The Java Calendar class is an abstract class that offers methods for converting dates between a certain point in time and a collection of calendar fields like MONTH, YEAR, HOUR, and so on.
  • Because it is an Abstract class, we cannot create an instance with a constructor
  • To instantiate and implement a sub-class, we must instead utilise the static method Calendar.getInstance()
  • add() function adds or subtracts the provided amount of time from the supplied calendar field.
  • getTime() returns a Date object with the time value of this Calendar (millisecond offset from the Epoch")