What is a Leap Year?
Leap year is a year, occurring once every four years, that has 366 days including February 29 as an intercalary day.
- Is the year divisible by 4? If no – it’s not a leap year
- If yes – Is the year divisible by 100? If no – it’s a leap year
- If yes – Is the year divisible by 400? If no – it’s not a leap year
- If yes – it’s a leap year
- If yes – Is the year divisible by 400? If no – it’s not a leap year
- If yes – Is the year divisible by 100? If no – it’s a leap year
In this tutorial we will implement this logic and find out year is a Leap year or not?
Let’s get started:
Create class CrunchifyFindLeapYear.java
. Just copy and paste below code.
Here there are two different ways we will check if year is a leap year or not?
package crunchify.com.java.tutorials; import java.util.GregorianCalendar; /** * @author Crunchify.com * Program: In Java how to find if Year is Leap Year or not? * Two different methods. */ public class CrunchifyFindLeapYear { public static void main(String[] args) { CrunchifyFindLeapYear crunchifyInstance = new CrunchifyFindLeapYear(); crunchifyLog("Is it a leap year?: " + crunchifyInstance.isLeapYear(createRandomIntBetween(1900, 2021))); crunchifyLog("Is it a leap year?: " + crunchifyInstance.isLeapYear(createRandomIntBetween(1900, 2021))); crunchifyLog("Is it a leap year?: " + crunchifyInstance.isLeapYear(createRandomIntBetween(1900, 2021))); crunchifyLog("Is it a leap year?: " + crunchifyInstance.isLeapYear(createRandomIntBetween(1900, 2021))); crunchifyLog("Is it a leap year?: " + crunchifyInstance.isLeapYearUsingGregorianCalendar(createRandomIntBetween(1900, 2021))); crunchifyLog("Is it a leap year?: " + crunchifyInstance.isLeapYearUsingGregorianCalendar(createRandomIntBetween(1900, 2021))); crunchifyLog("Is it a leap year?: " + crunchifyInstance.isLeapYearUsingGregorianCalendar(createRandomIntBetween(1900, 2021))); crunchifyLog("Is it a leap year?: " + crunchifyInstance.isLeapYearUsingGregorianCalendar(createRandomIntBetween(1900, 2021))); } // Simple Crunchify Print Utility private static void crunchifyLog(String s) { System.out.println(s); } // Generate Random Year between 1900 and 2021 public static int createRandomIntBetween(int start, int end) { int year = start + (int) Math.round(Math.random() * (end - start)); crunchifyLog("\n==> " + year + ""); return year; } // Method-1: Logic to find out if Year is a Leap year or not? public boolean isLeapYear(int year) { return (year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)); } // Method-2: Logic to find out if Year is a Leap year or not using GregorianCalendar. public boolean isLeapYearUsingGregorianCalendar(int year) { // GregorianCalendar is a concrete subclass of Calendar and provides the standard calendar system used by most of the world. // GregorianCalendar is a hybrid calendar that supports both the Julian and Gregorian calendar systems with the support of a single discontinuity, // which corresponds by default to the Gregorian date when the Gregorian calendar was instituted (October 15, 1582 in some countries, later in others). // The cutover date may be changed by the caller by calling setGregorianChange(). GregorianCalendar crunchifyDates = ( GregorianCalendar) GregorianCalendar.getInstance(); // isLeapYear(): Determines if the given year is a leap year. Returns true if the given year is a leap year. // To specify BC year numbers, 1 - year number must be given. For example, year BC 4 is specified as -3. return crunchifyDates.isLeapYear(year); } }
IntelliJ IDEA Result:
Just run above program as Java Application and you will see result as below.
==> 2015 Is it a leap year : false ==> 1936 Is it a leap year : true ==> 1992 Is it a leap year : true ==> 1960 Is it a leap year : true ==> 1949 Is it a leap year : false ==> 1969 Is it a leap year : false ==> 1924 Is it a leap year : true ==> 1926 Is it a leap year : false Process finished with exit code 0
Let me know if you face any issue running above program.