Let’s learn java program to find if given year is leap year.

## Java program to check leap year

Java program to check whether year is leap year or not is one of the common interview question.

Leap year is a year where it has 366 days. In leap year program in java user is requested to enter year using scanner. This year is checked whether it is divisible by 400 or not.

Also read – insertion sort in java

If it is divisible by 400 then it is a leap year else we check whether it is divisible by 100.

If it is divisible by 100 then it is a leap year else we further divide it by 4. Again if it is divisible by 4 then it is a leap year else it’s not a leap year.

Now we will see java program to find if given year is leap year or simple java program to check leap year or leap year program in java using scanner,

import java.util.Scanner; public class LeapYearDemo { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Please enter any year: "); int year = sc.nextInt(); boolean temp = false; if(year % 400 == 0) { temp = true; } else if(year % 100 == 0) { temp = false; } else if(year % 4 == 0) { temp = true; } else { temp = false; } if(temp) { System.out.println("Year " + year + " is a Leap Year"); } else { System.out.println("Year " + year + " is not a Leap Year"); } sc.close(); } }

**Output:**

Please enter any year: 2007

Year 2007 is not a Leap Year

Please enter any year: 2016

Year 2016 is a Leap Year

### leap year using conditional operator

Let’s learn java program to find leap year using conditional operator. Here first we have to read value from user using nextLong() method of Scanner class.

Then using ternary operator we should calculate given year is leap year or not. For that we have to see that given year is divisible by 4 or divisible by 400 and not divisible by 100.

Now let’s see java program to find leap year using conditional operator,

import java.util.Scanner; public class LeapYearExample { public static void main(String[] args) { long number, year, a; Scanner sc = new Scanner(System.in); System.out.print("Please enter any year :"); year = sc.nextLong(); if(year != 0) { number = (year % 400 == 0)?(a = 1):((year % 100 == 0)?(a = 0):((year % 4 == 0)?(a = 1):(a = 0))); if(number == 1) { System.out.println(year + " is a leap year"); } else { System.out.println(year + " is not a leap year"); } } else { System.out.println("year zero does not exist "); } sc.close(); } }

**Output:**

Please enter any year :2002

2002 is not a leap year

Please enter any year :2004

2004 is a leap year

**leap year java method**

Here we are going to learn java leap year program using isLeap() method. isLeap() method is of Year class in java.

**Syntax:**

public boolean isLeap()

**Returns:** true if the year is leap, false otherwise.

Checks if the year is a leap year, according to the ISO proleptic calendar system rules.

Let’s see java program to illustrate leap year using isLeap() method,

import java.time.Year; public class IsLeapYear { public static void main(String[] args) { // creating Year object Year obj = Year.of(2005); // check if given year a leap year or not System.out.println(obj.isLeap()); } }

**Output:**

false