The Wayback Machine - https://web.archive.org/web/20240601094851/https://www.geeksforgeeks.org/calendar-class-in-java-with-examples/
Open In App

Calendar Class in Java with examples

Last Updated : 28 Aug, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

Calendar class in Java is an abstract class that provides methods for converting date between a specific instant in time and a set of calendar fields such as MONTH, YEAR, HOUR, etc. It inherits Object class and implements the Comparable, Serializable, Cloneable interfaces.

As it is an Abstract class, so we cannot use a constructor to create an instance. Instead, we will have to use the static method Calendar.getInstance() to instantiate and implement a sub-class.

  • Calendar.getInstance(): return a 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)

Java program to demonstrate getInstance() method:




// Date getTime(): It is used to return a
// Date object representing this
// Calendar's time value.
  
import java.util.*;
public class Calendar1 {
    public static void main(String args[])
    {
        Calendar c = Calendar.getInstance();
        System.out.println("The Current Date is:" + c.getTime());
    }
}


Output:

The Current Date is:Tue Aug 28 11:10:40 UTC 2018

Important Methods and their usage







METHOD DESCRIPTION
abstract void add(int field, int amount) It is used to add or subtract the specified amount of time to the given calendar field, based on the calendar’s rules.
int get(int field) It is used to return the value of the given calendar field.
abstract int getMaximum(int field) It is used to return the maximum value for the given calendar field of this Calendar instance.
abstract int getMinimum(int field) It is used to return the minimum value for the given calendar field of this Calendar instance.
Date getTime() It is used to return a Date object representing this Calendar’s time value.</td

Below programs illustrate the above methods:

Program 1: Java program to demonstrate get() method.




// Program to demonstrate get() method
// of Calendar class
  
import java.util.*;
public class Calendar2 {
    public static void main(String[] args)
    {
        // creating Calendar object
        Calendar calendar = Calendar.getInstance();
          
        // Demonstrate Calendar's get()method
        System.out.println("Current Calendar's Year: " + calendar.get(Calendar.YEAR));
        System.out.println("Current Calendar's Day: " + calendar.get(Calendar.DATE));
        System.out.println("Current MINUTE: " + calendar.get(Calendar.MINUTE));
        System.out.println("Current SECOND: " + calendar.get(Calendar.SECOND));
    }
}


Output:

Current Calendar's Year: 2018
Current Calendar's Day: 28
Current MINUTE: 10
Current SECOND: 45

Program 2: Java program to demonstrate getMaximum() method.




// Program to demonstrate getMaximum() method
// of Calendar class
  
import java.util.*;
public class Calendar3 {
    public static void main(String[] args)
    {
        // creating calendar object
        Calendar calendar = Calendar.getInstance();
      
        int max = calendar.getMaximum(Calendar.DAY_OF_WEEK);
        System.out.println("Maximum number of days in a week: " + max);
          
        max = calendar.getMaximum(Calendar.WEEK_OF_YEAR);
        System.out.println("Maximum number of weeks in a year: " + max);
    }
}


Output:

Maximum number of days in a week: 7
Maximum number of weeks in a year: 53

Program 3: Java program to demonstrate the getMinimum() method.




// Program to demonstrate getMinimum() method
// of Calendar class
  
import java.util.*;
public class Calendar4 {
    public static void main(String[] args)
    {
        // creating calendar object
        Calendar calendar = Calendar.getInstance();
          
        int min = calendar.getMinimum(Calendar.DAY_OF_WEEK);
        System.out.println("Minimum number of days in week: " + min);
          
        min = calendar.getMinimum(Calendar.WEEK_OF_YEAR);
        System.out.println("Minimum number of weeks in year: " + min);
    }
}


Output:

Minimum number of days in week: 1
Minimum number of weeks in year: 1

Program 4: Java program to demonstrate add() method.




// Program to demonstrate add() method
// of Calendar class
  
import java.util.*;
public class Calendar5 {
    public static void main(String[] args)
    {
        // creating calendar object
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -15);
        System.out.println("15 days ago: " + calendar.getTime());
        calendar.add(Calendar.MONTH, 4);
        System.out.println("4 months later: " + calendar.getTime());
        calendar.add(Calendar.YEAR, 2);
        System.out.println("2 years later: " + calendar.getTime());
    }
}


Output:

15 days ago: Mon Aug 13 11:10:57 UTC 2018
4 months later: Thu Dec 13 11:10:57 UTC 2018
2 years later: Sun Dec 13 11:10:57 UTC 2020

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html



Similar Reads

Java Program to Generate Calendar of Any Year Without calendar.get() Function
Java program for generating the calendar of any desired year and month let us first go through an illustration before landing upon logic and procedural part. Illustration: Say the user wants to get the calendar of April 2011. Then, he is required to enter the year along with the month as integers and the output would return the desired month's cale
4 min read
Calendar clone() Method in Java with Examples
The clear() method in Calendar class is used to clone a calendar object. It basically creates a shallow copy of this object. Syntax: public Object clone() Parameters: The method does not take any parameters. Return Value: The method does not return any value. Below programs illustrate the working of clone() Method of Calendar class: Example 1: // J
2 min read
Calendar clear() Method in Java with Examples
The clear() method in Calendar class is used to set all the calendar field values and the time value of this Calendar undefined. Note: Implementation of calendar class may use default field values for date and time calculations. Syntax: public final void clear() Parameters: The method does not take any parameters. Return Value: The method does not
2 min read
Calendar add() Method in Java with Examples
The add() method of Calendar class present inside is used to add or subtract from the given calendar field(int field), a specific amount of time(int amt), based on the calendar's rules. Syntax: public abstract void add(int field, int amt) Parameters: The method takes two parameters: The field of the calendar on which the operation is to be performe
3 min read
Calendar toString() Method in Java with Examples
The toString() method in Calendar class is used to get the string representation of the Calendar object. This method in Calendar Class is just for debug process and not to be used as an operation. Syntax: public String toString() Parameters: The method does not take any parameters. Return Value: The method returns the String representation of the C
1 min read
Calendar compareTo() Method in Java with Examples
The add(Calendar Calendar2) method of Calendar class is used to compare the time values or the millisecond offsets of this Calendar object with the passed Calendar object. Syntax: public int compareTo(Calendar Calendar2) Parameters: The method takes one parameter Calendar2 of Calendar object type and refers to the object to be compared to this Cale
2 min read
Calendar complete() Method in Java with Examples
The complete() method in Calendar class is used to fill any unset fields of the calendar fields. It follows the following process of completion: At first, if the time value has not been calculated, the computeTime() method is called to calculate it from calendar field values. Second, to calculate all the calendar field values the computeFields() me
2 min read
Calendar computeTime() method in Java with Examples
The computeTime() method in Calendar class is used for the conversion operation of the current calendar field values in fields[] to the millisecond time value time. Syntax: protected abstract void computeTime() Parameters: The method does not take any parameters. Return Value: The method does not return any value. Below programs illustrate the work
2 min read
Calendar computeFields() method in Java with Examples
The computeFields() method in Calendar class is used for the conversion operation of the current millisecond time value time to calendar field values mentioned in the fields[]. Note: This allows syncing up the new time that is set for the calendar object with the calendar field values. Syntax: protected abstract void computeFields() Parameters: The
2 min read
Calendar get() method in Java with Examples
The get(int field_value) method of Calendar class is used to return the value of the given calendar field in the parameter. Syntax: public int get(int field) Parameters: The method takes one parameter field_value of integer type and refers to the calendar whose value is needed to be returned. Return Value: The method returns the value of the passed
2 min read
Article Tags :
Practice Tags :