Home Java Check if a Year is a Leap Year in Java

Check if a Year is a Leap Year in Java

In this example we will look at how to check if a year is a leap year or not

if a year is not divisible by 4  then it is not a leap year
if the year is not divisible by 100 then it is a leap year
if the year is not divisible by 400 then it is not a leap year
Otherwise it is a leap year

Example

import java.util.Scanner;

public class JavaApplication1 
{
    private static Scanner myScanner;
    public static void main(String[] args) 
    {
        
        int year;
        // get user input
        myScanner= new Scanner(System.in);
        year = myScanner.nextInt();
 
        // condition check
        if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) 
        {        
            // true- Print leap year
            System.out.println(year + " : Leap Year");
        }
        else 
        {
            // false- Print Non-leap year
            System.out.println(year + " : Non - Leap Year");
        }
    }
}

 

Here are a few test runs

run:
2000
2000 : Leap Year

run:
2002
2002 : Non - Leap Year

run:
2004
2004 : Leap Year

You may also like