以下是java实现的代码,逻辑简单,如有错误,请指正
import java.util.Scanner;
public class Test1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入一个要判断的日期:");
int year = input.nextInt();
int month = input.nextInt();
int day = input.nextInt();
boolean isTrue = false;
boolean isLeap = year%4==0&&year%100!=0||year%400==0;//闰年的判断
if(year>0){
if(month>=1&&month<=12){
if(month==4||month==6||month==9||month==11){
if(day>0&&day<31){
isTrue = true;
}
}else if(month==2){
int FebruaryDays = 28+(isLeap==true?1:0);
if(day>0&&day<=FebruaryDays){
isTrue = true;
}
}else{
if(day>0&&day<32){
isTrue = true;
}
}
}
}
if(isTrue){
System.out.println("这个日期是合法日期");
}else{
System.out.println("这个日期不是合法日期");
}
int days=0;
switch(month){//利用switch语句的顺序往下执行,且没有break时不退出的特点 计算平年的天数
case 12:days+=30;
case 11:days+=31;
case 10:days+=30;
case 9:days+=31;
case 8:days+=31;
case 7:days+=30;
case 6:days+=31;
case 5:days+=30;
case 4:days+=31;
case 3:days+=28;
case 2:days+=31;
case 1:days+=day;
}
if(isLeap&&month>2){//如果是闰年且月份大于2,则2月份为29天
days++;//days天数+1
}
System.out.println("这一天是那一年的第"+days+"天");
if(isTrue){//前提是合法日期
if(day==31){
day=1;
if(month==12){//当12月31号时,需要year+1,
year++;
month=1;
}else{//其他能达到31号的月份 月份+1
month++;
}
}else if(day==30){//与上同理
if(month==4||month==6||month==9||month==11){
month++;
day=1;
}else{
day++;
}
}else if(day==29){
if(month==2){//因为前提是合法日期,能到达2月29下一天肯定为3月1号
month=3;
day=1;
}else{
day++;
}
}else if(day==28){
if(!isLeap){//非闰年
if(month==2){//非闰年的2月28号为2月的最后一天
month=3;
day=1;
}
}else{//其他月份28号都不是最后一天,day+1即可
day++;
}
}
}
System.out.print("这一天的下一天是"+year+"年"+month+"月"+day+"日");
}
}
运行结果如下所示: