Scanner sc =new Scanner(System.in);
System.out.println("请输入日期");
int year = sc.nextInt();
int month = sc.nextInt();
int day = sc.nextInt();
int sumdays = getSumDays(year,month,day);
System.out.println(year + "年" +month + "月" +day+ "日是" +year
+"年的第" +sumdays +"天");
}
public static int getSumDays(int year,int month,int day){
int num = 0;
for (int x =1;x<month;x++){
num +=getMonthDays(year, x);
}
num +=day;
return num;
}
public static int getMonthDays(int year,int month){
switch (month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 4:
case 6:
case 9:
case 11:
return 30;
case 2:
if (year%4==0 && year%100!=0 || year%400 == 0){
return 29;
}else {
return 28;
}
}
return 0;
}
Java根据输入的日期计算日期是这一年的第几天
最新推荐文章于 2023-09-21 10:38:24 发布