w3resource logo

:

Java Programming Exercies

Java DateTime, Calendar Exercises: Check a year is a leap year or not

Java DateTime, Calendar : Exercise-18 with Solution

Write a Java program to check a year is a leap year or not.

Sample Solution:

Java Code:

public class Exercise18 {
   public static void main(String[] args)
    {
     //year to leap year or not
       int year = 2016;
       System.out.println();        
       if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
               System.out.println("Year " + year + " is a leap year");
       else
               System.out.println("Year " + year + " is not a leap year");
               System.out.println();
    }
}

Sample Output:

Year 2016 is a leap year 

Sample Solution:

Alternate Code :

import java.time.*;
import java.util.*;

public class Exercise1 {
   public static void main(String[] args)
    {
     LocalDate today = LocalDate.now();    
     if(today.isLeapYear())
     { 
       System.out.println("This year is Leap year"); 
     }
     else 
     { 
       System.out.println("This year is not a Leap year"); 
      }
    }
}

Java Code Editor:

Improve this sample solution and post your code through Disqus