Java Data Type How to - Check Leap Year in Java 8








Question

We would like to know how to check Leap Year in Java 8.

Answer

import java.time.LocalDate;
//from  w  ww.ja v a 2s.  c o m
public class Main {
  public static void main(String[] argv) {
    LocalDate today = LocalDate.now();
    if (today.isLeapYear()) {
      System.out.println("This year is Leap year");
    } else {
      System.out.println("not a Leap year");
    }

    if (today.withYear(2016).isLeapYear()) {
      System.out.println("2016 is Leap year");
    }

  }
}

The code above generates the following result.