Java Calendar Class Example | Calendar Class in Java
Java calendar class provides methods for converting dates between a specific instant in time and set of calendar fields such as MONTH, YEAR, HOUR, etc. The Calendar class is an abstract class. This class extends Object Class and implements Serializable, Cloneable, and Comparable interfaces. See the following figure.
Methods of Calendar class provide us with converting between a specific instance of time and a set of Fields such as MONTH, YEAR, HOUR, etc.
As it is an Abstract class, we cannot create its instance using the constructor. To initiate and create its subclass, we have Calender.getInstance() static method.
Java Calendar Class Tutorial
As it is the Abstract class, so we cannot use the constructor to create an instance. Instead, we will have to use a static method Calendar.getInstance() to instantiate and implement a sub-class.
- Calendar.getInstance(): returns the Calendar instance based on the current time in the default time zone with the default locale.
- Calendar.getInstance(TimeZone zone)
- Calendar.getInstance(Locale aLocale)
- Calendar.getInstance(TimeZone zone, Locale aLocale)
Let’s see the declaration of java.util.Calendar class.
public abstract class Calendar extends Object implements Serializable, Cloneable, Comparable<Calendar>
Methods of Calendar Class
#public void add (int field, int amount)
This method helps to add the amount of time-based to specified field according to the calendar rule. This method does not return any value.
// Add.java import java.util.Calendar; public class Add { public static void main(String[] args) { // creating a new calendar Calendar c =Calendar.getInstance(); // displaying the current date System.out.println("Current date and time: " + c.getTime()); // adding 10 years to the current date c.add((Calendar.YEAR), 10); // displaying the date after modification System.out.println("Date and time after 10 years: " + c.getTime()); } }
See the output.
#public boolean after (Object when)
The above method returns true if the method the time of the calendar is after the time represented by the when object if not false is returned.
// After.java import java.util.*; public class After { public static void main(String[] args) { // creating calendar objects. Calendar c =Calendar.getInstance(); Calendar future = Calendar.getInstance(); // displaying the current date System.out.println("Current date n time:" + c.getTime()); // change year in future calendar future.set(Calendar.YEAR, 2016); System.out.println("Year is " + future.get(Calendar.YEAR)); // check weather calendar date is after current date Date time = future.getTime(); if (!future.after(c)) { System.out.println("Date " + time + " is before current date."); } } }
See the output.
#public boolean before (Object when)
The above method returns true if the method the time of the calendar is before the time represented by the when object if not false is returned.
// Before.java import java.util.Calendar; import java.util.Date; public class Before { public static void main(String[] args) { // creating calendar objects. Calendar c = Calendar.getInstance(); Calendar past = Calendar.getInstance(); // print the current date System.out.println("Current date: " + c.getTime()); // changing the year in future calendar past.set(Calendar.YEAR, 2017); System.out.println("Year is " + past.get(Calendar.YEAR)); // check weather calendar date is after current date Date time = past.getTime(); if (past.before(c)) { System.out.println("Date " + time + " is before current date."); } } }
See the following output.
#public final void clear (int field)
The above method sets the given calendar field value and the time value of this calendar undefined.
// Clear.java import java.util.Calendar; public class Clear { public static void main(String[] args) { // create a calendar Calendar cal = (Calendar) Calendar.getInstance(); // display the current date and time System.out.println("Current Calendar Date: " + cal.getTime()); // use clear method to set year as undefined. cal.clear(Calendar.YEAR); // print the result System.out.println("The calendar shows : " + cal.getTime()); } }
See the output.
public Object clone()
The above method is used to create a copy of the current object.
// Clone.java import java.util.Calendar; public class Clone { public static void main(String[] args) { // creating new calendar object Calendar c =(Calendar) Calendar.getInstance(); // displaying date for default value System.out.println("Actual Calendar Object is : " + c.getTime()); // creating a clone of first calendar object Calendar c1 = (Calendar)c.clone(); // displaying the copy System.out.println("Cloned calendar object is : " + c1.getTime()); } }
See the output.
#public String getCalendarType()
Returns all available calendar types supported by Java run time environment in String.
// CalendarType.java import java.util.Calendar; public class CalendarType { public static void main(String[] args) { // creating a calendar class object Calendar c = Calendar.getInstance(); // displaying the calendar type System.out.println("Type of Calendar is " +c.getCalendarType()); } }
See the output.
#public int get(int field)
In this method fields of the calendar are passed as a parameter and return the value of the fields.
// Get.java import java.util.Calendar; public class Get { public static void main(String[] args) { // creating a calendar Calendar c = Calendar.getInstance(); // DATE field is passed as parameter to get method System.out.println("Day of month is: " +c.get(Calendar.DATE)); // MONTH field DATE field is passed as parameter to get method System.out.println("Month of year is: " +c.get(Calendar.MONTH)); // YEAR field DATE field is passed as parameter to get method System.out.println("and year is : " +c.get(Calendar.YEAR)); System.out.println("Today is "+c.get(Calendar.DATE) + " Day of " + c.get(Calendar.MONTH)+ " Month of the year " + c.get(Calendar.YEAR) ); } }
See the following example.
#public static Calendar getInstance()
The above method is used with the calendar object to get the instance of the calendar according to the current time zone using by the java runtime environment.
// GetInstance.java import java.util.Calendar; public class GetInstance { public static void main(String args[]) { Calendar c1 = Calendar.getInstance(); Calendar c2 = Calendar.getInstance(); c2.set(1996, 9 , 23); System.out.println("Calendar 1 :" + c1.getTime()); System.out.println("Calendar 2 :" + c2.getTime()); if(c1.equals(c2)) { System.out.println("Both calendar are equal"); } else { System.out.println("Both calendar are not equal"); } } }
See the output.
Finally, Java Calendar Class Example is over.