Java LocalDate isLeapYear Method

JavaJavaBeginner
Practice Now

Introduction

This lab will guide you through the process of using the isLeapYear() method in the Java LocalDate class. The isLeapYear() method returns a boolean value of true or false indicating whether the given year is a leap year or not.

Create a new Java file

Navigate to the ~/project directory and create a new Java file called LeapYearChecker.java using the following command:

touch LeapYearChecker.java

Import the required package

Add the following import statement at the beginning of your file to import the Java LocalDate class:

import java.time.LocalDate;

Create a main method

Add the following signature to your file to create a new main method:

public static void main(String[] args) {
    // Write your code here
}

Check if a year is a leap year

Inside the main method, create a LocalDate object and pass the year you want to check as the first argument while setting the month and day values to any valid values. Use the isLeapYear() method to check if the year is a leap year or not, then print the result. Here's an example:

LocalDate date = LocalDate.of(2024, 1, 1);
boolean isLeapYear = date.isLeapYear();
System.out.println("Is the year 2024 a leap year? " + isLeapYear);

Test the program

Save the changes to the file and exit the editor. Compile and run the program using the following commands:

javac LeapYearChecker.java
java LeapYearChecker

You should see the following output:

Is the year 2024 a leap year? true

Test another year

Modify the year value to any other year to check if it is a leap year or not. For example, change the year to 2021:

LocalDate date = LocalDate.of(2021, 1, 1);
boolean isLeapYear = date.isLeapYear();
System.out.println("Is the year 2021 a leap year? " + isLeapYear);

Test the next leap year

Modify the year to the next leap year, which is 2024, to make sure the program can check for leap years accurately:

LocalDate date = LocalDate.of(2024, 1, 1);
boolean isLeapYear = date.isLeapYear();
System.out.println("Is the year 2024 a leap year? " + isLeapYear);

Test a year that is not a leap year

Modify the year to any year that is not a leap year, such as 2022:

LocalDate date = LocalDate.of(2022, 1, 1);
boolean isLeapYear = date.isLeapYear();
System.out.println("Is the year 2022 a leap year? " + isLeapYear);

Test a year divisible by 100 but not by 400

Modify the year to a year that is divisible by 100 but not by 400, such as 1900:

LocalDate date = LocalDate.of(1900, 1, 1);
boolean isLeapYear = date.isLeapYear();
System.out.println("Is the year 1900 a leap year? " + isLeapYear);

Test a year that is divisible by 400

Modify the year to a year that is divisible by 400, such as 2000:

LocalDate date = LocalDate.of(2000, 1, 1);
boolean isLeapYear = date.isLeapYear();
System.out.println("Is the year 2000 a leap year? " + isLeapYear);

Summary

In this lab, you have learned how to use the isLeapYear() method in the Java LocalDate class to check if a given year is a leap year or not. By following this guide, you can accurately determine whether a year is a leap year or not according to the ISO proleptic calendar system rules using Java.

Other Java Tutorials you may like