{"id":4787,"date":"2020-02-22T23:09:01","date_gmt":"2020-02-22T15:09:01","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=4787"},"modified":"2020-02-22T23:09:01","modified_gmt":"2020-02-22T15:09:01","slug":"java%e4%b8%ad%e5%a6%82%e4%bd%95%e6%a0%a1%e9%aa%8c%e4%b8%80%e4%b8%aa%e6%97%a5%e6%9c%9f%e6%98%af%e5%90%a6%e6%ad%a3%e7%a1%ae%e4%b8%94%e5%90%88%e6%b3%95","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/4787.html","title":{"rendered":"Java\u4e2d\u5982\u4f55\u6821\u9a8c\u4e00\u4e2a\u65e5\u671f\u662f\u5426\u6b63\u786e\u4e14\u5408\u6cd5"},"content":{"rendered":"\n<p>=Start=<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u7f18\u7531\uff1a<\/h4>\n\n\n\n<p>\u6574\u7406\u4e00\u4e0bJava\u4e2d\u6821\u9a8c\u4e00\u4e2a\u65e5\u671f\u662f\u5426\u6b63\u786e\u4e14\u5408\u6cd5\u7684\u65b9\u6cd5\uff0c<strong>\u7f51\u4e0a\u5173\u4e8e\u8fd9\u7c7b\u529f\u80fd\u7684\u4ee3\u7801\u5f88\u591a\uff0c\u4f46\u8d28\u91cf\u53c2\u5dee\u4e0d\u9f50\uff0c\u4e14\u4e0d\u4fdd\u8bc1\u6b63\u786e\u6027<\/strong>\uff0c\u6240\u4ee5\u81ea\u5df1\u5728\u6b64\u6574\u7406\u4e00\u4e0b\u81ea\u5df1\u9a8c\u8bc1\u8fc7\u7684\u5185\u5bb9\uff0c\u65b9\u4fbf\u9700\u8981\u7684\u65f6\u5019\u53c2\u8003\u548c\u4f7f\u7528\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6b63\u6587\uff1a<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n\n\n\n<p>\u53ef\u884c\u7684\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>\u81ea\u5df1\u6309\u7167\u539f\u7406\u8fdb\u884c\u7f16\u5199<\/strong><\/li><li><strong>SimpleDateFormat + setLenient(false)<\/strong><\/li><li>LocalDate (\u9700\u8981Java 8\u53ca\u4ee5\u4e0a\u7248\u672c)<\/li><li><strong>Apache Commons Validator \u5305<\/strong><\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.example.learn;\n\nimport org.apache.commons.validator.GenericValidator;\n\nimport java.text.DateFormat;\nimport java.text.ParseException;\nimport java.text.SimpleDateFormat;\nimport java.time.DateTimeException;\nimport java.time.LocalDate;\nimport java.time.format.DateTimeFormatter;\nimport java.time.format.DateTimeParseException;\nimport java.time.format.ResolverStyle;\n\npublic class DateUtils {\n\n    public static String dateArray[] = {\"2020-02-28\",\"2020-02-29\",\"2020-02-30\",\"2020-02-31\",\"2020-01-31\",\"2019-02-29\"};\n    public static String datePattern = \"yyyy-MM-dd\";\n\n    \/\/ Key is setLenient(false);\n    public static boolean isValidDate(String dateString, String datePattern) {\n        try {\n            DateFormat df = new SimpleDateFormat(datePattern);\n            df.setLenient(false);\n            df.parse(dateString);\n            return true;\n        } catch (ParseException e) {\n            return false;\n        }\n    }\n\n    \/\/ The java.time framework is built into Java 8 and later.\n    public static boolean isValidDate2(String dateString) {\n        boolean dateIsValid = true;\n        try {\n            int year = Integer.parseInt(dateString.substring(0,4));\n            int month = Integer.parseInt(dateString.substring(5,7));\n            int day = Integer.parseInt(dateString.substring(8,10));\n            \/\/ System.out.println(String.format(\"%d\\t%d\\t%d\", year, month, day));\n            LocalDate.of(year, month, day);\n        } catch (DateTimeException e) {\n            dateIsValid = false;\n        }\n        return dateIsValid;\n    }\n\n    \/\/ The java.time framework is built into Java 8 and later.\n    \/\/ Error case:\n    \/\/ 2020-02-30: true\n    \/\/ 2020-02-31: true\n    \/\/ 2019-02-29: true\n    public static boolean isValidDateFormat(String dateString, String pattern) {\n        boolean valid = true;\n        \/\/ DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);\n        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern).withResolverStyle(ResolverStyle.STRICT);\n        try {\n            formatter.parse(dateString);\n        } catch (DateTimeParseException e) {\n            valid = false;\n        }\n        return valid;\n    }\n\n\n    static int MAX_VALID_YR = 9999;\n    static int MIN_VALID_YR = 1900;\n\n    \/\/ Judge is \u95f0\u5e74(leap year) or not ?\n    static boolean isLeap(int year) {\n        \/\/ Return true if year is a multiple of 4 and not multiple of 100.\n        \/\/ OR year is multiple of 400.\n        return (((year % 4 == 0) &amp;&amp;\n                (year % 100 != 0)) ||\n                (year % 400 == 0));\n    }\n\n    \/\/ Returns true if given year+month+day is valid, or false(not valid).\n    static boolean isValidDate3(int y, int m, int d) {\n        \/\/ If year, month and day \/\/ are not in given range\n        if (y > MAX_VALID_YR || y &lt; MIN_VALID_YR)\n            return false;\n        if (m &lt; 1 || m > 12)\n            return false;\n        if (d &lt; 1 || d > 31)\n            return false;\n\n        \/\/ Handle February month with leap year\n        if (m == 2) {\n            if (isLeap(y))\n                return (d &lt;= 29);\n            else\n                return (d &lt;= 28);\n        }\n\n        \/\/ Months of April, June, Sept and Nov must have\n        \/\/ number of days less than or equal to 30.\n        if (m == 4 || m == 6 || m == 9 || m == 11)\n            return (d &lt;= 30);\n\n        return true;\n    }\n\n\n    public static void main(String[] args) {\n        System.out.println(\"SimpleDateFormat-setLenient\");\n        for (String dateStr: dateArray) {\n            System.out.println(String.format(\"%s: %b\", dateStr, isValidDate(dateStr, datePattern)));\n        }\n\n        System.out.println(\"\\njava.time.LocalDate\");\n        for (String dateStr: dateArray) {\n            System.out.println(String.format(\"%s: %b\", dateStr, isValidDate2(dateStr)));\n        }\n\n        System.out.println(\"\\ncommons-validator\");\n        for (String dateStr: dateArray) {\n            System.out.println(String.format(\"%s: %b\", dateStr, GenericValidator.isDate(dateStr, datePattern, true)));\n        }\n\n        System.out.println(\"\\nvia default judgement\");\n        for (String dateStr: dateArray) {\n            int year = Integer.parseInt(dateStr.substring(0,4));\n            int month = Integer.parseInt(dateStr.substring(5,7));\n            int day = Integer.parseInt(dateStr.substring(8,10));\n            System.out.println(String.format(\"%s: %b\", dateStr, isValidDate3(year, month, day)));\n        }\n\n        System.out.println(\"\\nDateTimeFormatter\");\n        for (String dateStr: dateArray) {\n            System.out.println(String.format(\"%s: %b\", dateStr, isValidDateFormat(dateStr, datePattern)));\n        }\n\n    }\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h5 class=\"wp-block-heading\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n\n\n\n<p>Program to check if a date is valid or not<br> <a href=\"https:\/\/www.geeksforgeeks.org\/program-check-date-valid-not\/\">https:\/\/www.geeksforgeeks.org\/program-check-date-valid-not\/<\/a><\/p>\n\n\n\n<p>Check If a String Is a Valid Date in Java<br> <a href=\"https:\/\/www.baeldung.com\/java-string-valid-date\">https:\/\/www.baeldung.com\/java-string-valid-date<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/github.com\/eugenp\/tutorials\/tree\/master\/core-java-modules\/core-java-datetime-string\">https:\/\/github.com\/eugenp\/tutorials\/tree\/master\/core-java-modules\/core-java-datetime-string<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/mvnrepository.com\/artifact\/commons-validator\/commons-validator\/1.6\">https:\/\/mvnrepository.com\/artifact\/commons-validator\/commons-validator\/1.6<\/a><\/p>\n\n\n\n<p>Java\u4e2d\u5982\u4f55\u6821\u9a8c\u4e00\u4e2a\u65e5\u671f\u662f\u5426\u6b63\u786e\uff1f<br> <a href=\"https:\/\/stackoverflow.com\/questions\/226910\/how-to-sanity-check-a-date-in-java\">https:\/\/stackoverflow.com\/questions\/226910\/how-to-sanity-check-a-date-in-java<\/a><br> <a href=\"https:\/\/stackoverflow.com\/a\/4528094\">https:\/\/stackoverflow.com\/a\/4528094<\/a><br> <a href=\"https:\/\/stackoverflow.com\/a\/23438158\">https:\/\/stackoverflow.com\/a\/23438158<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/util\/Calendar.html#setLenient(boolean)\">https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/util\/Calendar.html#setLenient(boolean)<\/a><\/p>\n\n\n\n<p>How to check if date is valid in Java<br> <a href=\"https:\/\/mkyong.com\/java\/how-to-check-if-date-is-valid-in-java\/\">https:\/\/mkyong.com\/java\/how-to-check-if-date-is-valid-in-java\/<\/a><\/p>\n\n\n\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u6574\u7406\u4e00\u4e0bJava\u4e2d\u6821\u9a8c\u4e00\u4e2a\u65e5\u671f\u662f\u5426\u6b63\u786e\u4e14\u5408\u6cd5\u7684\u65b9\u6cd5\uff0c\u7f51\u4e0a\u5173\u4e8e\u8fd9\u7c7b\u529f\u80fd\u7684\u4ee3\u7801\u5f88\u591a\uff0c\u4f46\u8d28\u91cf [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7,12],"tags":[61,1564,1565,1563],"class_list":["post-4787","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","category-tools","tag-java","tag-localdate","tag-setlenient","tag-simpledateformat"],"views":5231,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4787","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=4787"}],"version-history":[{"count":2,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4787\/revisions"}],"predecessor-version":[{"id":4790,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4787\/revisions\/4790"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=4787"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=4787"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=4787"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}