All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Find Leap Year In Java

Last Updated : Aug 8, 2023

How To Find Leap Year In Java

In this article we will show you the solution of how to find leap year in java, the leap year is the year in which there are 366 days instead of 365. In order for a leap year to occur, it must satisfy either of the following constraints:

  • A number that can be divided by 400 should be used
  • In order for it to be divisible by four, it should not be divisible by 100

The Java language supports if-else blocks with appropriate conditions for identifying a leap year.

Similarly, Java provides a built-in method isLeap() for determining whether or not a year is a leap year. We will now discuss the idea of how to find leap years in java with an example.

Step By Step Guide On How To Find Leap Year In Java :-

import java.io.*;
 public class TalkersCode{
    public static void isLeapYear(int year)
    {
        boolean is_leap_year = false;
        if (year % 4 == 0) {
            is_leap_year = true;
            if (year % 100 == 0) {
                if (year % 400 == 0)
                    is_leap_year = true;
                else
                    is_leap_year = false;
            }
        }
        else
            is_leap_year = false;
        if (!is_leap_year)
            System.out.println(year + " : Non Leap-year");
        else
            System.out.println(year + " : Leap-year");
    }
    public static void main(String[] args)
    {
        isLeapYear(2000);
        isLeapYear(2002);
    }
}
  1. The program begins with importing necessary classes/files.
  2. The class is defined with the name 'TalkersCode'.
  3. A method named 'isLeapYear' is defined that takes an integer value 'year' as a parameter.
  4. A boolean variable 'is_leap_year' is declared with the initial value 'false'. This variable is used to store whether a given year is a leap year or not.
  5. The method checks whether the given year is divisible by 4 using the modulo operator.
  6. If the year is divisible by 4, the boolean variable 'is_leap_year' is set to true.
  7. The method checks whether the given year is a century or not. A century year is a year that is divisible by 100.
  8. If the given year is a century year, the method checks whether it is divisible by 400. If it is divisible by 400, then it is a century leap year. The year is not a leap year if the conditions are not met.
  9. If the given year is not a century year, the method does not check the divisibility by 400.
  10. If the given year is not divisible by 4, then the boolean variable 'is_leap_year' is set to false.
  11. The method checks the final value of the boolean variable 'is_leap_year'. If it is false, then the year is a non-leap year, and if it is true, it is a leap year.
  12. The method prints the year and whether it is a leap year or not on the console.
  13. The main method is defined that calls the 'isLeapYear' method twice with different values.
  14. The first call is made with the year 2000, which is a leap year because it is a century leap year.
  15. The second call is made with the year 2002, which is not a leap year.
  16. The program execution ends.

Conclusion :-

As a result, we have successfully learned how to find leap year in java with an example.

The term leap year refers to years that are either divisible by 400 or by 4, and not by 100. The answer to this question can be found by using simple if-else blocks.

In an elegant manner, ternary operators can be used to represent if-else logic.

In order to modularize the code, the entire if-else logic can be moved to a method that determines whether the input year is leap year.

I hope this article on how to find leap year in java helps you and the steps and method mentioned above are easy to follow and implement.