Leap Year Check

Description: Java implementation of checking if a given year (represented as an integer) is a leap year according to the Julian and Gregorian calendars. Requires importing java.util.GregorianCalendar.
Tested Platform: Java SE 19, Windows 10
Language: Java
// Determines if a given year is a leap year according to the Julian Calendar
// up to 1582 then proceeds with the Gregorian calendar from 1582 onwards.
// Expects year as integer and returns true or false if it is a leap year or not.

public static boolean isLeapYear(int year) {
    GregorianCalendar cal = new GregorianCalendar();
    cal.set(GregorianCalendar.YEAR, year);
    return cal.getActualMaximum(GregorianCalendar.DAY_OF_YEAR) > 365;

Posted: March 20, 2023

Return to the snippets listing