思路:
1、取出年月日
2、判断月份是否合法
3、判断日期是否合法
import java.util.Scanner;
public class Demo1 {
public static void main(String[] args) {
Scanner sca = new Scanner(System.in);
System.out.println("请输入日期字符串:");
String date = sca.next();
//分别取出年月日
int year = Integer.parseInt("" + date.charAt(0) + date.charAt(1) + date.charAt(2) + date.charAt(3));
int month = Integer.parseInt("" + date.charAt(5) + date.charAt(6));
int day = Integer.parseInt("" + date.charAt(8) + date.charAt(9));
boolean flag = true;
if(month < 1 || month > 12) {
flag = false;
}else{
if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
if(day >= 1 && day <= 31) {
flag = true;
}else{
flag = false;
}
}else if(month == 4 || month == 6 || month == 9 || month == 11) {
if(day >= 1 && day <= 30) {
flag = true;
}else{
flag = false;
}
}else{
if(year % 400 == 0 || year % 4 == 0 && year % 100 != 0) {
if(day >= 1 && day <= 29) {
flag = true;
}else{
flag = false;
}
}else{
if(day >= 1 && day <= 28) {
flag = true;
}else{
flag = false;
}
}
}
}
if(flag) {
System.out.println("合法日期");
}else{
System.out.println("非法日期");
}
}
}