除世纪年(以 00 结尾的年份)外,闰年可以被 4 整除。只有能被 400 整除的世纪年才是闰年。
示例: 检查闰年的 Java 程序
public class Main { public static void main(String[] args) { // year to be checked int year = 1996; boolean leap = false; // if the year is divided by 4 if (year % 4 == 0) { // if the year is century if (year % 100 == 0) { // if year is divided by 400 // then it is a leap year if (year % 400 == 0) leap = true; else leap = false; } // if the year is not century else leap = true; } else leap = false; if (leap) System.out.println(year + " is a leap year."); else System.out.println(year + " is not a leap year."); } }
输出
1900 is not a leap year.
在上面的例子中,我们正在检查年份
1900
是否是闰年。由于
1900
是世纪年(以 00 结尾),所以它应该可以被 4 和 400 整除才能成为闰年。 >
然而,
1900
不能被 400 整除。因此,它不是闰年。
现在,让我们将年份更改为
2012
。输出将是
2012 is a leap year.
这里,
2012
不是世纪年。因此,要成为闰年,它需要只能被4整除。
由于
2012
能被4整除,所以是闰年。