Calendar Class in Java

Introduction to Calendar Class in Java

The Calendar class in Java is an abstract class that provides methods for converting between a specific instant in time and a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and so on. It also provides methods for manipulating the calendar fields, such as getting the date of the next week.

The Calendar class is a powerful tool for working with date and time in Java. It can be used to solve a variety of problems, such as:

  • Calculating the difference between two dates
  • Finding the next or previous occurrence of a date
  • Formatting dates for display
  • Validating dates

To use the Calendar class, you first need to create a new instance of the class. You can do this using the static getInstance() method. This method will return a Calendar object based on the current time in the default time zone with the default locale.

Once you have a Calendar object, you can use the various methods provided by the class to access and manipulate the calendar fields. For example, you can use the get() method to get the value of a particular calendar field, such as the year or month. You can also use the set() method to set the value of a calendar field.

The Calendar class also provides a number of methods for performing various operations on dates, such as adding or subtracting days, months, or years. You can also use the Calendar class to find the next or previous occurrence of a date, such as the next Friday the 13th.

The Calendar class is a very versatile and powerful tool for working with date and time in Java. If you need to do anything with dates, you should definitely learn how to use the Calendar class.

Here are some examples of how to use the Calendar class:

  // Create a new Calendar object based on the current time in the default time zone with the default locale.
Calendar calendar = Calendar.getInstance();

// Get the current year.
int year = calendar.get(Calendar.YEAR);

// Get the current month.
int month = calendar.get(Calendar.MONTH);

// Get the current day of the month.
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);

// Set the year to 2024.
calendar.set(Calendar.YEAR, 2024);

// Add 10 days to the current date.
calendar.add(Calendar.DAY_OF_MONTH, 10);

// Find the next Friday the 13th.
Date nextFridayThe13th = calendar.getTime();  

Exercises

Code for creating a calendar instance in Java


  package javalabdemo;
  import java.util.Calendar;
  public class javalabclass 
  {
    public static void main(String args[])
      {
      Calendar c = Calendar.getInstance();
      System.out.println(c.getTime());
    }
  } 

Code for setting a Calendar instance in Java


  package javalabdemo;
  import java.util.Calendar;
  
  public class javalabclass 
  {
    public static void main(String args[])
      {
      Calendar c = Calendar.getInstance();
      c.set(2020, 12, 23);
      System.out.println(c.getTime());
    }
  }   

Code for setting the date and time for a calendar instance in Java


  package javalabdemo;
  import java.util.Calendar;
  
  public class javalabclass 
  {
    public static void main(String args[])
      {
      Calendar c = Calendar.getInstance();
      c.set(2020, 12, 23,14,30);
      System.out.println(c.getTime());
    }
  } 

Code for setting date and time along with seconds in Java


  package javalabdemo;
  import java.util.Calendar;
  
  public class javalabclass 
  {
    public static void main(String args[])
      {
      Calendar c = Calendar.getInstance();
      c.set(2020, 12, 23,14,30,56);
      System.out.println(c.getTime());
    }
  } 

Code for returning a string representation of a calendar instance in java


  package javalabdemo;
  import java.util.Calendar;
  
  public class javalabclass 
  {
    public static void main(String args[])
      {
      Calendar c = Calendar.getInstance();
      System.out.println(c.toString());
      
      }
  } 

Code for getting the time zone from a calendar instance in java


  package javalabdemo;
  import java.util.Calendar;
  
  public class javalabclass 
  {
    public static void main(String args[])
      {
      Calendar c = Calendar.getInstance();
      System.out.println(c.getTimeZone());
      
  }
  } 

Code for adding 4 months to the current date using the calendar instance in java


  package javalabdemo;
  import java.util.Calendar;
  
  public class javalabclass 
  {
    public static void main(String args[])
      {
      Calendar c = Calendar.getInstance();
      c.add(Calendar.MONTH, 4);
      System.out.println(c.getTime());
  }
  } 

Code for adding 3 days to the current date using the calendar instance in java


  package javalabdemo;
  import java.util.Calendar;
  
  public class javalabclass 
  {
    public static void main(String args[])
      {
      Calendar c = Calendar.getInstance();
      c.add(Calendar.DATE, 3);
      System.out.println(c.getTime());
  }
  } 

Code for adding years to the current date using the calendar instance in java


  package javalabdemo;
  import java.util.Calendar;
  
  public class javalabclass 
  {
    public static void main(String args[])
      {
      Calendar c = Calendar.getInstance();
      c.add(Calendar.YEAR, 3);
      System.out.println(c.getTime());
  }
  } 

Code for determining the difference between two dates using the Calendar instance. The difference should be displayed as number of days.


  package javalabdemo;
  import java.util.Calendar;
  import java.util.Scanner;
  public class javalabclass 
  {
    public static void main(String args[])
      {
      Calendar d1 = Calendar.getInstance();
      d1.set(2022, 8, 2);
      Calendar d2 = Calendar.getInstance();
      d2.set(2022,8,5);
      System.out.println((d2.getTimeInMillis()-d1.getTimeInMillis())/(24*60*60*1000));
      }
  } 

Write a program to calculate the age from date of birth.


package javalabdemo;
import java.util.Calendar;
import java.util.Scanner;

public class javalabclass 
{
  public static void main(String args[])
    {
    //program for calculating age from Date of Birth
    Calendar dob = Calendar.getInstance();
    System.out.println("Enter the Year");
    Scanner input = new Scanner(System.in);
    int year = input.nextInt();
    System.out.println("Enter the Month");
    int month = input.nextInt();
    System.out.println("Enter the Day");
    int day = input.nextInt();
    dob.set(year, month, day);
    Calendar current = Calendar.getInstance();
    int age = current.get(Calendar.YEAR)-dob.get(Calendar.YEAR);
    if((dob.get(Calendar.MONTH)>current.get(Calendar.MONTH) || dob.get(Calendar.MONTH) == current.get(Calendar.MONTH)) && dob.get(Calendar.DATE)>current.get(Calendar.DATE))
    {
      age--;
    }
    System.out.println("Your age is " +age+ " Years");

}
}