可以使用以下的代码判断某一年是否为闰年:
public class LeapYearChecker { public static void main(String[] args) { int year = 2020; // 要判断的年份 if (isLeapYear(year)) { System.out.println(year + "是闰年"); } else { System.out.println(year + "不是闰年"); } } public static boolean isLeapYear(int year) { if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { return true; } else { return false; } } }
以上代码中,isLeapYear()
方法判断给定的年份是否为闰年。如果年份能被4整除但不能被100整除,或者能被400整除,则为闰年。在main()
方法中,可以将需要判断的年份赋值给year
变量,然后调用isLeapYear()
方法来判断是否为闰年,并打印结果。