1.导入jar
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
</dependency>
2.编写代码
public class DateUtil {
public static boolean isLeapYear(int year){
if (0<year&&year<10000){
if ((year%4==0&&year%100!=0)||(year%100==0&&year%400==0)){
System.out.println(+year+"是润年");
return true;
}else{
System.out.println(year+"是平年");
return false;
}
}else{
System.out.println(year+"不再范围之内");
return false;
}
}
}
测试:
@Test
public void test(){
System.out.println(DateUtil.isLeapYear(-100));
System.out.println(DateUtil.isLeapYear(1000));
System.out.println(DateUtil.isLeapYear(20000));
System.out.println(DateUtil.isLeapYear(2019));
System.out.println(DateUtil.isLeapYear(2020));
System.out.println(DateUtil.isLeapYear(2000));
System.out.println(DateUtil.isLeapYear(1900));
}
**
结果:
-100不再范围之内
false
1000是平年
false
20000不再范围之内
false
2019是平年
false
2020是润年
true
2000是润年
true
1900是平年
false
**