Java 8 has java.time API now has an inbuilt function isLeap() in class Year that you can use to check if a year is a |leap year or not.
Example:import java.time.Year;
public class Example {
public static void main(String... args) {
int year1 = 2011;
int year2 = 2024;
System.out.println(year1 + " is leap year? " + isLeapYear(year1));
System.out.println(year1 + " is leap year? " + isLeapYear(year2));
}
//Check if year is leap or not using Java 8
//time API class Year and isLeap method.
public static boolean isLeapYear(int year) {
return Year.isLeap(year);
}
}
Check out more Leap year programs: https://code2care.org/java/java-leap-year-program-with-examples
More Posts related to Java,
- Drop table using Java JDBC Template
- Java - Check if array contains the value
- YAML Parser using Java Jackson Library Example
- Java Jackson ObjectMapper Class with Examples
- Get Client IP address from HTTP Response in Java
- How to Word-Warp Console logs in IntelliJ
- Exception in thread main java.lang.NoClassDefFoundError: package javaClass
- Setting Java_Home Environment variable on Windows Operating System
- Fix: Maven - Failed to execute goal - Compilation failure - Source/Target option 5 is no longer supported. Use 7 or later
- Java SE JDBC Select Statement Example
- How to extract Java Jar/War/Ear files in Linux
- java: unclosed string literal [Error]
- [Solution] Exception in thread main java.util.EmptyStackException
- Read YAML file Java Jackson Library
- What Java version is used for Minecraft 1.18
- [Java] How to throws Exception using Functional Interface and Lambda code
- [Program] How to read three different values using Scanner in Java
- Java 8 Predicate Functional Interface Examples
- Display Era details (AD BC) in Java Date using SimpleDateFormat
- Convert String Date to Date Object in Java
- Struts 2 Hello World Example in Eclipse
- Read a file using Java 8 Stream
- Java - How to set custom thread name?
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- java: ']' expected error [fixed]
More Posts:
- Installing MongoDB on Linux/Unix/macOS/Ubuntu - MacOS
- Can we move apps like WhatsApp, Facebook to external MicroSD card - WhatsApp
- How to open new tabs macOS Terminal - MacOS
- How to update Android Studio - Android-Studio
- Make Android View Scrollable both Horizontally and Vertically - Android
- Android : Unable to load VM from snapshot : Mac OS X Error - Android
- Remove AirDrop Icon from macOS Menu Bar - MacOS
- Check help on commands while being on sftp> prompt - FTP