In this article, we will learn how to check whether a given Date or LocalDate or Year or Calendar or GregorianCalendar is leap year or not ?
A leap year consists of 366 days with month of February being 29 days and on all other instances a normal year consists of 365 days with month of February being 28 days
Check given Date/Year/Calendar is Leap Year :
- Check Leap year for LocalDate
- Check Leap year for Year class
- Check Leap year for Date/Calendar in a custom way
- Check Leap year for GregorianCalendar
1. Check Leap year for LocalDate :
- LocalDate class has a method isLeapYear() which checks whether the invoking LocalDate is leap year or not and returns result in boolean form either true/false
- In the below illustration, we are checking for 3 different LocalDate with year being 2022, 2020 and 2024
CheckLeapYear1.java
package in.bench.resources.java8.localdate.examples;
import java.time.LocalDate;
import java.time.Month;
public class CheckLeapYear1 {
public static void main(String[] args) {
// 1. get current LocalDate
LocalDate localDate1 = LocalDate.now();
System.out.println("Current LocalDate is :- " + localDate1);
// 1.1 check leap year ?
boolean leapYear = localDate1.isLeapYear();
System.out.println("Current Year (" + localDate1.getYear() + ") is Leap year ? :- "
+ leapYear);
// 2. get past LocalDate
LocalDate localDate2 = LocalDate.of(2020, Month.MARCH, 20);
System.out.println("\nPast LocalDate is :- " + localDate2);
// 2.1 check leap year ?
leapYear = localDate2.isLeapYear();
System.out.println("Past Year (" + localDate2.getYear() + ") is Leap year ? :- "
+ leapYear);
// 3. get future LocalDate
LocalDate localDate3 = LocalDate.of(2024, Month.FEBRUARY, 16);
System.out.println("\nFuture LocalDate is :- " + localDate3);
// 3.1 check leap year ?
leapYear = localDate3.isLeapYear();
System.out.print("Future Year (" + localDate3.getYear() + ") is Leap year ? :- "
+ leapYear);
}
}
Output:
Current LocalDate is :- 2022-09-12
Current Year (2022) is Leap year ? :- false
Past LocalDate is :- 2020-03-20
Past Year (2020) is Leap year ? :- true
Future LocalDate is :- 2024-02-16
Future Year (2024) is Leap year ? :- true
2. Check Leap year for Year class :
- In a similar way, Year class has a method isLeap() which checks whether the invoking Year is leap year or not and returns result in boolean form either true/false
- In the below illustration, we are checking for 3 different Years with year being 2022, 2020 and 2024
CheckLeapYear2.java
package in.bench.resources.java8.localdate.examples;
import java.time.Year;
public class CheckLeapYear2 {
public static void main(String[] args) {
// 1. get current Year
Year currentYear = Year.now();
System.out.println("Current Year is :- " + currentYear);
// 1.1 check leap year ?
boolean leapYear = currentYear.isLeap();
System.out.println("Current Year (" + currentYear
+ ") is Leap year ? :- " + leapYear);
// 2. get past Year
Year pastYear = Year.of(2020);
System.out.println("\nPast Year is :- " + pastYear);
// 2.1 check leap year ?
leapYear = pastYear.isLeap();
System.out.println("Past Year (" + pastYear
+ ") is Leap year ? :- " + leapYear);
// 3. get future Year
Year futureYear = Year.of(2024);
System.out.println("\nFuture Year is :- " + futureYear);
// 3.1 check leap year ?
leapYear = futureYear.isLeap();
System.out.print("Future Year (" + futureYear
+ ") is Leap year ? :- " + leapYear);
}
}
Output:
Current Year is :- 2022
Current Year (2022) is Leap year ? :- false
Past Year is :- 2020
Past Year (2020) is Leap year ? :- true
Future Year is :- 2024
Future Year (2024) is Leap year ? :- true
3. Check Leap year for Date/Calendar :
- There are no direct methods available to check whether Date object or Calendar instance is from leap year or not
- So, to check whether given Date object or Calendar instance is leap year, custom code has to be written with below logic or pseudo code
- A year is Leap year -> if it is perfectly divisible by 4 && it isn’t a Century Year
- If it is a Century Year, then it should be perfectly divisible by 400
- Note: we are checking against a current Date and past Calendar instance
CheckLeapYearForDateAndCalendar.java
package in.bench.resources.java8.localdate.examples;
import java.util.Calendar;
import java.util.Date;
public class CheckLeapYearForDateAndCalendar {
// main method
public static void main(String[] args) {
// 1. get current date/time
Date date1 = new Date();
System.out.println("Current Date/time is :- \n" + date1);
// 1.1 check leap year or not
boolean boolLeapYear = isLeapYear(date1.getYear());
System.out.println("\nCurrent Year (" + (1900 + date1.getYear()) + ") is Leap Year ? :- "
+ boolLeapYear);
// 2. get Calendar instance
Calendar calendar = Calendar.getInstance();
calendar.set(2000, 06, 13);
System.out.println("\nCalendar instance is :- \n" + calendar.getTime());
// 2.1 check leap year or not
boolLeapYear = isLeapYear(calendar.get(Calendar.YEAR));
System.out.print("\nYear (" + calendar.get(Calendar.YEAR) + ") is Leap Year ? :- "
+ boolLeapYear);
}
/**
* This method checks whether a given year is leap or not ?
*
* @param year
* @return
*/
private static boolean isLeapYear(int year) {
// 1. A year is Leap year -> if it is perfectly divisible by 4 && it is not Century Year
// 2. If it is Century Year, then it should be perfectly divisible by 400
return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
}
}
Output:
Current Date/time is :-
Mon Sep 12 22:23:21 IST 2022
Current Year (2022) is Leap Year ? :- false
Calendar instance is :-
Thu Jul 13 22:23:21 IST 2000
Year (2000) is Leap Year ? :- true
4. Check Leap year for GregorianCalendar :
- GregorianCalendar class has a method isLeapYear() which checks whether invoking gregorianCalendar object is leap year or not
- First, extract year part from the GregorianCalendar object and use isLeapYear() method by passing year to check whether given GregorianCalendar object is from leap year or not
- In the below illustration, we are checking against current GregorianCalendar object
CheckLeapYearForGregorianCalendar.java
package in.bench.resources.java8.localdate.examples;
import java.util.GregorianCalendar;
public class CheckLeapYearForGregorianCalendar {
public static void main(String[] args) {
// 1. get GregorianCalendar instance
GregorianCalendar gregorianCalendar = new GregorianCalendar();
System.out.println("GregorianCalendar instance is :- \n"
+ gregorianCalendar.getTime());
// 2. check leap year or not
int year = gregorianCalendar.get(GregorianCalendar.YEAR);
boolean boolLeapYear = gregorianCalendar.isLeapYear(year);
System.out.print("\nYear (" + year + ") is Leap Year ? :- "
+ boolLeapYear);
}
}
Output:
GregorianCalendar instance is :-
Mon Sep 12 22:27:11 IST 2022
Year (2022) is Leap Year ? :- false
Related Articles :
- Java 8 – How to get Year, Month and Day fields from LocalDate ?
- Java 8 – How to get Number of Days in a Month from LocalDate ?
- Java 8 – How to get Number of Days in a Year from LocalDate ?
- Java 8 – How to form LocalDate passing Year, Month and Day fields ?
- Java 8 – How to parse LocalDate in String form ?
- Java 8 – How to convert String to LocalDate ?
- Java 8 – How to convert LocalDate to String ?
- Java 8 – How to convert LocalDate in different formats ?
- Java 8 – How to convert LocalDate in different Format Style ?
- Java 8 – How to convert LocalDate to LocalDateTime ?
- Java 8 – How to convert LocalDate to ZonedDateTime ?
- Java 8 – How to convert LocalDate to an OffsetDateTime ?
- Java 8 – How to convert LocalDate to an Instant ?
- Java 8 – How to convert LocalDate to java.util.Date and vice-versa ?
- Java 8 – How to convert LocalDate to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.Calendar and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.GregorianCalendar and vice-versa ?
- Java 8 – How to convert LocalDate to javax.xml.datatype.XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalDate in different ways ?
- Java 8 – How to add Year, Month and Day fields to LocalDate ?
- Java 8 – How to subtract Year, Month and Day fields from LocalDate ?
- Java 8 – How to alter Year, Month and Day fields of LocalDate ?
- Java 8 – How to check whether a LocalDate is Before another LocalDate ?
- Java 8 – How to check whether a LocalDate is After another LocalDate ?
- Java 8 – How to compare two LocalDate instances ?
- Java 8 – How to get remaining number of Days in a Year using LocalDate ?
- Java 8 – How to find difference between two LocalDate instances using Period ?
- Java 8 – How to calculate number of Days between two LocalDate instances ?
- Java 8 – What are the different ways to calculate difference between 2 Date/time ?
- Java 8 – How to get all dates between two LocalDate ?
References :
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html
- https://docs.oracle.com/javase/8/docs/api/java/time/Year.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Date.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html
- https://docs.oracle.com/javase/8/docs/api/java/util/GregorianCalendar.html
Happy Coding !!
Happy Learning !!