Java Program to Check If a Year is Leap Year or Not

  • Write a program in Java to check for leap years.
  • Algorithm to check whether a year is leap year or not.

A leap year contains 366 days instead of the usual 365, by extending February to 29 days rather than the common 28 days. A leap year is a year containing one additional day in order to keep the calendar year in sync with the astronomical year.

It takes the Earth approximately 365.242189 days – or 365 days, 5 hours, 48 minutes, and 45 seconds – to circle once around the Sun. However, the Gregorian calendar has only 365 days in a year, so if we didn't add a leap day on February 29 nearly every four years, we would lose almost six hours off our calendar every year.

Example of leap years: 1980, 1984, 1988, 1992, 1996
Algorithm to check whether a year is leap year or not
  • If a year is divisible by 4 but not by 100, then it is a leap year.
  • If a year is divisible by both 4 and by 100, then it is a leap year only if it is also divisible by 400.
Here is the leap year check condition in one line.
    if(((year%4 == 0) && (year%100 != 0)) || (year%400==0)){
        /* Leap year */
    } else {
        /* Not a leap year */
    }

Java Program to check whether for leap year

package com.tcc.java.programs;

import java.util.*;

public class LeapYearCheck {
    public static void main(String args[]) {
        int year;

        Scanner in = new Scanner(System.in);
        System.out.println("Enter a year");
        year = in.nextInt();
   
        if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
             System.out.println(year + " is a Leap Year");
        else
             System.out.println(year + " is not a Leap Year");
    }
}
Output
Enter a year
2014
2014 is not a Leap Year
Enter a year
2012
2012 is a Leap Year