In this example, we will use LocalDate class provided isLeapYear() API to check if a given year is a leap year or not. A LocalDate represents a year-month-day in the ISO calendar and is useful for representing a date without a time. You might use a LocalDate to track a significant event, such as birth date or wedding date.
Read more about LocalDate class with an example at https://www.javaguides.net/2018/08/java-8-localdate-class-api-guide.html.
Read more about LocalDate class with an example at https://www.javaguides.net/2018/08/java-8-localdate-class-api-guide.html.
LocalDate class provides below API to check if a given year is a leap year or not.
- boolean isLeapYear() - Checks if the year is a leap year, according to the ISO proleptic calendar system rules.
package com.ramesh.java8.datetime.api; import java.time.LocalDate; import java.time.Month; /** * Program to demonstrate LocalDate Class APIs. * @author javaguides.net * */ public class LocalDateExamples { public static void main(String[] args) { checkIfYearIsLeapYear(); } private static void checkIfYearIsLeapYear() { LocalDate localDate1 = LocalDate.of(2017, Month.JANUARY, 1); LocalDate localDate2 = LocalDate.of(2016, Month.JANUARY, 1); if (localDate1.isLeapYear()) { System.out.println(localDate1.getYear() + " is a leap year"); } else { System.out.println(localDate1.getYear() + " is not a leap year"); } if (localDate2.isLeapYear()) { System.out.println(localDate2.getYear() + " is a leap year"); } else { System.out.println(localDate2.getYear() + " is not a leap year"); } } }
Output:
2017 is not a leap year 2016 is a leap year
Comments
Post a Comment