HomeLanguagesJavaGregorianCalendar isLeapYear() Method in Java

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:

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

RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32548 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6922 POSTS0 COMMENTS
Nicole Veronica
12039 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12144 POSTS0 COMMENTS
Shaida Kate Naidoo
7059 POSTS0 COMMENTS
Ted Musemwa
7300 POSTS0 COMMENTS
Thapelo Manthata
7016 POSTS0 COMMENTS
Umr Jansen
7005 POSTS0 COMMENTS