import java.util.Scanner;
/**
* 给定一个日期,求后一天的日期
*
* @author lm吹梦到西洲
* @date 2018/5/6
*
*/
public class NextDate {
int year, month, day;
public void getDay(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 9:
case 11:
if (day < 1 || day > 31) {
System.out.println("天数在1-31之间!");
System.exit(0);
} else if (day <= 30) {
day++;
} else if (day == 31) {
month++;
day = 1;
}
System.out.println(year + "/" + month + "/" + day);
break;
case 12:
if (day < 1 || day > 31) {
System.out.println("天数在1-31之间!");
System.exit(0);
} else if (day <= 30) {
day++;
} else if (day == 31) {
year++;
month = 1;
day = 1;
}
System.out.println(year + "/" + month + "/" + day);
break;
case 4:
case 6:
case 8:
case 10:
if (day < 1 || day > 30) {
System.out.println("天数在1-30之间!");
System.exit(0);
} else if (day <= 29) {
day++;
} else if (day == 30) {
month++;
day = 1;
System.out.println(year + "/" + month + "/" + day);
}
break;
case 2:
if (day < 1 || day > 29) {
System.out.println("天数在1-29之间!");
System.exit(0);
} else if (day <= 27) {
day++;
} else if (day == 28) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
day++;
} else {
month++;
day = 1;
}
} else if (day == 29) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
month++;
day = 1;
} else {
System.out.println("平年2月没有29天!");
System.exit(0);
}
}
System.out.println(year + "/" + month + "/" + day);
break;
default:
System.out.println("月数在1-12之间!");
}
}
}
class Test {
public static void main(String[] args) {
NextDate a = new NextDate();
Scanner date = new Scanner(System.in);
System.out.println("请输入年份:");
int year = date.nextInt();
System.out.println("请输入月份:");
int month = date.nextInt();
System.out.println("请输入天数:");
int day = date.nextInt();
System.out.println("您输入的日期为:" + year + "/" + month + "/" + day);
System.out.print("后一天的日期为:");
a.getDay(year, month, day);
}
}
运行如下 请输入年份:
2018
请输入月份:
5
请输入天数:
6
您输入的日期为:2018/5/6
后一天的日期为:2018/5/7
——————————————
请输入年份:
2008
请输入月份:
2
请输入天数:
29
您输入的日期为:2008/2/29
后一天的日期为:2008/3/1
——————————————– 请输入年份:
2009
请输入月份:
2
请输入天数:
29
您输入的日期为:2009/2/29
后一天的日期为:平年2月没有29天!