# Java program to find if given year is leap year

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 then it is a leap year else we further divide it by 4.

Again if it is divisible then it is a leap year else it’s not a leap year. Now we will see java program to find if a given year is a leap year,

```/*
*
*
java program to check leap year
*
*
*/

import java.util.Scanner;

public class LeapYearDemo
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
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:

Year 2007 is not a Leap Year

Year 2016 is a Leap Year

### Using conditional operator

Let’s learn program to check if a given year is 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,

```/*
*
*
* java program to check if a given year is leap year
*
*
*/

import java.util.Scanner;

public class LeapYearExample
{
public static void main(String[] args)
{
long number, year, a;
Scanner sc = new Scanner(System.in);
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:

2002 is not a leap year

2004 is a leap year

Using 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 program to illustrate leap year java 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