Check for a Leap Year on your own in Java

Description

The following code shows how to check for a Leap Year on your own.

Example


/*  ww  w. jav  a  2 s  . c  o m*/
import java.text.ParseException;

public class Main {

  public static void main(String[] args) throws ParseException {
    System.out.println(isLeapYear(2000));
  }

  public static boolean isLeapYear(int year) {

    if (year < 0) {
      return false;
    }

    if (year % 400 == 0) {
      return true;
    } else if (year % 100 == 0) {
      return false;
    } else if (year % 4 == 0) {
      return true;
    } else {
      return false;
    }
  }

}

The code above generates the following result.





















Home »
  Java Tutorial »
    Date »




Date Get
Date Set
Date Format
Date Compare
Date Convert
Date Calculation
Date Parse
Timezone