- 使用简单的if语句判断:
public static boolean isLeapYear(int year) { if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) { return true; } else { return false; } }
- 使用三元运算符进行判断:
public static boolean isLeapYear(int year) { return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) ? true : false; }
- 使用Calendar类进行判断:
import java.util.Calendar; public static boolean isLeapYear(int year) { Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, year); return cal.getActualMaximum(Calendar.DAY_OF_YEAR) > 365; }