Last Updated: 06 March, 2023
The Calendar is an abstract class that provides methods for converting dates between a specific instant in time and a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, etc. The Calendar class is available in the java.util package.
Calender is an abstract class, so we cannot create an object; we can use the static method Calendar.getInstance() to instantiate and implement a sub-class.
The getInstance() method returns a Calendar instance based on the current time in the default time zone with the default locale.
Java Calendar Class Example Program
Output
Current Date and time: Sun Feb 05 18:15:22 IST 2023
Calendar class declaration:
The abstract Calendar class extends Object class and implements Serializable, Cloneable, and Comparable interfaces in Java.
No. | Constructors and Description |
---|---|
1 | protected Calendar() It is used to construct a Calendar with the default time zone and locale. |
2 | protected Calendar(TimeZone zone, Locale aLocale) It is used to construct a calendar with the specified time zone and locale. |
No. | Methods and 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 the Date object from an instant date. |
5 | int get(int field) It returns the value for the given calendar field. |
6 | int getFirstDayOfWeek() It returns the day of the week in integer form. |
7 | int getWeeksInWeekYear() It reruns the total number of weeks in a year. |
8 | int getWeekYear() It returns the week and year represented by the 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 the 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 makes a copy of the current object. |
14 | int compareTo(Calendar anotherCalendar) It compares and retruns the time values (millisecond offsets) between two calendar objects. |
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 the invoking object and the 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 types supported by the 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 locale. |
24 | Map getDisplayNames(int field, int style, Locale locale) It returns a Map representation of the given calendar field value in a given style and locale. |
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 the required minimum days in integer form. |
29 | int getMinimum(int field) It returns the minimum value of the specified calendar field. |
30 | long getTimeInMillis() It returns the current time in milliseconds. |
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 the value is not set, it returns false; otherwise, it returns true. |
35 | boolean isWeekDateSupported() It returns true if the calendar supports the specified week date. The default value is false. |
36 | void roll(int field, boolean up) It increases or decreases 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 to 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 milliseconds. |
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. |
Example 1: Calendar class add() and getInstance() methods
Output
The current date is : Sun Mar 05 12:28:11 IST 2023
15 days ago: Sat Feb 18 12:28:11 IST 2023
4 months later: Sun Jun 18 12:28:11 IST 2023
5 years later: Wed Jun 18 12:28:11 IST 2025
Example 2: Calendar class get() method
Output
Current Calendar's Year: 2023
Current Calendar's Day: 5
Current MINUTE: 30
Current SECOND: 36
Example 3: Calendar class getMaximum() method
Output
Maximum number of days in a week: 7
Maximum number of weeks in a year: 53
Example 4: Calendar class getMinimum() method
Output
Minimum number of days in week: 1
Minimum number of weeks in year: 1
Reference: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Calendar.html
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com
Related Articles