Java Program to Check a Leap Year

In this tutorial, we will learn how to write a Java program to check whether a given year is a leap year or not. We will explore different ways to check a leap year in Java with examples.

Before writing the Java program, we will first understand what a leap year is and the rules for identifying one.

What Is a Leap Year?

A leap year is a year that contains 366 days instead of the usual 365 days. An extra day, February 29, is added to the month of February during a leap year.

Earth takes about 365.24 days to complete one full orbit around the Sun. Because of that extra quarter of a day, our calendar slowly drifts out of sync with the seasons. A leap year fixes this drift.

For example:

  • 2024 was a leap year because it is divisible by 4 but not by 100.
  • 2025 was not a leap year because it is not divisible by 4.
  • 2000 was a leap year because it is divisible by 400.
  • 1900 was not a leap year because it is divisible by 100 but not by 400.

Rules for Checking a Leap Year

To check whether a year is a leap year, we follow these rules:

  • If a year is divisible by 400, it is a leap year.
  • If a year is divisible by 4 but not by 100, it is a leap year.
  • If a year is divisible by 100 but not by 400, it is not a leap year.
  • If a year is not divisible by 4, it is not a leap year.

We can combine these rules into the following Java condition:

(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)

Here, the modulus operator (%) returns the remainder after division.

  • The first condition, year % 4 == 0, checks whether the year is divisible by 4.
  • The second condition, year % 100 != 0, checks whether the year is not divisible by 100.
  • The third condition, year % 400 == 0, checks whether the year is divisible by 400.
  • The && is a logical AND operator, while || is a logical OR operator.
  • If the complete condition evaluates to true, the year is a leap year. Otherwise, it is not a leap year.

Java Program to Check a Leap Year Using if-else

Let us first write a simple Java program in which we assign a year to a variable directly and check whether it is a leap year using the if-else statement.

Complete Java Program

// Java program to check whether a given year is a leap year
public class LeapYear {
    public static void main(String[] args) {
        
        // Declare and initialize the year variable
        int year = 2024;

        // Check the leap year conditions:
        // 1. Year must be divisible by 4 AND not divisible by 100
        // OR 
        // 2. Year must be divisible by 400
        if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
            System.out.println(year + " is a leap year.");
        } else {
            System.out.println(year + " is not a leap year.");
        }
    }
}

Output:

2024 is a leap year.

In this example program:

  • We declare an integer variable named year and assign it the value 2024.
  • The if statement checks whether the year is divisible by 4 but not by 100, or whether it is divisible by 400.
  • If the condition is true, the program displays that the year is a leap year.
  • If the condition is false, the else block displays that the year is not a leap year.

In this example, 2024 is divisible by 4 but not by 100. Therefore, the condition evaluates to true, and the program prints 2024 is a leap year. This example uses a fixed year. To check any year entered by the user, we can use the Scanner class.

Java Program to Check a Leap Year Using Scanner

In the previous example, we assigned a year directly to the variable. However, in practical programs, we often need to accept input from the user.

Java provides the Scanner class in the java.util package to read input from the keyboard. Let’s write a Java program to check whether a given year is a leap year.

Complete Java Program

// Java program to check a leap year using Scanner
// Import the Scanner class
import java.util.Scanner; 
public class LeapYearScanner {
    public static void main(String[] args) {
        // Create a Scanner object to read input from the keyboard
        Scanner scanner = new Scanner(System.in);

        // Prompt the user to enter a year
        System.out.print("Enter a year: ");
        int year = scanner.nextInt();

        // Close the scanner to prevent memory leaks
        scanner.close();

        // Check whether the year is a leap year
        if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
            System.out.println(year + " is a leap year.");
        } else {
            System.out.println(year + " is not a leap year.");
        }
    }
}

Output:

Enter a year: 2025
2025 is not a leap year.

In this example program:

  • import java.util.Scanner; — Imports the Scanner class from the java.util package.
  • Scanner sc = new Scanner(System.in); — Creates a Scanner object to read input from the keyboard (System.in).
  • sc.nextInt() — Reads the integer value entered by the user and stores it in the year variable.
  • if-else statement — Evaluates the standard Gregorian leap year condition.
  • System.out.println(...) — Prints the final result to the console.
  • sc.close() — Closes the Scanner object to free up system resources.

This program accepts an integer year from the user and checks it using the standard Gregorian leap year rules.

Java Program to Check a Leap Year Using Nested if-else

We can also check a leap year using nested if-else statements. In this approach, we examine each condition separately instead of combining them into a single Boolean expression. So, let’s write a Java program to verify whether a given year is a leap year using nested if-else statements.

Complete Java Program

