Java 日期判断

  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 }

Java 日期判断,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。