Java Leap Year
Java Leap Year
*;
/**
* LeapYear.java -
* Given a year it is calculated if that year
* is a leap year. Leap year calculation was
* one of the big issues in the Y2K problem.
*
* The specific rules for determining leap years are:
*
* 1) If a year is divisible by 4 it is a
* leap year if #2 does not apply.
* 2) If a year is divisible by 100 it is
* not a leap year unless #3 applies.
* 3) If a year is divisible by 400 it is
* a leap year.
*
* Many programs are believed to have incorrect
* logic for computing leap years due to the
* omission of #2 and/or #3.
*
* The handling of the 1900's vs. 2000's is
* done by a technique called windowing. This
* is a common strategy that was used to "solve"
* the Y2K problem. However, the problem has
* realy just been postponed by some number
* of years.
*
* NOTE: This program ignores changes in the
* Gregorian calendar and only applies
* correctly to dates after 1582.
*
* @author Grant William Braught
* @author Dickinson College
* @version 9/17/2001
*/
public class LeapYear {
public static void main (String[] args) {
// The year to check for leapiness.
int theYear;
// Get the year.
// For now we'll assume they enter a reasonable value.
System.out.print("Enter the year: ");
theYear = Console.in.readInt();
// Check for a 2 digit year and adjust the year
// using a windowing technique. If the date has
// more than 2 digits we'll assume it is a complete
// year.
if (theYear < 100) {
// If the year is greater than 40 assume it
// is from 1900's. If the year is less than
// 40 assume it is from 2000's.
if (theYear > 40) {
theYear = theYear + 1900;
}
else {
theYear = theYear + 2000;
}
}
// Is theYear Divisible by 4?
if (theYear % 4 == 0) {
// Is theYear Divisible by 4 but not 100?
if (theYear % 100 != 0) {
System.out.println(theYear + " is a leap year.");
}
// Is theYear Divisible by 4 and 100 and 400?
else if (theYear % 400 == 0) {
System.out.println(theYear + " is a leap year.");
}
// It is Divisible by 4 and 100 but not 400!
else {
System.out.println(theYear + " is not a leap year.");
}
}
// It is not divisible by 4.
else {
System.out.println(theYear + " is not a leap year.");
}
}
}