Write a Java program to get the information of current/given year


This Java program demonstrates how to get information about the current year using the java.time package:

  • import java.time.* - import the necessary classes from the java.time package.
  • Year y = Year.now(); - create a Year object using the now() method which returns the current year.
  • System.out.println("Current Year : " + y); - print the current year using the toString() method of the Year class.
  • boolean ly = y.isLeap(); - check if the current year is a leap year using the isLeap() method of the Year class which returns a boolean value.
  • System.out.println("Is Current Year or leap Year ? " + ly); - print the result of the previous step.
  • int nod = y.length(); - get the number of days in the current year using the length() method of the Year class.
  • System.out.println("Number of Days in the year : " +nod+" Days"); - print the result of the previous step.

Source Code

import java.time.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
class Get_Information_CurrentYear
{
	public static void main(String[] args)
	{
		Year y = Year.now();
		System.out.println("Current Year : " + y);  
		boolean ly = y.isLeap();
		System.out.println("Is Current Year or leap Year ? " + ly);  
		int nod = y.length();
		System.out.println("Number of Days in the year : " +nod+" Days"); 
	}
}

Output

Current Year : 2022
Is Current Year or leap Year ? false
Number of Days in the year : 365 Days

Example Programs