"*/importjava.util.Scanner;//用来存储被选中的日期classDate{privateintyear;privateintmonth;privateintday;//构造器publicDate(intyear,intmonth,intday){this.year=year;this.month=month;this.day=day;}//判断输入的日期是否合法publicbooleanisAValidDate(){booleanflag=false;if(month>0&&month<13){switch(month){case2://2月份有两种天数(闰年和非闰年)if(year%4==0&&year%100!=0||year%400==0){if(day>0&&day<30){flag=true;}}else{if(day>0&&day<29){flag=true;}}break;case4://4、6、9、11月份只有30天case6:case9:case11:if(day>0&&day<31){flag=true;}break;default://其他月份含31天if(day>0&&day<32){flag=true;}break;}}returnflag;}//当输入的日期合法,根据用户的选择生成日期publicStringgenerateNextDate(intchoose){switch(choose){case1://将日期往后一天推算if(day==31&&month==12){year=year+1;month=1;day=1;}elseif(day==31&&(month==1||month==3||month==5||month==7||month==8||month==10)){month=month+1;day=1;}elseif(day==30&&(month==4||month==6||month==9||month==11)){month=month+1;day=1;}elseif((day==28&&month==2&&year%4==0&&year%100!=0||year%400==0)){month=2;day=29;}elseif(day==28&&month==2){month=3;day=1;}else{day=day+1;}break;case2://将日期往前推算一天if(day==1&&month==1){year=year-1;month=12;day=31;}elseif(day==1&&(month==2||month==4||month==6||month==8||month==9||month==11||month==1)){month=month-1;day=31;}elseif(day==1&&(month==5||month==7||month==10||month==12)){month=month-1;day=30;}elseif(day==1&&month==3&&year%4==0&&year%100!=0||year%400==0){month=2;day=29;}elseif(day==1&&month==3){month=2;day=28;}else{day=day-1;}break;default://默认返回该日期break;}return""+year+"年"+month+"月"+day+"日";}}publicclassdate{publicstaticvoidmain(String[]args){Scannerin=newScanner(System.in);System.out.print("请输入一个日期(年、月、日):");intyear=in
Copyright © 2025