日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java 日期是否合法_检测日期字符串是否为合法(java版)

發布時間:2023/12/1 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 日期是否合法_检测日期字符串是否为合法(java版) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1 /**

2 * 檢測日期字符串是否為合法3 *@paramdateStr4 *@paramformat5 *@return

6 */

7 public static final boolean checkDateFormat(String dateStr,String format) throwsNumberFormatException{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, intpos,String identifier){142 if(str.substring(pos,pos+1).equals(identifier)) return true;143 return false;144 }

總結

以上是生活随笔為你收集整理的java 日期是否合法_检测日期字符串是否为合法(java版)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。