Java Program to Check Whether a Year is a Leap Year
A normal year has 365 days. A leap year has 366 days because February gets an extra day, the 29th. Leap years keep the calendar in step with the Earth's orbit, which takes about 365.25 days.
This page explains the leap year rules and shows three Java programs that check whether a year is a leap year.
1. Leap Year Rules
A year is a leap year if:
- it is divisible by 4 and not divisible by 100, or
- it is divisible by 400.
So most century years (years ending in 00) are not leap years. Only the centuries divisible by 400 are.
| Year | Divisible by 4 | Divisible by 100 | Divisible by 400 | Leap year? |
|---|---|---|---|---|
| 2024 | Yes | No | No | Yes |
| 2023 | No | No | No | No |
| 1900 | Yes | Yes | No | No |
| 2000 | Yes | Yes | Yes | Yes |
2. Leap Year Program Using if-else
The nested if-else statements follow the rules step by step: first divisibility by 4, then by 100, then by 400.
import java.util.Scanner;
public class LeapYear {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a year: ");
int year = scanner.nextInt();
scanner.close();
boolean isLeap;
if (year % 4 == 0) {
if (year % 100 == 0) {
// century year: leap only if divisible by 400
isLeap = year % 400 == 0;
} else {
isLeap = true;
}
} else {
isLeap = false;
}
if (isLeap) {
System.out.println(year + " is a leap year.");
} else {
System.out.println(year + " is not a leap year.");
}
}
}
Enter a year: 2024 2024 is a leap year.
Enter a year: 1900 1900 is not a leap year.
Enter a year: 2000 2000 is a leap year.
2024 is divisible by 4 but not by 100, so it is a leap year. 1900 is divisible by 100 but not by 400, so it is not. 2000 is divisible by 400, so it is.
3. Leap Year Check in One Condition
The same rules fit into a single boolean expression using && (and) and || (or). Putting it in a method makes it easy to test many years.
public class LeapYearCondition {
static boolean isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
}
public static void main(String[] args) {
int[] years = {1900, 1996, 2000, 2023, 2024, 2100};
for (int year : years) {
System.out.println(year + " -> " + (isLeapYear(year) ? "leap year" : "not a leap year"));
}
}
}
1900 -> not a leap year 1996 -> leap year 2000 -> leap year 2023 -> not a leap year 2024 -> leap year 2100 -> not a leap year
4. Leap Year Check Using Year.isLeap()
Since Java 8, the java.time.Year class has a built-in isLeap() method that applies the same rules.
import java.time.Year;
public class LeapYearBuiltIn {
public static void main(String[] args) {
System.out.println("2024 is a leap year: " + Year.isLeap(2024));
System.out.println("2100 is a leap year: " + Year.isLeap(2100));
System.out.println("2000 is a leap year: " + Year.of(2000).isLeap());
}
}
2024 is a leap year: true 2100 is a leap year: false 2000 is a leap year: true
5. Summary
- A year is a leap year if it is divisible by 4 and not by 100, or if it is divisible by 400.
- Century years such as 1900 and 2100 are not leap years; 2000 is, because it is divisible by 400.
- The whole rule fits in one condition:
(year % 4 == 0 && year % 100 != 0) || year % 400 == 0. - In real code,
Year.isLeap(year)fromjava.timedoes the check for you.