Check Leap Year in Java

It is common Java Interview Question to Write a Java program to check leap year.

 

What is Leap Year? Leap year is a year with 366 days which is 1 extra day than normal year. This extra day comes in

Leap year is a year with 366 days which is 1 extra day than normal year. This extra day comes in the month of February and on leap year Feb month has 29 days than normal 28 days. You might know that leap year comes in an interval of 4 years. This year 2012 is a leap year and Feb has 29 days, you can check.

This year 2016 is a leap year and Feb has 29 days, you can check.
The logic to find Leap Year, if a year is multiple of 400 or multiple of 4 but not multiple of 100 then it’s a leap year.

In addition to this standard logic, you can also use Java’s Date, Time and Calendar API to check how many days any year has and by comparing that number with 365 you can find whether that year is the leap year or not.

 

Java program to Check Leap Year

Below Java program is implemented logic to find out whether a year is the leap year or not. The isleapyear(int year) method uses Java Calendar API to get the maximum number of days in that year and compare that with 365. If year contains more than 365 days, it’s a leap year.

The second method doesLeapYear(int year) uses programming logic to find if the year is the leap or not.

Output :

Is 1990 a leap year ? : false
Is 2012 a leap year ? : true
Is 1800 a leap year ? : false
Is 1920 a leap year ? : true

Does 1990 a leap year : false
Does 2012 a leap year : true
Does 1800 a leap year : false
Does 1920 a leap year : true

 

That’s all on How to check if a year is the leap year or not in Java.

0 Comment

Leave a Reply