Check Leap Year in Java

Everyone knows that in a leap year we will have 29 days in February month. But how can we determine a leap year programmatically?

Rules to identify a leap year

  • Year should be divisible by 4 but not by 100, or
  • Year should be divisible by 400

Program

package com.techstackjournal.java.basics;

import java.util.Scanner;

public class LeapYearCheck {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		System.out.println("Enter Year:");
		int year = sc.nextInt();
		sc.close();

		if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
			System.out.format("%d is a leap year", year);
		} else {
			System.out.format("%d is not a leap year", year);
		}

	}
}
Enter Year:
2100
2100 is not a leap year

Leave a Comment