java examples
Home Java Examples Resources Java Interview Questions
Brainpower SEO

How to check if a year is a leap year?

package com.javacoderanch.example.java.util;

import java.util.GregorianCalendar;

public class LeapYearExample 
{    
    public static void main(String[] args)
    {
        // Here we show how to know if a specified year is a leap year or not.
        // The GregorialCalendar object provide a convenient method to do this.
        // The method is GregorianCalendar.isLeapYear().
        
        // First, let's obtain an instance of GregorianCalendar.
        GregorianCalendar cal = new GregorianCalendar();    
        
        // The isLeapYear(int year) method will return true for leap year and
        // otherwise return false. In this example the message will be printed
        // as 2004 is a leap year.
        if (cal.isLeapYear(2004))
        {        
            System.out.println("The year 2004 is a leap year!");
        }
    }
}

The result of our code is:

The year 2004 is a leap year!