// Java program to check a leap year using nested if-else statements
// Import the Scanner class for user input
import java.util.Scanner; 
public class LeapYearNestedIf {
    public static void main(String[] args) {
        
        // Create a Scanner object to take input from the keyboard
        Scanner sc = new Scanner(System.in);

        // Prompt the user to enter a year
        System.out.print("Enter a year: ");
        int year = sc.nextInt();

        // Step 1: Check if the year is divisible by 4
        if (year % 4 == 0) {
            
            // Step 2: Check if it is a century year (divisible by 100)
            if (year % 100 == 0) {
                
                // Step 3: If it's a century year, check if it is divisible by 400
                if (year % 400 == 0) {
                    System.out.println(year + " is a leap year.");
                } else {
                    System.out.println(year + " is not a leap year.");
                }
                
            } else {
                // If it is divisible by 4 but not a century year, it is a leap year
                System.out.println(year + " is a leap year.");
            }
            
        } else {
            // If the year is not divisible by 4, it is not a leap year
            System.out.println(year + " is not a leap year.");
        }

        // Close the scanner to prevent memory leaks
        sc.close();
    }
}

Output:

Enter a year: 2028
2028 is a leap year.

In this example program, the program checks the conditions in the following order:

  • First, it checks whether the year is divisible by 4.
  • If it is divisible by 4, the program checks whether it is divisible by 100.
  • If it is divisible by 100, the program checks whether it is also divisible by 400.
  • A year divisible by 400 is a leap year. A century year not divisible by 400 is not a leap year.
  • A year divisible by 4 but not by 100 is a leap year.

Java Program to Check a Leap Year Using the Ternary Operator

Java also provides the conditional or ternary operator (?:), which can be used as a concise alternative to an if-else statement. Let us write a Java program to verify if a given year is a leap year using the conditional operator.

Complete Java Program

// Java program to check a leap year using the ternary operator
public class LeapYearTernary {
    public static void main(String[] args) {
        
        // Declare and initialize the year variable
        int year = 2023;

        // Use the ternary operator (? :) as a shorthand for an if-else statement:
        // Syntax: condition ? expressionIfTrue : expressionIfFalse
        String result =
            ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
            ? "is a leap year."       // Assigned if the condition is true
            : "is not a leap year.";  // Assigned if the condition is false

        // Print the final result
        System.out.println(year + " " + result);
    }
}

Output:

2023 is not a leap year.

In this example program, the ternary operator evaluates the leap year condition.

  • If the condition is true, the variable result receives the message is a leap year.
  • If the condition is false, the variable result receives the message is not a leap year.

Finally, the program displays the year and the corresponding result.

For beginners, the if-else approach is generally easier to understand. The ternary operator is useful when you want to express a simple conditional choice in fewer lines.

Java Program to Check a Leap Year Using the Built-in Method

Java 8 introduced java.time.Year class found in the java.time package, which includes the static Year.isLeap(long year) method. This method automatically checks if a year is a leap year for us according to the ISO calendar rules. Therefore, you don’t have to write the mathematical logic ourselves.

Let’s write a Java program to check whether a given year is a leap year using the built-in method.

Complete Java Program

// Java program to check whether a year is a leap year using the built-in Year.isLeap() method.
// Import the Year class from the java.time package
import java.time.Year; 
public class LeapYearBuiltInMethod {
    public static void main(String[] args) {
       
        // Declare and initialize the year variable
        int year = 2024;

        // Use the built-in Year.isLeap() method to check if the year is a leap year
        if (Year.isLeap(year)) {
            System.out.println(year + " is a leap year.");
        } else {
            System.out.println(year + " is not a leap year.");
        }
    }
}

Output:

2024 is a leap year.

In this example program:

  • The statement import java.time.Year; imports the Year class.
  • The Year.isLeap(year) method checks whether the specified year is a leap year and returns a Boolean value.
  • The if-else statement uses the returned value to display the result.

This method is useful when you want to use Java’s standard date and time API instead of writing the leap year condition yourself.

For basic programming exercises and coding interviews, understanding the manual condition is important even when a built-in method is available.

DEEPAK GUPTA

DEEPAK GUPTA

Deepak Gupta is the Founder of Scientech Easy, a Full Stack Developer, and a passionate coding educator with 8+ years of professional experience in Java, Python, web development, and core computer science subjects. With strong expertise in full-stack development, he provides hands-on training in programming languages and in-demand technologies at the Scientech Easy Institute, Dhanbad.

He regularly publishes in-depth tutorials, practical coding examples, and high-quality learning resources for both beginners and working professionals. Every article is carefully researched, technically reviewed, and regularly updated to ensure accuracy, clarity, and real-world relevance, helping learners build job-ready skills with confidence.