# Java : Calculate Leap Year and Number of days in a Year

Below program shows you how to calculate or check if a year is leap year or not and how to check number of days in a year.

## Check if year is Leap Year and Number of Days

The algorithm to check if given year is leap year or not is as follows

- If year is divisible by 400 then it is leap year.
- OR
- If year is divisible by 4 but not by 100, then it is leap year.

**CalculateLeapYearExample.java**

package com.sneppets.example; import java.time.LocalDate; public class CalculateLeapYearExample { public static final Integer LAST_DOY_LEAP_YEAR = 366; public static final Integer LAST_DOY = 365; public static void main (String[] args) { LocalDate date = LocalDate.now(); System.out.println("Today's Date : " + date); System.out.println("Year : " + date.getYear()); System.out.println("Leap Year ? " + checkLeapYear(date.getYear())); System.out.println("Number of days : " + getNumberOfDays(date.getYear())); } private static boolean checkLeapYear(int year) { //Algorithm //If year is divisible by 400 then it is leap year //OR //If year is divisible by 4 but not by 100, then it is leap year if((year % 400 == 0) || ((year % 4 ==0)&& (year % 100 != 0))) { return true; }else { return false; } } private static int getNumberOfDays(int year) { if(checkLeapYear(year)) { return LAST_DOY_LEAP_YEAR; }else { return LAST_DOY; } } }

**Output**

Today's Date : 2020-02-09 Year : 2020 Leap Year ? true Number of days : 366

Note, a leap year is a calendar year that contains an additional day. For example, in the **Gregorian calendar**, each leap year has 366 days instead of 365 days by adding additional one day to February month (29 days instead of 28 days). These extra days occur in years that are calculated as leap year using the algorithm above.

- Java : How to Compare two Sets and check its Equality efficiently
- Find first element from a collection (List or Set)
- How to add hours to unix timestamp or epochmilli in java?
- Check if Element exists in a Collection using Lambda Expressions

## Leave a Reply