1 package Test8_19;
2
3 import java.util.Scanner;
4
5 public class CheckDate {
6 public static void main(String args[]) {
7 CheckDate cd = new CheckDate();
8 Scanner reader = new Scanner(System.in);
9 while (true) {
10 System.out.println("Enter a date:");
11 String date = reader.nextLine();
12 if (cd.CheckDateFormat(date)) {
13 System.out.println("Yes");
14 } else {
15 System.out.println("No");
16
17 }
18 }
19 }
20
21 public boolean CheckDateFormat(String date) {
22 String regex = "[1-2]\\d{3}.[0-1][0-9].[0-3][0-9]";
23
24 if (date.matches(regex)) {
25 // get year month day of String
26 String year = date.substring(0, 4);
27 String month;
28 String day = date.substring(8,10);
29
30 if (date.substring(5, 6).equals("0")) {
31 month = date.substring(6, 7); // 0-9 month
32 } else {
33 month = date.substring(5, 7); // 10-12 month
34 }
35
36 if (date.substring(8, 9).equals("0")) {
37 day = date.substring(9, 10); // 0-9 day
38 } else {
39 day = date.substring(8, 10); // 10-31 day
40 }
41 // get year month day of int
42 int y = Integer.parseInt(year);
43 int m = Integer.parseInt(month);
44 int d = Integer.parseInt(day);
45 System.out.println("y="+y+" m="+m+" d="+d);
46
47 //if ((m >= 1 && m <= 12) && (d >= 1 && d <= 31)) {
48 switch(m){
49 case 2:if (isLeapYear(y)) {
50 if (d <= 29) {
51 return true;
52 } else {
53 return false;
54 }
55 }
56 else{ if (d <= 28) {
57 return true;
58 } else {
59 return false;
60 }
61 }
62
63 case 1:
64 case 3:
65 case 5:
66 case 7:
67 case 8:
68 case 10:
69 case 12:if (d <= 31) {
70 return true;
71 } else {
72 return false;
73 }
74 case 4:
75 case 6:
76 case 9:
77 case 11:if (d <= 30) {
78 return true;
79 } else {
80 return false;
81 }
82 default:return false;
83
84 }
85 }
86 else{
87 return false;
88 }
89 }
90
91
92
93 public boolean isLeapYear(int year) {
94 if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
95 return true;
96 } else {
97 return false;
98 }
99 }
100
101 }
改进:加入对当前日判断
1 public boolean accountDateOfOpening(String date) {
2 String regex = "[1-2]\\d{3}.[0-1][0-9].[0-3][0-9]";
3 String month1 = null;
4 String month;
5 if (date.matches(regex)) {
6 // get year month day of String
7 String year = date.substring(0, 4);
8 String day = date.substring(8,10);
9
10 if (date.substring(5, 6).equals("0")) {
11 month = date.substring(6, 7); // 0-9 month
12 month1= date.substring(5, 7);
13 } else {
14 month = date.substring(5, 7); // 10-12 month
15 }
16
17 String enterDate=year+month1+day;
18 Date nowdate=new Date();
19 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
20 String nowDateString=dateFormat.format(nowdate);
21 //System.out.println("enterdate: "+enterDate);
22 //System.out.println("nowDateString: "+nowDateString);
23 //System.out.println("compare: "+(enterDate.compareTo(nowDateString)>=0));
24 if(enterDate.compareTo(nowDateString)>=1){
25 System.out.println("Date cannot be more than today!");
26 return false;
27 }
28
29 if (date.substring(8, 9).equals("0")) {
30 day = date.substring(9, 10); // 0-9 day
31 } else {
32 day = date.substring(8, 10); // 10-31 day
33 }
34 // get year month day of int
35 int y = Integer.parseInt(year);
36 int m = Integer.parseInt(month);
37 int d = Integer.parseInt(day);
38 if(d==0){
39 return false;
40 }
41 switch(m){
42 case 2:if (isLeapYear(y)) {
43 if (d <= 29) {
44 return true;
45 } else {
46 return false;
47 }
48 }
49 else{ if (d <= 28) {
50 return true;
51 } else {
52 return false;
53 }
54 }
55 // break;
56 case 1:
57 case 3:
58 case 5:
59 case 7:
60 case 8:
61 case 10:
62 case 12:if (d <= 31) {
63 return true;
64 } else {
65 return false;
66 }
67 case 4:
68 case 6:
69 case 9:
70 case 11:if (d <= 30) {
71 return true;
72 } else {
73 return false;
74 }
75 default:return false;
76
77 }
78 }
79 else{
80 return false;
81 }
82 }
83
84
85 // year is Leap Year or not
86 public boolean isLeapYear(int year) {
87 if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
88 return true;
89 } else {
90 return false;
91 }
92 }