Java Program to Find if a Given Year is a Leap Year - GeeksforGeeks

Java Program to Find if a Given Year is a Leap Year

Last Updated : 25 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Leap Year contains 366 days, which comes once every four years. In this article, we will learn how to write the leap year program in Java.

Facts about Leap Year

Every leap year corresponds to these facts :

  • A century year is a year ending with 00. A century year is a leap year only if it is divisible by 400.
  • A leap year (except a century year) can be identified if it is exactly divisible by 4.
  • A century year should be divisible by 4 and 100 both.
  • A non-century year should be divisible only by 4.

Leap Year Program in Java

Below is the implementation of Leap Year:

Java




// Java program to find a leap year
// Importing Classes/Files
import java.io.*;
 
// Class for leap-year dealing
public class GeeksforGeeks {
    // Method to check leap year
    public static void isLeapYear(int year)
    {
        // flag to take a non-leap year by default
        boolean is_leap_year = false;
 
        // If year is divisible by 4
        if (year % 4 == 0) {
            is_leap_year = true;
 
            // To identify whether it is a
            // century year or not
            if (year % 100 == 0) {
                // Checking if year is divisible by 400
                // therefore century leap year
                if (year % 400 == 0)
                    is_leap_year = true;
                else
                    is_leap_year = false;
            }
        }
 
        // We land here when corresponding if fails
        // If year is not divisible by 4
        else
 
            // Flag dealing-  Non leap-year
            is_leap_year = false;
 
        if (!is_leap_year)
            System.out.println(year + " : Non Leap-year");
        else
            System.out.println(year + " : Leap-year");
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Calling our function by
        // passing century year not divisible by 400
        isLeapYear(2000);
 
        // Calling our function by
        // passing Non-century year
        isLeapYear(2002);
    }
}


Output

2000 : Leap-year
2002 : Non Leap-year

The complexity of  the above method

Time Complexity: O(1)
Auxiliary Space: O(1)

Explaination of the above Program:

Leap-Year-in-Java

As we check that 2000 is a century year divisible by 100 and 4. 2002 is not divisible by 4, therefore not a leap year. Using these facts we can check any year if it is leap or not.

More Methods to Check Leap Year in Java

There are certain methods for leap year programs in Java are mentioned below:

  1. Using Scanner Class
  2. Using Ternary Operator
  3. Using In-built isLeap() Method

1. Using Scanner Class

Here the user is provided the flexibility to enter the year of their own choice as Scanner Class is imported here rest of the if-else blocks are also combined in a single statement to check if the input year is a leap year.

Below is the Java program to implement the approach:

Java




// Java program to check Leap-year
// by taking input from user
 
// Importing Classes/Files
import java.io.*;
// Importing Scanner Class
import java.util.Scanner;
 
// Class to check leap-year or not
public class GFG {
 
    // Driver code
    public static void main(String[] args)
    {
        // Considering any random year
        int year;
 
        // Taking input from user using Scanner Class
        // scn is an object made of Scanner Class
        Scanner scn = new Scanner(System.in);
        year = scn.nextInt();
 
        // 1st condition check- It is century leap year
        // 2nd condition check- It is leap year and not
        // century year
        if ((year % 400 == 0)
            || ((year % 4 == 0) && (year % 100 != 0))) {
 
            // Both conditions true- Print leap year
            System.out.println(year + " : Leap Year");
        }
 
        else {
            // Any condition fails- Print Non-leap year
            System.out.println(year + " : Non - Leap Year");
        }
    }
}


 
Input

2012

Output

2012 : Leap Year

The complexity of  the above method

Time Complexity: O(1)
Auxiliary Space: O(1)

2. Using Ternary Operator

Ternary Operator is used to reduce the if-else statements. The ternary operator takes three operands, a boolean condition, an expression to execute if the condition is true, and an expression to execute if false.

Below is the Java program to implement the approach:

Java




// Java program to find a leap year
 
// Importing Classes/Files
import java.io.*;
 
// Class for leap-year dealing
public class GeeksforGeeks {
 
    // Method to check leap year
    public static void isLeapYear(int year)
    {
        // flag to take a non-leap year by default
        boolean is_leap_year = false;
 
        is_leap_year = (year % 4 == 0 && year % 100 != 0
                        || year % 400 == 0)
                           ? true
                           : false;
        if (!is_leap_year)
            System.out.println(year + " : Non Leap-year");
        else
            System.out.println(year + " : Leap-year");
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Calling our function by
        // passing century year not divisible by 400
        isLeapYear(2000);
 
        // Calling our function by
        // passing Non-century year
        isLeapYear(2002);
    }
}


