Java GregorianCalendar - isLeapYear() Method
The java.util.GregorianCalendar.isLeapYear() method is used to determine if the given year is a leap year. The method returns true if the given year is a leap year. To specify BC year numbers, 1 - year number must be given. For example, year BC 4 is specified as -3.
Syntax
public boolean isLeapYear(int year)
Parameters
year |
Specify the given year. |
Return Value
Returns true if the given year is a leap year; false otherwise.
Exception
NA
Example:
In the example below, the java.util.GregorianCalendar.isLeapYear() method is used to check if the given year is a leap year or not.
import java.util.*; public class MyClass { public static void main(String[] args) { //creating a Calendar object with specified date GregorianCalendar Cal = new GregorianCalendar(2015, 10, 25); //printing the Calendar System.out.println("The Calendar is: " + Cal.getTime()); //checking if it is a leap year boolean isLeapYear = Cal.isLeapYear(Cal.get(GregorianCalendar.YEAR)); System.out.println("Is this a leap year: " + isLeapYear); //checking if 2016 is a leap year isLeapYear = Cal.isLeapYear(2016); System.out.println("Is 2016 a leap year: " + isLeapYear); } }
The output of the above code will be:
The Calendar is: Wed Nov 25 00:00:00 UTC 2015 Is this a leap year: false Is 2016 a leap year: true
❮ Java.util - GregorianCalendar