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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/date("`Date`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("`Packages / API`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/comments("`Comments`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/class_methods -.-> lab-117808{{"`Java LocalDate isLeapYear Method`"}} java/date -.-> lab-117808{{"`Java LocalDate isLeapYear Method`"}} java/modifiers -.-> lab-117808{{"`Java LocalDate isLeapYear Method`"}} java/packages_api -.-> lab-117808{{"`Java LocalDate isLeapYear Method`"}} java/identifier -.-> lab-117808{{"`Java LocalDate isLeapYear Method`"}} java/arrays -.-> lab-117808{{"`Java LocalDate isLeapYear Method`"}} java/comments -.-> lab-117808{{"`Java LocalDate isLeapYear Method`"}} java/data_types -.-> lab-117808{{"`Java LocalDate isLeapYear Method`"}} java/operators -.-> lab-117808{{"`Java LocalDate isLeapYear Method`"}} java/output -.-> lab-117808{{"`Java LocalDate isLeapYear Method`"}} java/strings -.-> lab-117808{{"`Java LocalDate isLeapYear Method`"}} java/variables -.-> lab-117808{{"`Java LocalDate isLeapYear Method`"}} java/system_methods -.-> lab-117808{{"`Java LocalDate isLeapYear Method`"}} end

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