Output

2000 : Leap-year
2002 : Non Leap-year

The complexity of  the above method

Time Complexity: O(1)
Auxiliary Space: O(1)

3. Using In-built isLeap() Method

Java has an in-built isLeap() method to check if the input year is a leap year or not.

Below is the Java program to implement the approach:

Java




// Java program to find a leap year
 
// Importing Classes/Files
import java.io.*;
import java.time.*;
import java.util.*;
 
// Class for leap-year dealing
public class GeeksforGeeks {
 
    // Method to check leap year
    public static void isLeapYear(int year)
    {
        // flag to take a non-leap year by default
        boolean is_leap_year = false;
 
        Year checkyear = Year.of(year);
 
        is_leap_year = checkyear.isLeap();
 
        if (!is_leap_year)
            System.out.println(year + " : Non Leap-year");
        else
            System.out.println(year + " : Leap-year");
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Calling our function by
        // passing century year not divisible by 400
        isLeapYear(2000);
 
        // Calling our function by
        // passing Non-century year
        isLeapYear(2002);
    }
}


Output

2000 : Leap-year
2002 : Non Leap-year

The complexity of  the above method

Time Complexity: O(1)
Auxiliary Space: O(1)



Previous Article
Next Article

Similar Reads

TCS Coding Practice Question | Checking Leap Year
Given a number N, the task is to check if N is a Leap Year or not, using Command Line Arguments. Examples: Input: N = 2000Output: YesInput: N = 1997Output: No Approach: Since the number is entered as Command line Argument, there is no need for a dedicated input lineExtract the input number from the command line argumentThis extracted number will be
3 min read
How to Print the Next N Leap Years in Java?
Concept: The basic assertion in problem-solving for the leap year is an interval of 4 years which is wrong in itself. For any random year in the calendar to be a leap year it must hold below conditions. Now if the year is a leap year the goal is simply to print the consecutive same kinds of years in a calendar year that is all years should be leap
3 min read
Java Program to Extract Last two Digits of a Given Year
As the name suggests where there is an execution to be operated required to deal with digits of the number, the modulo operator plays a vital role. Here the goal is to extract the last digits of a number. So making the problem easier to think about what if the goal is to extract the last digit from a number. Here in this case number is representing
3 min read
Java Program to Get Year From Date
Java is the most powerful programming language, by which we can perform many tasks and Java is an industry preferable language. So it is filled with a huge amount of features. Here we are going to discuss one of the best features of Java, which is how to get a year from date using Java. Methods: There are many ways to get a year from date of which
4 min read
Java Program to Display Dates of a Calendar Year in Different Format
As different countries do opt for different formats. So here the goal is simply to print dates of calendar in different years. The generic symbolic notation opted across the globe is: Symbolic NotationDepictsyyearMmonth in year dday in month Eday of week Concept: Whenever it comes down to the date and time the primary goal is to Date Class. It is n
4 min read
Java Program to Display Name of the Weekdays in Calendar Year
Concept: In java when it comes down to the date and time problems after hitting the brute force method one should always remember the Date class of java which not only provides to print current or forthcoming year, month, day, date, time, hour, minutes, and even precision to seconds. Not only one can display these parameters but also can be formatt
5 min read
Java Program to Display Name of Months of Calendar Year in Short Format
As we know that in a calendar year, there are a total of 12 Months. In order to convert the name of months to a shorter format, there are basically two ways, ie we can either make use of DateFormatSymbols class or SimpleDateFormat class. These classes have methods that are used to convert the names of the months to the shorter format, ie for eg, if
3 min read
Java Program to Generate Calendar of Any Year Without calendar.get() Function
Java program for generating the calendar of any desired year and month let us first go through an illustration before landing upon logic and procedural part. Illustration: Say the user wants to get the calendar of April 2011. Then, he is required to enter the year along with the month as integers and the output would return the desired month's cale
4 min read
java.time.Year Class in Java
The java.time.Year class represents a year in the ISO-8601 calendar system, such as 2021. Year is an immutable date-time object that represents a year. This class does not store or represent a month, day, time, or time-zone. The years represented by this class follow the proleptic numbering system that is as follows: The year 0 is preceded by year
8 min read
How to Find Which Week of the Year in Java?
In Java, we have the Calendar class and the newer LocalDate class from the java.time package which we can use to find which week of the year it is. The java.time package offers better functionality and ease of use than older date and time classes such as Calendar class. In this article, we will learn how to find which week of the year in Java. Prog
4 min read
Article Tags :
Practice Tags :