What is a Leap year?
A leap year is a year that has one extra day added to it, making it 366 days long instead of the usual 365. This additional day is added to the month of February, which has 29 days in a leap year. The rules for determining if a year is a leap year are as follows:
- A year is a leap year if it is divisible by 4.
- However, if the year is divisible by 100, it is not a leap year unless:
- The year is also divisible by 400, which is a leap year.
In this article, we will explore how to check if a given year is a leap year using Java with several code examples.
Basic Leap Year Check
Here’s a simple Java program that checks if a given year is a leap year:
Code
import java.util.Scanner;
public class LeapYearChecker {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a year: ");
int year = scanner.nextInt();
if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) {
System.out.println(year + " is a leap year.");
} else {
System.out.println(year + " is not a leap year.");
}
scanner.close();
}
}
Explanation
- We use Scanner to take user input for the year.
- The conditions check if the year meets the criteria for being a leap year.
- The result is printed based on the evaluation.
Output
Using Ternary Operator
You can also use the ternary operator to make the code more concise:
Code
import java.util.Scanner;
public class LeapYearChecker {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a year: ");
int year = scanner.nextInt();
String result = (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
? (year + " is a leap year.")
: (year + " is not a leap year.");
System.out.println(result);
scanner.close();
}
}
Explanation
- The ternary operator checks the condition and assigns the appropriate message to the result.
Finally, we print the result.
Output Example
Using Functions
To make your code more modular, you can create a separate function to check for leap years:
Code
import java.util.Scanner;
public class LeapYearChecker {
public static boolean isLeapYear(int year) {
return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a year: ");
int year = scanner.nextInt();
if (isLeapYear(year)) {
System.out.println(year + " is a leap year.");
} else {
System.out.println(year + " is not a leap year.");
}
scanner.close();
}
}
Explanation
- We define the isLeapYear method, which encapsulates the logic for checking whether the given year is a leap year.
- This method returns true or false, allowing for cleaner code in the main method.
Output
Using Java's Built-in Year Class
Java provides an in-built Year class that simplifies checking for leap years:
Code
import java.time.Year;
import java.util.Scanner;
public class LeapYearChecker {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a year: ");
int year = scanner.nextInt();
if (Year.of(year).isLeap()) {
System.out.println(year + " is a leap year.");
} else {
System.out.println(year + " is not a leap year.");
}
scanner.close();
}
}
Explanation
- We import java.time.Year and use its isLeap() method to check if the specified year is a leap year.
Output
Conclusion
In this article, we explored different ways to check if a given year is a leap year in Java. From basic conditional statements to using Java's built-in classes, you can choose any method that suits your coding style and requirements. Understanding how to determine leap years not only helps with date calculations but also reinforces fundamental programming concepts such as conditional logic and modular design. Happy coding!