检测日期字符串是否为合法(java版)
1 /** 2 * 检测日期字符串是否为合法 3 * @param dateStr 4 * @param format 5 * @return 6 */ 7 public static final boolean checkDateFormat(String dateStr,String format) throws NumberFormatException{ 8 if(dateStr == null || "".equals(dateStr) ) return false; 9 if(dateStr.length() != format.length()){ 10 //alert(objName+"长度和格式("+format+")要求的不一致,请重新输入"); 11 return false; 12 } 13 Integer year=null,month=null ,day=null,hour=null,minute= null,second= null; 14 15 if( "YYYY".equals(format)){ 16 year = Integer. valueOf(dateStr); 17 } else if( "YYYYMM".equals(format)){ 18 year = Integer.valueOf(dateStr.substring(0,4)); 19 month = Integer.valueOf(dateStr.substring(4,6)); 20 } else if( "YYYYMMDD".equals(format)){ 21 year = Integer.valueOf(dateStr.substring(0,4)); 22 month = Integer.valueOf(dateStr.substring(4,6)); 23 day = Integer.valueOf(dateStr.substring(6,8)); 24 } else if( "YYYYMMDD HH:MI".equals(format)){ 25 year = Integer.valueOf(dateStr.substring(0,4)); 26 month = Integer.valueOf(dateStr.substring(4,6)); 27 day = Integer.valueOf(dateStr.substring(6,8)); 28 hour = Integer.valueOf(dateStr.substring(9,11)); 29 minute = Integer.valueOf(dateStr.substring(12,14)); 30 } else if( "YYYYMMDD HH:MI:SS".equals(format)){ 31 year = Integer.valueOf(dateStr.substring(0,4)); 32 month = Integer.valueOf(dateStr.substring(4,6)); 33 day = Integer.valueOf(dateStr.substring(6,8)); 34 hour = Integer.valueOf(dateStr.substring(9,11)); 35 minute = Integer.valueOf(dateStr.substring(12,14)); 36 second = Integer.valueOf(dateStr.substring(15,17)); 37 } else if( "YYYY-MM".equals(format)){ 38 if(! _checkSpecial(dateStr,4,"-")) return false; 39 year = Integer.valueOf(dateStr.substring(0,4)); 40 month = Integer.valueOf(dateStr.substring(5,7)); 41 } else if( "YYYY-MM-DD".equals(format)){ 42 if(! _checkSpecial(dateStr,4,"-")) return false; 43 if(! _checkSpecial(dateStr,7,"-")) return false; 44 year = Integer.valueOf(dateStr.substring(0,4)); 45 month = Integer.valueOf(dateStr.substring(5,7)); 46 day = Integer.valueOf(dateStr.substring(8,10)); 47 } else if( "YYYY-MM-DD HH:MI:SS".equals(format)){ 48 if(! _checkSpecial(dateStr,4,"-")) return false; 49 if(! _checkSpecial(dateStr,7,"-")) return false; 50 year = Integer.valueOf(dateStr.substring(0,4)); 51 month = Integer.valueOf(dateStr.substring(5,7)); 52 day = Integer.valueOf(dateStr.substring(8,10)); 53 hour = Integer.valueOf(dateStr.substring(11,13)); 54 minute = Integer.valueOf(dateStr.substring(14,16)); 55 second = Integer.valueOf(dateStr.substring(17,19)); 56 } else if( "YYYYMMDDHHMISS".equals(format)){ 57 year = Integer.valueOf(dateStr.substring(0,4)); 58 month = Integer.valueOf(dateStr.substring(4,6)); 59 day = Integer.valueOf(dateStr.substring(6,8)); 60 hour = Integer.valueOf(dateStr.substring(8,10)); 61 minute = Integer.valueOf(dateStr.substring(10,12)); 62 second = Integer.valueOf(dateStr.substring(12,14)); 63 } else{ 64 //alert(objName+"中定义的时间格式还不能处理!"); 65 return false; 66 } 67 68 //check year 69 if(year != null){ 70 if(year<1900 || year>2200){ 71 //alert(objName+" 年份 应介于1900与2200之间,请重新输入!"); 72 return false; 73 } 74 75 } 76 //check month 77 if(month != null){ 78 if(month<1 || month >12){ 79 //alert(objName+" 月份 应介于1与12之间,请重新输入!"); 80 return false; 81 } 82 } 83 //check day 84 if(day != null){ 85 if((day==0)||(day>31)){ 86 //alert(objName+" 日 必须介于1与31之间!"); 87 return false; 88 } 89 else if(day>28 && day<31){ 90 if(month==2){ 91 if(day!=29){ 92 //alert(objName+year+"年"+month+"月无"+day+"日。"); 93 return false; 94 } 95 else { 96 if((year%4)!=0){ 97 //alert(objName+year+"年"+month+"月无"+day+"日。"); 98 return false ; 99 } 100 else { 101 if((year%100==0)&&(year%400!=0)){ 102 //alert(objName+year+"年"+month+"月无"+day+"日。"); 103 return false ; 104 } 105 } 106 } 107 } 108 } 109 110 else if(day==31){ 111 if((month==2)||(month==4)||(month==6)||(month==9)||(month==11)){ 112 //alert(objName+month+"月无"+day+"日"); 113 return false; 114 } 115 } 116 } 117 //check hour 118 if(hour != null){ 119 if(hour<0 || hour >23){ 120 //alert(objName+"小时应介于0与23之间,请重新输入!"); 121 return false; 122 } 123 } 124 //check minute 125 if(minute != null){ 126 if(minute<0 || minute >59){ 127 //alert(objName+"小时应介于0与59之间,请重新输入!"); 128 return false; 129 } 130 } 131 //check second 132 if(second != null){ 133 if(second<0 || second >59){ 134 //alert(objName+"秒应介于0与59之间,请重新输入!"); 135 return false; 136 } 137 } 138 return true; 139 } 140 141 private static boolean _checkSpecial(String str, int pos,String identifier){ 142 if(str.substring(pos,pos+1).equals(identifier)) return true ; 143 return false; 144 }