GregorianCalendar isLeapYear() Method in Java

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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");
      }
   }
}

chevron_right


Output:

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

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 calender
      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");
      }
   }
}

chevron_right


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()



My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.