Related Articles

Related Articles

GregorianCalendar isLeapYear() Method in Java
  • Last Updated : 27 Jul, 2018

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

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :