Table of contents
1.
Introduction
2.
Why Checking for Leap Year Matters in Programming
3.
Method 1: Using if-else for Finding Leap Year in Java
3.1.
Implementation
4.
Method 2: Using the Ternary Operator for Finding Leap Year
4.1.
Implementation
5.
Method 3: Using the command line to check for leap year in Java
5.1.
Using Command Line Arguments
5.1.1.
Implementation
5.2.
Using Scanner Class
5.2.1.
Implementation
6.
Method 4: Using in-built isLeap() method to check for Leap Year in Java
6.1.
Implementation
6.2.
Java
7.
Real-World Applications of Leap Year Check
7.1.
Leap Year Check in Calendars and Schedulers
7.2.
Validating Date Inputs in Software
8.
Frequently Asked Questions
8.1.
How to write leap year program in Java?
8.2.
How do you define a leap year in Java? 
8.3.
Is 3000 a leap year or not?
8.4.
Can a leap year be divisible by 100?
8.5.
What happens every 400 years?
8.6.
Is the logic same in other languages?
8.7.
What is an example of a leap year?
9.
Conclusion
Last Updated: Jun 9, 2025
Easy

Java Program to Check Leap Year

Author Vikash Kumar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Welcome to the coding problem to determine whether a given year is a leap year or not. In this article, we will discuss the question and also see the leap year program in Java. A leap year is a year that has 366 days instead of the usual 365 days. Leap years are necessary to keep our calendar year aligned with the astronomical year. 

leap year program in java

In this coding problem, we will be writing a leap year program in Java that takes a year as input and determines whether that year is a leap year or not. This problem is a classic example of conditional statements and logical operators in programming and is commonly used in many software applications. Let's get started!

Why Checking for Leap Year Matters in Programming

Leap year logic is crucial in software systems that rely on accurate date and time calculations, such as calendar applications, scheduling tools, financial systems, and time-sensitive operations. Failing to account for February 29th in leap years can lead to incorrect date validations, flawed billing cycles, or even system crashes. For example, a payroll system might miscalculate monthly salaries or due dates if it overlooks leap years. Ensuring leap year accuracy helps maintain reliability, user trust, and prevents subtle yet costly bugs in production environments.

Method 1: Using if-else for Finding Leap Year in Java

In this method, we will use a sequence of if-else conditions to match the whole condition of a year to be a leap year. 

Implementation

class Solution {
    public static void main(String[] args) {
        int year = 2023;

        // Checking the first condition
        if (year % 400 == 0) {
            System.out.println(year + " is a Leap Year.");
        } 
        
        // Checking the second condition
        else if (year % 100 == 0) {
            System.out.println(year + " is not a Leap Year.");
        } 
        
        // checking the third condition
        else if (year % 4 == 0) {
            System.out.println(year + " is a Leap Year.");
        } 
        else {
            System.out.println(year + " is not a Leap Year.");
        }
    }
}
You can also try this code with Online Java Compiler
Run Code


Output

2023 is not a Leap Year.


Explanation

In the above method, we are checking all the necessary conditions for a year to be a leap year. We used multiple if-else statements in the correct order to meet the conditions for a leap year. If we change any of the order of conditional statements in the above code, the logic of our program will get changed, and we will not get the desired output.

Method 2: Using the Ternary Operator for Finding Leap Year

In the previous method, we can see that using if-else statements in our code makes it a lot longer. The ternary operators are used to reduce the length of the if-else statement in our code. Thus, making the code short and easy to understand.

Implementation

class Solution {
    public static void main(String[] args) {
        int year = 2023 ;
        // Taking a boolean flag by defualt false
        boolean flag = false;

        // Checking the conditions and updating the flag
        flag = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)? true: false;

        // Printing the results
        if (!flag){
            System.out.println(year + " is not a Leap Year.");
        }
        else{
            System.out.println(year + " is a Leap Year.");
        }
    }
}
You can also try this code with Online Java Compiler
Run Code


Output

2023 is not a Leap Year.


Explanation

In the above method, we used a boolean variable to store the condition for leap year returned by the ternary operations. If the conditions are satisfied, we will get a true value, and if not, then a false value. After getting the values, we check them using if-else conditions and print the result accordingly. This method reduced the length of code drastically compared to the previous method.

Also see, Java Ioexception

Method 3: Using the command line to check for leap year in Java

In Java, the command-line arguments are the arguments that are passed at the time of running the Java code. These arguments are passed from the console and are received in the Java program as input. Users can pass arguments during the execution of the program by passing these command-line arguments in the main() method.

Two methods to take command line input in Java:

  • Using command-line arguments
  • Using Scanner class

Using Command Line Arguments

In Java, command-line arguments are used to pass arguments to the main program. The Java main method accepts a String array as an argument. When we pass a command line argument, they are treated as a string. Thus if we want to check for a leap year, the string should be converted to an integer.

Let's look at the code to understand it better.

Implementation

public class HelloJava {

