{"id":960,"date":"2018-09-17T18:51:38","date_gmt":"2018-09-17T09:51:38","guid":{"rendered":"https:\/\/chat-messenger.com\/?p=960"},"modified":"2018-12-23T01:55:16","modified_gmt":"2018-12-22T16:55:16","slug":"dateformat-setlenient","status":"publish","type":"post","link":"https:\/\/chat-messenger.com\/vi\/blog\/java\/dinh-dang-ngay-thang","title":{"rendered":"Ki\u1ec3m tra t\u00ednh h\u1ee3p l\u1ec7\/t\u1ed3n t\u1ea1i c\u1ee7a ng\u00e0y Java"},"content":{"rendered":"<h2 class=\"common_title\"><a name=\"0\"><strong>Java<\/strong> <strong>ng\u00e0y<\/strong>Ki\u1ec3m tra t\u00ednh h\u1ee3p l\u1ec7\/t\u1ed3n t\u1ea1i <strong>Calendar.setLenient()<\/strong><\/a><\/h2>\n<p>\n<strong>Java<\/strong>TRONG<strong>Ki\u1ec3m tra t\u00ednh h\u1ee3p l\u1ec7\/t\u1ed3n t\u1ea1i c\u1ee7a ng\u00e0y<\/strong>\u0110\u00e2y l\u00e0 m\u1ed9t ch\u01b0\u01a1ng tr\u00ecnh m\u1eabu th\u1ef1c hi\u1ec7n \u0111i\u1ec1u n\u00e0y.<br \/>\nKi\u1ec3m tra xem chu\u1ed7i ng\u00e0y \u0111\u01b0\u1ee3c ch\u1ec9 \u0111\u1ecbnh (yyyy\/MM\/dd ho\u1eb7c yyyy-MM-dd) c\u00f3 t\u1ed3n t\u1ea1i tr\u00ean l\u1ecbch hay kh\u00f4ng.<\/p>\n<p><strong>java.text.DateFormat<\/strong>l\u1edbp h\u1ecdc<strong>setLenient()<\/strong>B\u1eb1ng c\u00e1ch ch\u1ec9 \u0111\u1ecbnh sai, b\u1ea1n c\u00f3 th\u1ec3 th\u1ef1c hi\u1ec7n ph\u00e2n t\u00edch ng\u00e0y th\u00e1ng m\u1ed9t c\u00e1ch nghi\u00eam ng\u1eb7t. (*L\u1edbp DateFormat n\u1eb1m trong n\u1ed9i b\u1ed9<strong>setLenient() trong l\u1edbp java.util.Calendar<\/strong>\u0111ang g\u1ecdi. )<br \/>\nPh\u01b0\u01a1ng th\u1ee9c ki\u1ec3m tra n\u00e0y l\u1ee3i d\u1ee5ng th\u1ef1c t\u1ebf l\u00e0 n\u1ebfu b\u1ea1n g\u1ecdi ph\u01b0\u01a1ng th\u1ee9c parse() v\u1edbi ng\u00e0y kh\u00f4ng h\u1ee3p l\u1ec7 ho\u1eb7c ng\u00e0y kh\u00f4ng t\u1ed3n t\u1ea1i, th\u00ec ParseException s\u1ebd x\u1ea3y ra.\n<\/p>\n<p><script type=\"text\/javascript\" src=\"https:\/\/chat-messenger.com\/js\/common.js?dd12sssas2223\" charset=\"UTF-8\"><\/script><br \/>\n<script type=\"text\/javascript\"> writePR(); <\/script><\/p>\n<h2 class=\"common_title\"><a name=\"1\">ch\u01b0\u01a1ng tr\u00ecnh m\u1eabu<\/a><\/h2>\n<pre class=\"sample_src\">\/** * Th\u1ef1c hi\u1ec7n ki\u1ec3m tra t\u00ednh h\u1ee3p l\u1ec7 c\u1ee7a ng\u00e0y. * Tr\u1ea3 v\u1ec1 xem chu\u1ed7i ng\u00e0y \u0111\u00e3 ch\u1ec9 \u0111\u1ecbnh (yyyy\/MM\/dd hay yyyy-MM-dd) * c\u00f3 t\u1ed3n t\u1ea1i trong l\u1ecbch hay kh\u00f4ng. * @param strDate Chu\u1ed7i \u0111\u1ec3 ki\u1ec3m tra * @return true n\u1ebfu ng\u00e0y t\u1ed3n t\u1ea1i *\/ public static boolean checkDate(String strDate) { if (strDate == null || strDate.length() != 10) { n\u00e9m IllegalArgumentException m\u1edbi (&quot;\u0110\u1ed1i s\u1ed1) chu\u1ed7i [&quot;+ strDate +&quot;]&quot; + &quot;kh\u00f4ng h\u1ee3p l\u1ec7.&quot;); } strDate = strDate.replace(&#039;-&#039;, &#039;\/&#039;); \u0110\u1ecbnh d\u1ea1ng DateFormat = DateFormat.getDateInstance(); \/\/ \u0110\u1eb7t xem c\u00f3 th\u1ef1c hi\u1ec7n ng\u00e0y nghi\u00eam ng\u1eb7t hay kh\u00f4ng \/ph\u00e2n t\u00edch th\u1eddi gian. format.setLenient(false); th\u1eed { format.parse(strDate); return true; } Catch (Ngo\u1ea1i l\u1ec7 e) { return false; } }\r\n<\/pre>\n<h2 class=\"common_title\"><a name=\"2\">K\u1ebft qu\u1ea3 th\u1ef1c hi\u1ec7n<\/a><\/h2>\n<p>\u25c6V\u00ed d\u1ee5 th\u1ef1c thi<\/p>\n<pre class=\"sample_src\">public static void main(String[] args) { System.out.println(checkDate(&quot;2007-01-01&quot;)); System.out.println(checkDate(&quot;2007\/02\/31&quot;)); System.out .println(checkDate(&quot;aaaa\/02\/31&quot;)); }\r\n<\/pre>\n<p>\u25c6K\u1ebft qu\u1ea3 \u0111\u1ea7u ra<\/p>\n<pre class=\"console\">\u0111\u00fang sai sai\r\n<\/pre>","protected":false},"excerpt":{"rendered":"<p>Ki\u1ec3m tra t\u00ednh h\u1ee3p l\u1ec7\/t\u1ed3n t\u1ea1i c\u1ee7a ng\u00e0y Java Calendar.setLenient() Ki\u1ec3m tra t\u00ednh h\u1ee3p l\u1ec7\/t\u1ed3n t\u1ea1i c\u1ee7a ng\u00e0y trong Java [\u2026]<\/p>","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"swell_btn_cv_data":""},"categories":[19],"tags":[],"_links":{"self":[{"href":"https:\/\/chat-messenger.com\/vi\/wp-json\/wp\/v2\/posts\/960"}],"collection":[{"href":"https:\/\/chat-messenger.com\/vi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chat-messenger.com\/vi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chat-messenger.com\/vi\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/chat-messenger.com\/vi\/wp-json\/wp\/v2\/comments?post=960"}],"version-history":[{"count":6,"href":"https:\/\/chat-messenger.com\/vi\/wp-json\/wp\/v2\/posts\/960\/revisions"}],"predecessor-version":[{"id":9055,"href":"https:\/\/chat-messenger.com\/vi\/wp-json\/wp\/v2\/posts\/960\/revisions\/9055"}],"wp:attachment":[{"href":"https:\/\/chat-messenger.com\/vi\/wp-json\/wp\/v2\/media?parent=960"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chat-messenger.com\/vi\/wp-json\/wp\/v2\/categories?post=960"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chat-messenger.com\/vi\/wp-json\/wp\/v2\/tags?post=960"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}