Java 8 – Check if a Year is a Leap Year

Introduction

Determining whether a year is a leap year is a common task in programming, especially in applications involving calendars, date calculations, or time-based events. A leap year has an extra day, February 29, and occurs every four years, with some exceptions. In Java, checking if a year is a leap year can be done using both traditional methods and the modern features introduced in Java 8. In this guide, we’ll explore how to check if a year is a leap year using both approaches.

Problem Statement

The task is to create a Java program that:

  • Accepts a year as input.
  • Checks if the year is a leap year.
  • Outputs whether the year is a leap year or not.

Example 1:

  • Input: 2020
  • Output: "2020 is a leap year."

Example 2:

  • Input: 2021
  • Output: "2021 is not a leap year."

Solution Steps

  1. Input Year: Start with a year that can either be hardcoded or provided by the user.
  2. Check Leap Year (Traditional Approach): Use conditional logic to determine if the year meets the criteria for a leap year.
  3. Check Leap Year (Java 8 Approach): Use the java.time.Year class introduced in Java 8 to perform the check.
  4. Display the Result: Print whether the year is a leap year or not.

Java Program

Traditional Approach: Check if a Year is a Leap Year

/**
 * Traditional Approach: Check if a Year is a Leap Year
 * Author: https://www.rameshfadatare.com/
 */
public class LeapYearTraditional {

    public static void main(String[] args) {
        // Step 1: Take input year
        int year = 2020;

        // Step 2: Check if the year is a leap year using traditional approach
        boolean isLeapYear = checkLeapYearTraditional(year);

        // Step 3: Display the result
        if (isLeapYear) {
            System.out.println(year + " is a leap year.");
        } else {
            System.out.println(year + " is not a leap year.");
        }
    }

    // Method to check if a year is a leap year (traditional approach)
    public static boolean checkLeapYearTraditional(int year) {
        if (year % 4 == 0) {
            if (year % 100 == 0) {
                return year % 400 == 0;
            } else {
                return true;
            }
        } else {
            return false;
        }
    }
}

Java 8 Approach: Check if a Year is a Leap Year

import java.time.Year;

/**
 * Java 8: Check if a Year is a Leap Year
 * Author: https://www.rameshfadatare.com/
 */
public class LeapYearJava8 {

    public static void main(String[] args) {
        // Step 1: Take input year
        int year = 2021;

        // Step 2: Check if the year is a leap year using Java 8 Year class
        boolean isLeapYear = checkLeapYearJava8(year);

        // Step 3: Display the result
        if (isLeapYear) {
            System.out.println(year + " is a leap year.");
        } else {
            System.out.println(year + " is not a leap year.");
        }
    }

    // Method to check if a year is a leap year (Java 8 Year class approach)
    public static boolean checkLeapYearJava8(int year) {
        return Year.of(year).isLeap();
    }
}

Explanation of the Programs

  • Traditional Approach: The first program uses simple conditional logic to determine if a year is a leap year. A year is a leap year if it is divisible by 4. However, if the year is divisible by 100, it must also be divisible by 400 to be considered a leap year.

  • Java 8 Approach: The second program utilizes the Year class from the java.time package introduced in Java 8. The Year.of(year).isLeap() method directly checks if the given year is a leap year, simplifying the logic.

Output Example

For both methods, the output will be:

Example 1:

Input: 2020
Output: 2020 is a leap year.

Example 2:

Input: 2021
Output: 2021 is not a leap year.

Advanced Considerations

  1. Historical Leap Years: The programs correctly handle leap years according to the Gregorian calendar, which was introduced in 1582. For years before that, the leap year rules may differ.

  2. Performance Considerations: Both methods are efficient and suitable for checking leap years. The traditional method is straightforward and easy to understand, while the Java 8 approach leverages modern features and simplifies the code.

  3. Input Validation: The examples provided assume that the input is a valid integer year. In a real-world application, you should include error handling to manage invalid input.

Conclusion

This guide provides two methods for checking if a year is a leap year: the traditional approach using conditional logic and a more modern approach using Java 8’s Year class. Both methods are effective, but the Java 8 approach offers a more concise and readable solution. Depending on your needs and the style of your codebase, either method can be used to achieve the desired result.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top