The perfect place for easy learning...

Java Programming


Topics List

Calendar class in java

The Calendar is a built-in abstract class in java used to convert date between a specific instant in time and a set of calendar fields such as MONTH, YEAR, HOUR, etc. The Calendar class is available inside the java.util package.

The Calendar class implements Serializable, Cloneable and Comparable interface.

🔔 As the Calendar class is an abstract class, we can not create an object using it.

🔔 We will use the static method Calendar.getInstance() to instantiate and implement a sub-class.

The Calendar class in java has the following methods.

S.No. Methods with Description
1 Calendar getInstance()

It returns a calendar using the default time zone and locale.

2 Date getTime()

It returns a Date object representing the invoking Calendar's time value.

3 TimeZone getTimeZone()

It returns the time zone object associated with the invoking calendar.

4 String getCalendarType()

It returns an instance of Date object from Instant date.

5 int get(int field)

It rerturns the value for the given calendar field.

6 int getFirstDayOfWeek()

It returns the day of the week in integer form.

7 int getWeeksInWeekYear()

It retruns the total weeks in week year.

8 int getWeekYear()

It returns the week year represented by current Calendar.

9 void add(int field, int amount)

It adds the specified amount of time to the given calendar field.

10 boolean after (Object when)

It returns true if the time represented by the Calendar is after the time represented by when Object.

11 boolean before(Object when)

It returns true if the time represented by the Calendar is before the time represented by when Object.

12 void clear(int field)

It sets the given calendar field value and the time value of this Calendar undefined.

13 Object clone()

It retruns the copy of the current object.

14 int compareTo(Calendar anotherCalendar)

It compares and retruns the time values (millisecond offsets) between two calendar object.

15 void complete()

It sets any unset fields in the calendar fields.

16 void computeFields()

It converts the current millisecond time value time to calendar field values in fields[].

17 void computeTime()

It converts the current calendar field values in fields[] to the millisecond time value time.

18 boolean equals(Object object)

It returns true if both invoking object and argumented object are equal.

19 int getActualMaximum(int field)

It returns the Maximum possible value of the specified calendar field.

20 int getActualMinimum(int field)

It returns the Minimum possible value of the specified calendar field.

21 Set getAvailableCalendarTypes()

It returns a string set of all available calendar type supported by Java Runtime Environment.

22 Locale[] getAvailableLocales()

It returns an array of all locales available in java runtime environment.

23 String getDisplayName(int field, int style, Locale locale)

It returns the String representation of the specified calendar field value in a given style, and local.

24 Map getDisplayNames(int field, int style, Locale locale)

It returns Map representation of the given calendar field value in a given style and local.

25 int getGreatestMinimum(int field)

It returns the highest minimum value of the specified Calendar field.

26 int getLeastMaximum(int field)

It returns the highest maximum value of the specified Calendar field.

27 int getMaximum(int field)

It returns the maximum value of the specified calendar field.

28 int getMinimalDaysInFirstWeek()

It returns required minimum days in integer form.

29 int getMinimum(int field)

It returns the minimum value of specified calendar field.

30 long getTimeInMillis()

It returns the current time in millisecond.

31 int hashCode()

It returns the hash code of the invoking object.

32 int internalGet(int field)

It returns the value of the given calendar field.

33 boolean isLenient()

It returns true if the interpretation mode of this calendar is lenient; false otherwise.

34 boolean isSet(int field)

If not set then it returns false otherwise true.

35 boolean isWeekDateSupported()

It returns true if the calendar supports week date. The default value is false.

36 void roll(int field, boolean up)

It increase or decrease the specified calendar field by one unit without affecting the other field

37 void set(int field, int value)

It sets the specified calendar field by the specified value.

38 void setFirstDayOfWeek(int value)

It sets the first day of the week.

39 void setMinimalDaysInFirstWeek(int value)

It sets the minimal days required in the first week.

40 void setTime(Date date)

It sets the Time of current calendar object.

41 void setTimeInMillis(long millis)

It sets the current time in millisecond.

42 void setTimeZone(TimeZone value)

It sets the TimeZone with passed TimeZone value.

43 void setWeekDate(int weekYear, int weekOfYear, int dayOfWeek)

It sets the current date with specified integer value.

44 Instant toInstant()

It converts the current object to an instant.

45 String toString()

It returns a string representation of the current object.

Let's consider an example program to illustrate methods of Calendar class.

import java.util.*;

public class CalendarClassExample {

	public static void main(String[] args) {

        Calendar cal = Calendar.getInstance();
        System.out.println("Current date and time : \n=>" + cal);
        System.out.println("Current Calendar type : " + cal.getCalendarType());
        System.out.println("Current date and time : \n=>" + cal.getTime());
        System.out.println("Current date time zone : \n=>" + cal.getTimeZone());
        System.out.println("Calendar filed 1 (year): " + cal.get(1));
        System.out.println("Calendar day in integer form: " + cal.getFirstDayOfWeek());
        System.out.println("Calendar weeks in a year: " + cal.getWeeksInWeekYear());
        System.out.println("Time in milliseconds: " + cal.getTimeInMillis());
        System.out.println("Available Calendar types: " + cal.getAvailableCalendarTypes());
        System.out.println("Calendar hash code: " + cal.hashCode());
        System.out.println("Is calendar supports week date? " + cal.isWeekDateSupported());
        System.out.println("Calendar string representation: " + cal.toString());		

When we execute the above code, it produce the following output.

Calendar class in java