public class Test4 {
// 一:键盘录入一个年份,判断该年是否为闰年
// 用两种方式完成:
// 第一种可以根据二月共有多少天来判断
// 第二种可以根据一年总共有多少天来判断
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int year;
while (true){
System.out.println("请输入一个年份:");
String s = sc.nextLine();
try {
year = Integer.parseInt(s);
break;
} catch (NumberFormatException e) {
//e.printStackTrace();
System.out.println("您输入的数据有误, 请重新输入:");
}
}
//demo1(year);
demo2(year);
}
private static void demo2(int year) {
LocalDate of = LocalDate.of(year, 12, 31);
//获取一年总共有多少天
int day = of.getDayOfYear();
//System.out.println(day);
//判断一年有多少天,如果有366天那么就是闰年否则就是平年
if (day == 366){
System.out.println(year+"是闰年");
}else {
System.out.println(year+"是平年");
}
}
// 第一种可以根据二月共有多少天来判断
private static void demo1(int year) {
//可以先将日期设置成3月的第一天然后减一天自然就获得了2月的最后一天
LocalDate localDate = LocalDate.of(year, 3, 1);
//三月的第一天减1就 等于二月的最后一天了
LocalDate date = localDate.plusDays(-1);
//获取月中的天数
int month = date.getDayOfMonth();
//判断一下二月是否等于29天,如果是的话就是闰年否则就是平年
if (month == 29){
System.out.println(year+"是闰年");
}else {
System.out.println(year+"是平年");
}
}
}