Write a Java Program to Get the current year using the get() method of Calendar class


This Java program retrieves and prints the current year using the Calendar class. The getInstance() method of the Calendar class is called to obtain a Calendar object that represents the current date and time. The get() method is then called with the Calendar.YEAR constant to retrieve the year. Here's an explanation of the code:

  • import java.util.*;: This imports the java.util package, which contains the Calendar class and other utility classes.
  • public class CurrentYear_GetMethod: This defines a public class named CurrentYear_GetMethod.
  • public static void main(String[] args) : This is the entry point of the program. It declares a public static method named main that takes an array of strings as an argument.
  • Calendar cal = Calendar.getInstance();: This creates a new Calendar object representing the current date and time.
  • System.out.println("Current Year : " + cal.get(Calendar.YEAR)); : This prints the string "Current Year : " followed by the value returned by the get() method of the Calendar object when called with the Calendar.YEAR constant. This retrieves the year, which is then printed to the console.

Source Code

import java.util.*;
public class CurrentYear_GetMethod
{
	public static void main(String[] args)
	{
		Calendar cal = Calendar.getInstance();
		System.out.println("Current Year : " + cal.get(Calendar.YEAR));
	}
}

Output

Current Year : 2022

Example Programs