LocalDate isLeapYear() method in Java


It can be checked if the LocalDate is in a leap year or not using the isLeapYear() method in the LocalDate class in Java. This method requires no parameters. It returns true if the LocalDate is in a leap year and false otherwise.

A program that demonstrates this is given as follows −

Example

 Live Demo

import java.time.*;
public class Main {
   public static void main(String[] args) {
      LocalDate ld = LocalDate.parse("2012-10-12");
      System.out.println("The LocalDate is: " + ld);
      boolean flag = ld.isLeapYear();
      if(flag)
         System.out.println("This is a leap year");
      else
         System.out.println("This is not a leap year");
   }
}

Output

The LocalDate is: 2012-10-12
This is a leap year

Now let us understand the above program.

The LocalDate object is displayed. It is checked if the LocalDate object is in a leap year or not using the isLeapYear() method. The returned value is displayed using an if statement. A code snippet that demonstrates this is as follows −

LocalDate ld = LocalDate.parse("2012-10-12");
System.out.println("The LocalDate is: " + ld);
boolean flag = ld.isLeapYear();
if(flag)
   System.out.println("This is a leap year");
else
   System.out.println("This is not a leap year");

Updated on: 30-Jul-2019

60 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements