{"id":105,"date":"2023-02-18T08:11:01","date_gmt":"2023-02-18T00:11:01","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=105"},"modified":"2023-02-18T08:12:02","modified_gmt":"2023-02-18T00:12:02","slug":"java-determine-string-is-date-format","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/18\/java-determine-string-is-date-format\/","title":{"rendered":"Java\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u4e3a\u65e5\u671f\u683c\u5f0f"},"content":{"rendered":"<h3>\u7eaf\u6b63\u5219\u5224\u65ad<\/h3>\n<pre><code class=\"language-java\">\/**\n * \u529f\u80fd\uff1a\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u4e3a\u65e5\u671f\u683c\u5f0f\n *\n * @param strDate\n * @return\n *\/\npublic static boolean isDate(String strDate) {\n    Pattern pattern = Pattern.compile(&quot;^((\\\\d{2}(([02468][048])|([13579][26]))[\\\\-\\\\\/\\\\s]?((((0?[13578])|(1[02]))[\\\\-\\\\\/\\\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\\\-\\\\\/\\\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\\\-\\\\\/\\\\s]?((0?[1-9])|([1-2][0-9])))))|(\\\\d{2}(([02468][1235679])|([13579][01345789]))[\\\\-\\\\\/\\\\s]?((((0?[13578])|(1[02]))[\\\\-\\\\\/\\\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\\\-\\\\\/\\\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\\\-\\\\\/\\\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\\\s((([0-1]?[0-9])|(2?[0-3]))\\\\:([0-5]?[0-9])((\\\\s)|(\\\\:([0-5]?[0-9])))))?$&quot;);\n    Matcher m = pattern.matcher(strDate);\n    if (m.matches()) {\n        return true;\n    } else {\n        return false;\n    }\n}<\/code><\/pre>\n<pre><code class=\"language-java\">System.out.println(isDate(&quot;2013-05-01&quot;));  \/\/true\nSystem.out.println(isDate(&quot;1900-05-01&quot;));  \/\/true\nSystem.out.println(isDate(&quot;20130501&quot;));  \/\/true\nSystem.out.println(isDate(&quot;2013\/5\/1&quot;));  \/\/true\nSystem.out.println(isDate(&quot;15\/5\/1&quot;));  \/\/false\nSystem.out.println(isDate(&quot;05\/01&quot;));  \/\/false\nSystem.out.println(isDate(&quot;2013\/6\/31&quot;));  \/\/false<\/code><\/pre>\n<h3>\u6b63\u5219\u4e0eSimpleDateFormat\u7ed3\u5408<\/h3>\n<pre><code class=\"language-java\">public static boolean isDate(String strDate) {\n    String regex = &quot;[0-9]{4}-[0-9]{2}-[0-9]{2}&quot;;\n    Pattern pattern = Pattern.compile(regex);\n    Matcher m = pattern.matcher(strDate);\n    boolean dateFlag = m.matches();\n    if (!dateFlag) {\n        return false;\n    }\n    DateFormat formatter = new SimpleDateFormat(&quot;yyyy-MM-dd&quot;);\n    \/\/\u8bbe\u7f6elenient\u4e3afalse\uff0c\u5426\u5219SimpleDateFormat\u4f1a\u6bd4\u8f83\u5bbd\u677e\u5730\u9a8c\u8bc1\u65e5\u671f\uff0c\u6bd4\u59822007\/02\/29\u4f1a\u88ab\u63a5\u53d7\uff0c\u5e76\u8f6c\u6362\u62102007\/03\/01\n    formatter.setLenient(false); \/\/\u9ed8\u8ba4\u503ctrue,\u4e0d\u4e25\u683c\u89e3\u6790\u65e5\u671f,\u4f1a\u81ea\u52a8\u8ba1\u7b97\n    try {\n        Date date = formatter.parse(strDate);\n        log.info(date);\n        return true;\n    } catch (Exception e) {\n        log.error(e);\n    }\n    return false;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7eaf\u6b63\u5219\u5224\u65ad \/** * \u529f\u80fd\uff1a\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u4e3a\u65e5\u671f\u683c\u5f0f * * @param strDate * @retur [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[],"class_list":["post-105","post","type-post","status-publish","format-standard","hentry","category-java-basic"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/105","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=105"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/105\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=105"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=105"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=105"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}