Calendar Class in Java
Date and time have become integral parts of our lives. Automating them has always been of priority. Java includes an abstract class to convert dates and time in the form of calendar fields like MONTH, YEAR, HOUR, MINUTES, and so on. This article will give you a deep insight into Java calendar class and the various inbuilt functions present in it.
Java Calendar class declaration:
To declare the calendar class, we must inherit the Object class and implement the Comparable interface.
public abstract class Calendar extends Object implements Serializable, Cloneable, Comparable<Calendar>
This is the snippet to declare a calendar class.
Java Calendar class methods:
Method | Description | |
1 | public void add(int field, int amount) | It adds the mentioned amount of time to the given calendar field. |
2 | public abstract void computeTime() | It converts the values in the calendar field to the respective millisecond time value. |
3 | public abstract void computeFields() | It converts the current millisecond time value to its respective calendar field value. |
4 | public boolean after(Object when) | It returns a boolean value. It returns true if the Calendar’s time is after the time that the object represents. |
5 | public boolean before(Object when) | It returns a boolean value. It returns true if the Calendar’s time is before the time that the object represents. |
6 | public boolean equals(Object object) | This method compares two objects and returns true if they are equal. |
7 | protected void complete() | It fills the unset calendar fields. |
8 | public final void clear(int field) | It sets the fields of the calendar and the time. |
9 | public int compareTo(Calendar anotherCalendar) | It compares the time values between two calendar objects. |
10 | public Object clone() | It gives the copy of the current object. |
11 | public abstract int getLeastMinimum(int field) | It returns the smallest value from all the maximum values in the given Calendar field |
12 | public abstract int getGreatestMinimum(int field) | It returns the highest minimum value in the given Calendar field |
13 | public abstract getMaximum(int field) | It returns the largest value from the given calendar field. |
14 | public int getMinimum(int field) | It returns the smallest value from the given calendar field. |
15 | public int getMinimalDaysInFirstWeek() | It returns the minimum days in the integer form |
16 | public int get(int field) | It returns the value of the fields as the parameter. |
17 | public static Locale[] getAvailableLocales() | It returns all the locale variables available in an array form. |
18 | public int getActualMinimum(int field) | It returns the minimum possible value that is passed as a parameter to the getActualMinimum() method. |
19 | public int getActualMaximum(int field) | It returns the minimum possible value that is passed as a parameter to the getActualMaximum() method. |
20 | public static Set<String> getAvailableCalendarTypes() | It returns a set that consists of all the available calendar types that the Java RunTimeEnvironment supports. |
21 | public String getCalendarType() | It returns all the available calendar types in the String form. |
22 | public String getDisplayName(int field, int style, Locale locale) | It returns the calendar field in the form of String representation in the mentioned style and local |
23 | public Map<String, Integer> getDisplaynames(int field, int style, Locale locale) | It returns the map representation of the calendar filed value that is passed as a parameter in the given style local |
24 | public int getFirstDayOfWeek() | It returns the first day of the current week in integer form |
25 | public static Calendar getInstance() | It obtains the instance with the calendar object according to the current time zone that is set by the java runtime environment. |
26 | public final Date getTime() | It obtains the time value of the calendar object and returns the date. |
27 | public long getTimeInMillis() | It returns the current time in milliseconds in long data type |
28 | public int getWeeksInWeekYear() | It returns the total number of weeks in a week year in integer form |
29 | public int getWeekYear() | It obtains the week that the current calendar denotes. |
30 | public TimeZone getTimeZone() | It obtains the TimeZone of the calendar object and returns a TimeZone object. |
31 | public int hashCode() | It returns the hash code for the calendar object. |
32 | protected final int internalGet(int field) | It returns the value of the calendar field that is given as a parameter |
33 | public boolean isLenient() | It returns a boolean value. It returns true if the interpretation mode of the calendar is lenient, else it is false. |
34 | public final boolean isSet(int field) | It checks if the parameter of the given field is sent or not. If sent, it returns true, other false. |
35 | public void set(int field, int value) | It sets the calendar field with the given value |
36 | public boolean isWeekDateSupported() | It checks if the calendar supports the week date and returns true if it does. The default value is false. |
37 | public abstract void roll(int field, boolean up) | It increases or decreases the mentioned calendar field by one unit without altering the other fields. |
38 | public void setTimeInMillis(long millis) | It sets the current time in milliseconds. |
39 | public void setFirstDayOfWeek(int value) | It sets the first day of the week. |
40 | public void setMinimalDaysInFirstWeek(int value) | It sets the minimal days required in the first week. |
41 | public final void setTime(Date date) | It sets the time of the current calendar object. |
42 | public String toString() | It returns the string representation of the current object. |
43 | public final Instant toInstant() | It converts the current object to an instant. |
44 | public void setWeekDate(int weekyear, int weekOfYear, int dayOfWeek) | It sets the current date with the given integer value line weekyear, weekOfYear, and dayofWeek. |
45 | public void setTimeZone(TimeZone value) | It sets the TimeZone with the timeZone value given as the parameter. |
You can work out all these methods with a simple java program.
The program given below returns the first day of the week.
import java.util.*; public class DFCalendarSample{ public static void main(String[] args) { Calendar cal = Calendar.getInstance(); System.out.println("First day of the week is:" + cal.getFirstDayOfWeek()); int day = cal.getFirstDayOfWeek(); switch (day) { case (1): System.out.println("Sunday"); break; case (2): System.out.println("Monday"); break; case 3: System.out.println("Tuesday"); break; case 4: System.out.println("Wednesday"); break; case 5: System.out.println("Thrusday"); break; case 6: System.out.println("Friday"); break; case 7: System.out.println("Saturday"); break; } } }
Output:
First day of the week is: 1
Sunday
In the same way, you can try all the other built-in methods in the calendar class to obtain the desired results.
Conclusion:
Conversion of date and time has become easier with the help of the calendar class. It also contains numerous other functionalities as listed in the table. This is all about the calendar class in java. I hope this article helps you.