Open In App

GregorianCalendar isLeapYear() Method in Java

Improve
Improve
Like Article
Like
Save
Share
Report

The java.util.GregorianCalendar.isLeapYear() method determines if the given year passed as a parameter to the function is a leap year or not and returns true if the given year is a leap year and false otherwise.

Syntax:

public boolean isLeapYear(int year)

Parameters: This function accepts a single integer parameter year that represents the year which the function needs to check for whether it is a leap year or not.

Return Values: The function returns a boolean value. If the year passed as a parameter is a leap year, it returns true and false otherwise.

Examples:

Input : 2016
Output : true

Input : 2018
Output : false

Below program illustrate the java.util.GregorianCalendar.isLeapYear() function in Java :

Program 1:

Java




// Java Program to illustrate isLeapYear() function 
// of GregorianCalendar
  
import java.io.*;
import java.util.*;
  
class GFG {
     public static void main(String[] args) {
      
      // Create a new calendar
      GregorianCalendar c = (GregorianCalendar) 
                 GregorianCalendar.getInstance();
  
      // Display the current date and time
      System.out.println("Current Date and Time : "
                                 + c.getTime());
  
      int year = c.get(GregorianCalendar.YEAR);
      if(c.isLeapYear(year))
      {
           System.out.println(year + 
                          " is leap year");
      }
      else
      {
          System.out.println(year + 
                     " is Not a leap year");
      }
   }
}


Output:

Current Date and Time : Fri Jul 27 11:53:39 UTC 2018
2018 is Not a leap year

Program 2:

Java




// Java Program to illustrate isLeapYear() function 
// of GregorianCalendar
  
import java.io.*;
import java.util.*;
  
class GFG {
     public static void main(String[] args) {
      
      // Create a new calendar
      GregorianCalendar c = (GregorianCalendar) 
                GregorianCalendar.getInstance();
  
      // Display the current date and time
      System.out.println("" + c.getTime());
        
      // Modifying the current calendar
      c.add((GregorianCalendar.MONTH), -30);
  
      int year = c.get(GregorianCalendar.YEAR);
      if(c.isLeapYear(year))
      {
           System.out.println(year + " is leap year");
      }
      else
      {
          System.out.println(year + " is Not a leap year");
      }
   }
}


Output:

Fri Jul 27 11:53:41 UTC 2018
2016 is leap year

Reference : https://docs.oracle.com/javase/7/docs/api/java/util/GregorianCalendar.html#isLeapYear()



Last Updated : 17 Dec, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads