I am pretty sure everyone of you must have heard about the term, Leap Year! A leap year has 366 days which occurs once in every 4 years. This additional day in the leap year is added in the month of February. Well, in this article I am going to brief you on how to implement leap year program in Java.
The agenda for this concept is going to be as follows:
What is a Leap Year?
A leap year has 366 days. Now, there are certain conditions to check whether a year is a leap year or not! Let’s have a look at them:
- The year should be divisible by 4.
- If the year is divisible by 100, it is NOT a leap year, UNLESS, the year is divisible by 400 also. For instance; take the year, 2100: it is divisible by 100, hence according to the condition it should NOT be a leap year, but, the second part says if it is divisible by 400 too, it is a leap year! Hence, 2100 is a leap year, because it is divisible by 100 and subsequently by 400!
Now that you are well aware of the concept, let us implement it through a Java code.
Implement Leap Year Program in Java
Here’s a java code to implement leap year program in java:
Code:
class Test { static boolean Year(int year) { if (year % 400 == 0) return true; if (year % 100 == 0) return false; if (year % 4 == 0) return true; return false; } public static void main(String[] args) { int year = 2018; System.out.println( Year(2018)? "Leap Year" : "Not a Leap Year" ); } }
Here, you witnessed how easily we implemented our concept in Java.
Java Program Explanation
- Input the year which you want your code to be checked for leap year.
- The if statement then checks if the year is a multiple of 4 but not 100 or if it is a multiple of 400.
- The result is then printed.
I hope you are clear with the concept of Leap Year in Java now. Keep reading, keep exploring!
With this, we come to an end of this blog on “leap year program in java”. I hope it added value to your knowledge of java .
Now that you have understood Java code, check out the Java training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification course is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.
Got a question for us? Please mention it in the comments section of this “leap year program in java” blog and we will get back to you as soon as possible.