    public static void main(String[] args) {
        if (args.length != 0){
        // converting string into integer
        int year = Integer.parseInt(args[0]);
        boolean flag = false ;

        flag = (year % 4 == 0 && year % 100 !=0 || year % 400 == 0)? true : false ;

        if(flag == false) {
            System.out.println(year + " is not a Leap Year.");
        }else {
            System.out.println(year + " is a Leap Year.");
        }

        }else {
            System.out.println("No arguments provided.");
        }
    }
}
You can also try this code with Online Java Compiler
Run Code


Output

2023 is not a Leap Year.


Explanation

In the above example, we used the Integer.parseInt() to convert the string argument into an integer. Now once the year is in integer format, we have applied various conditions to check for a leap year. After checking the conditions, we have printed the result accordingly.

Using Scanner Class

In Java, the Scanner class can be used to take command-line inputs from the user. It is present in the java.util package. Scanner class provides various methods like nextInt(), nextDouble(), etc., to take numerical input. Thus, the need for parsing using Integer.parseInt() is eliminated.

Let us now look at the code to check for a leap year using the Scanner class.

Implementation

import java.util.Scanner;
public class HelloJava {
    public static void main(String[] args) {
    	// Creating a Scanner object
    	Scanner s = new Scanner(System.in); 
    	System.out.println("Enter the year to check for a Leap Year: ");
   	
    	// Taking user input from command-line
    	int year = s.nextInt(); 
    	if (year % 400 == 0) {
        	System.out.println(year + " is a Leap Year.");
    	} else if (year % 100 == 0) {
        	System.out.println(year + " is not a Leap Year.");
    	} else if (year % 4 == 0) {
        	System.out.println(year + " is a Leap Year.");
    	} else {
        	System.out.println(year + " is not a Leap Year.");
    	}
    }
}
You can also try this code with Online Java Compiler
Run Code


Output

Enter the year to check for a Leap Year: 2023
2023 is not a Leap Year.


Explanation

In the above code, we are taking a command-line input using the Scanner object from the user. We used the nextInt() method of the Scanner object to store the year entered by the user and used if-else conditions to check for a leap year.

Method 4: Using in-built isLeap() method to check for Leap Year in Java

Java has an in-built isLeap() method that is used to check whether an input year is leap or not.

Implementation

  • Java

Java

import java.time.Year;

public class LeapYearExample {
   public static void main(String[] args) {
       int yearToCheck = 2024;

       if (Year.of(yearToCheck).isLeap()) {
           System.out.println(yearToCheck + " is a leap year.");
       } else {
           System.out.println(yearToCheck + " is not a leap year.");
       }
   }
}
You can also try this code with Online Java Compiler
Run Code

 

Output

2024 is a leap year.

 

Explanation

In code above, the java.time.Year class is used to create a Year object for the year you want to check. The isLeap() method of the Year class returns true if the year is a leap year and false otherwise.

Real-World Applications of Leap Year Check

Leap Year Check in Calendars and Schedulers

Calendar and scheduling applications like Google Calendar and Outlook depend on accurate leap year calculations to correctly handle February 29. These systems must ensure that recurring events, reminders, and appointments remain consistent across years. If leap year logic is incorrect or missing, events may shift by a day, causing confusion and data integrity issues. For instance, a recurring annual event might show up on February 28 or March 1 instead of February 29, disrupting user schedules and potentially affecting business workflows and automated notifications.

Validating Date Inputs in Software

User input validation in systems like flight booking, hotel reservations, or form submissions must accurately verify if February 29 exists in a given year. Without proper leap year logic, entering a valid date like February 29, 2024, might trigger errors or be rejected. This can lead to failed transactions, poor user experience, or even system crashes. Proper validation ensures that users can't enter impossible dates, maintaining both functionality and trust in time-sensitive applications.

Frequently Asked Questions

How to write leap year program in Java?

In order to check a leap year in Java, check whether the year is divisible by 400. If it is, then it is a leap year. Also, if the year is divisible by both 4 and 100, then it is also a leap year. Otherwise, it is not a leap year.

How do you define a leap year in Java? 

A leap year is a year with an extra day, making it 366 days in a year. In Java, you can define a leap year using various methods that check for a leap year. These methods can be if-else conditions, ternary operators, etc.

Is 3000 a leap year or not?

A year that is divisible by 100 should also be divisible by 400 and 4 to be a leap year. Hence, 3000 is not a leap year because it is not divisible by 400.

Can a leap year be divisible by 100?

Yes, but only if it's also divisible by 400. For example, 1900 is not a leap year, but 2000 is.

What happens every 400 years?

Every 400th year is a leap year to keep the calendar aligned with Earth’s orbit, correcting overcompensation from earlier rules.

Is the logic same in other languages?

Yes, most programming languages follow the same leap year rules based on the Gregorian calendar, ensuring consistent date handling.

What is an example of a leap year?

2024 is an example of a leap year as it is divisible by only 4 and not 100 or 400. According to the rules of the Gregorian calendar, a leap year number must be divisible by four – except for end-of-century years, which must also be divisible by 400.

Conclusion

In this article, we discussed the problem of determining whether a year is a leap year or not. We also saw the approach and Java code to solve it. Hope this article helped you to understand this question in an easy way and motivated you through your coding journey.

Check out other articles to learn more: 

Live masterclass