Java Program to Check Leap Year

You are Here:

What is Leap Year?

A leap year is a calendar year containing one additional day added to keep the calendar year synchronized with the astronomical or seasonal year. For example, 2024 is a leap year.

Tips: It is recommended to use our online Leap Year calculator for better understanding.

Condition for Leap Year

To check whether a year is a leap year or not, the year should satisfy at least one of the following two conditions

  1. A year should be exactly divisible by 4, but, not by 100.
  2. A year should be exactly divisible by 4, 100 and 400 at the same time.

Check Leap Year

In the following example, we will check whether the given year (2012) is leap year or not.

Example

Java Compiler
public class myClass { public static void main(String[] args) { int year = 2012; if(year % 4 == 0) { if((year % 100 == 0) && (year % 400 != 0)) System.out.format("%d is not a leap year", year); else System.out.format("%d is a leap year", year); } else System.out.format("%d is not a leap year", year); } }

Output

2012 is a leap year

Leap Years between the Given Range

In the following example, we will find all the Leap Years between 2000 and 2030.

Example

Java Compiler
public class myClass { public static void main(String[] args) { int start = 2000; int end = 2030; System.out.println("Leap years between 2000 and 2030:"); for(start=start; start<=end; start++) { if(start % 4 == 0) { if((start % 100 == 0) && (start % 400 != 0)) { // Not a leap year } else System.out.print(start +" "); } } } }

Output

Leap years between 2000 and 2030: 2000 2004 2008 2012 2016 2020 2024 2028

Check Leap Year for any Given Year

In the following example, we will check whether the given year is a leap year or not.

Example

Java Compiler
import java.util.Scanner; public class myClass { public static void main(String[] args) { Scanner reader = new Scanner(System.in); System.out.print("Enter a year: "); int year = reader.nextInt(); if(year % 4 == 0) { if((year % 100 == 0) && (year % 400 != 0)) System.out.format("%d is not a leap year", year); else System.out.format("%d is a leap year", year); } else System.out.format("%d is not a leap year", year); } }

Output

Enter a year: 2022 2022 is not a leap year

Reminder

Hi Developers, we almost covered 90% of String functions and Interview Question on Java with examples for quick and easy learning.

We are working to cover every Single Concept in Java.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author