Friday, December 21, 2012

How to check leap year in Java - program example

Write a Java program to find if a year is a leap year or not is a standard Java programming exercise during various Java programming course on school, colleges and various training institutes both online and offline,  along with other popular homework's e.g. printing Fibonacci numbers, checking palindromes or finding prime numbers. Just to recap a leap year is a year with 366 days which is 1 extra day than normal year. This extra day comes in month of February and on leap year Feb month has 29 days than normal 28 days. If you are following then you might know that leap year comes in a interval of 4 years. This year 2012 is a leap year and Feb has 29 days, you can check. Now if you are in programming before you might be familiar that there is standard logic to find leap year i.e. if an year is multiple of 400 or multiple of 4 but not multiple of 100 then its a leap year. In addition to this standard logic, you can also use Java's Date, Time and Calendar API to check how many days any year has and by comparing that number with 365 you can find whether that year is leap year or not . In this Java programming tutorial we will both of these example to check if a year is leap year or not.


Java program to check if a year is leap year

How to check if year is leap year or not in JavaHere is complete code example of Java program to find out whether an year is leap year or not. isLeapYear(int year) method uses Calendar API to get maximum number of days in that year and compare that with 365. If year contains more than 365 days, its a leap year. Second method doesLeapYear(int year) uses programming logic to find if year is leap or not.

package test;

import java.util.Calendar;

/**
 *
 * Java program to find if a year is leap year or not.
 * A leap year is a year which contains 366 day, which is 1 day more of normal 365 day year.
 * Leap year comes in a interval of 4 years. In Gregorian
 * calendar in leap year February has 29 days which is 1 day more than 28 day in normal year.
 *
 * @author
 */

public class LeapYearProgram {
 
 
    public static void main(String args[]) {
 
        //Testing some leap and non leap year using Java library code
        System.err.println("Is 2000 a leap year ? : " + isLeapYear(2000));
        System.err.println("Is 2012 a leap year ? : " + isLeapYear(2012));
        System.err.println("Is 1901 a leap year ? : " + isLeapYear(1901));
        System.err.println("Is 1900 a leap year ? : " + isLeapYear(1900));
     
     
        //Checking leap year without using library or API and applying logic
        System.err.println("Does 2000 a leap year : " + doesLeapYear(2000));
        System.err.println("Does 2012 a leap year : " + doesLeapYear(2012));
        System.err.println("Does 1901 a leap year : " + doesLeapYear(1901));
        System.err.println("Does 1900 a leap year : " + doesLeapYear(1900));
    }  
 
 
   /*
     * This method checks whether a year is leap or not by using Java Date
     * and Time API. Calendar class has utility method to return maximum
     * number of days in a year which can be used to check if its
     * greater than 365 or not
     */

   public static boolean isLeapYear(int year){
       Calendar cal = Calendar.getInstance(); //gets Calendar based on local timezone and locale
       cal.set(Calendar.YEAR, year); //setting the calendar year
       int noOfDays = cal.getActualMaximum(Calendar.DAY_OF_YEAR);
     
       if(noOfDays > 365){
           return true;
       }
     
       return false;
   }
 
   /*
    * This method uses standard logic to check leap year in Java.
    * A year is a leap year if its multiple of 400 or multiple of 4 but not 100
    */

   public static boolean doesLeapYear(int year){
       return (year%400 == 0) || ((year%100) != 0 && (year%4 == 0));
   }
}

Output:
Is 2000 a leap year ? : true
Is 2012 a leap year ? : true
Is 1901 a leap year ? : false
Is 1900 a leap year ? : false
Does 2000 a leap year : true
Does 2012 a leap year : true
Does 1901 a leap year : false
Does 1900 a leap year : false


That's all on How to check if a year is leap year or not in Java. I prefer to use library code instead of reinventing wheel but if it's an homework or programming exercise, you are likely to be asked to do this using pure logic. By the way this logic can be implemented in any other programming language like C, C++ or python as well.

Other Java programming and homework exercise for practice :

No comments:

Post a Comment

Java67 Headline Animator