Java Program to Find if a Given Year is a Leap Year

Last Updated : 17 Mar, 2026

Leap Year contains 366 days, which comes once every four years. In this article, we will learn how to write the leap year program in Java.

  • A leap year has 366 days and occurs roughly every 4 years.
  • A century year (ending with 00) is a leap year only if it is divisible by 400.
  • A non-century year is a leap year if it is divisible by 4 but not divisible by 100.
  • Using these rules, any year can be checked programmatically to determine whether it is a leap year.
Java
import java.io.*;

public class GeeksforGeeks {
    // Method to check leap year
    public static void isLeapYear(int year)
    {
        // flag to take a non-leap year by default
        boolean is_leap_year = false;

        // If year is divisible by 4
        if (year % 4 == 0) {
            is_leap_year = true;

            // To identify whether it is a
            // century year or not
            if (year % 100 == 0) {
                // Checking if year is divisible by 400
                // therefore century leap year
                if (year % 400 == 0)
                    is_leap_year = true;
                else
                    is_leap_year = false;
            }
        }

        // We land here when corresponding if fails
        // If year is not divisible by 4
        else

            // Flag dealing-  Non leap-year
            is_leap_year = false;

        if (!is_leap_year)
            System.out.println(year + " : Non Leap-year");
        else
            System.out.println(year + " : Leap-year");
    }

    // Driver Code
    public static void main(String[] args)
    {
        // Calling our function by
        // passing century year not divisible by 400
        isLeapYear(2000);

        // Calling our function by
        // passing Non-century year
        isLeapYear(2002);
    }
}

Output
2000 : Leap-year
2002 : Non Leap-year

Explaination of the above Program:

Leap-Year-in-Java

As we check that 2000 is a century year divisible by 100 and 4. 2002 is not divisible by 4, therefore not a leap year. Using these facts we can check any year if it is leap or not.

More Methods to Check Leap Year in Java

There are certain methods for leap year program.

1. Using Scanner Class

Here the user is provided the flexibility to enter the year of their own choice as Scanner Class is imported here rest of the if-else blocks are also combined in a single statement to check if the input year is a leap year.

Java
import java.io.*;

import java.util.Scanner;

public class GFG {

    // Driver code
    public static void main(String[] args)
    {
        // Considering any random year
        int year;

        // scn is an object made of Scanner Class
        Scanner scn = new Scanner(System.in);
        year = scn.nextInt();

        // 1st condition check- It is century leap year
        // 2nd condition check- It is leap year and not
        // century year
        if ((year % 400 == 0)
            || ((year % 4 == 0) && (year % 100 != 0))) {

            // Both conditions true- Print leap year
            System.out.println(year + " : Leap Year");
        }

        else {
            // Any condition fails- Print Non-leap year
            System.out.println(year + " : Non - Leap Year");
        }
    }
}

 
Input

2012

Output

2012 : Leap Year

2. Using Ternary Operator

Ternary Operator is used to reduce the if-else statements. The ternary operator takes three operands, a boolean condition, an expression to execute if the condition is true, and an expression to execute if false.

Java
import java.io.*;

public class GeeksforGeeks {

    public static void isLeapYear(int year)
    {
        // flag to take a non-leap year by default
        boolean is_leap_year = false;

        is_leap_year = (year % 4 == 0 && year % 100 != 0
                        || year % 400 == 0)
                           ? true
                           : false;
        if (!is_leap_year)
            System.out.println(year + " : Non Leap-year");
        else
            System.out.println(year + " : Leap-year");
    }

    // Driver Code
    public static void main(String[] args)
    {
        // Calling our function by
        // passing century year not divisible by 400
        isLeapYear(2000);

        // Calling our function by
        // passing Non-century year
        isLeapYear(2002);
    }
}

Output
2000 : Leap-year
2002 : Non Leap-year

3. Using In-built isLeap() Method

Java has an in-built isLeap() method to check if the input year is a leap year or not.

Java
import java.io.*;
import java.time.*;
import java.util.*;

public class GeeksforGeeks {

    public static void isLeapYear(int year)
    {
        // flag to take a non-leap year by default
        boolean is_leap_year = false;

        Year checkyear = Year.of(year);

        is_leap_year = checkyear.isLeap();

        if (!is_leap_year)
            System.out.println(year + " : Non Leap-year");
        else
            System.out.println(year + " : Leap-year");
    }

    // Driver Code
    public static void main(String[] args)
    {
        // Calling our function by
        // passing century year not divisible by 400
        isLeapYear(2000);

        // Calling our function by
        // passing Non-century year
        isLeapYear(2002);
    }
}
Try It Yourself
redirect icon

Output
2000 : Leap-year
2002 : Non Leap-year
Comment