{"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\/en\/blog\/java\/dateformat-setlenient","title":{"rendered":"Java Date validity\/existence check"},"content":{"rendered":"<h2 class=\"common_title\"><a name=\"0\"><strong>Java<\/strong> <strong>Date<\/strong>Validity and existence check of <strong>Calendar.setLenient()<\/strong><\/a><\/h2>\n<p>\n<strong>Java<\/strong>indicates certainty, emphasis, etc.<strong>Date validity\/existence check<\/strong>The following is a sample program that performs<br \/>\nChecks if the specified date string (yyyy\/MM\/dd or yyyy-MM-dd) exists on the calendar.<\/p>\n<p><strong>DateFormat<\/strong>Class<strong>setLenient()<\/strong>to be false, date parsing can be performed strictly. (*DateFormat class internally uses the<strong>setLenient() in java.util.Calendar class<\/strong>call.)<br \/>\nThis method of checking is based on the fact that calling the parse() method with an invalid or nonexistent date will result in a ParseException.\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\">sample program<\/a><\/h2>\n<pre class=\"sample_src\">\/**\r\n * Performs a date validity check.\r\n * Returns whether the given date string (yyyy\/MM\/dd or yyyy-MM-dd)\r\n * Returns whether the given date string (yyyy\/MM\/dd or yyyy-MM-dd) exists in the calendar.\r\n * @param strDate String to be checked.\r\n * @return true if the date exists.\r\n *\/\r\npublic static boolean checkDate(String strDate) {\r\n    if (strDate == null || strDate.length() ! = 10) {\r\n        throw new IllegalArgumentException(\r\n                \"The argument string [\"+ strDate +\"]\" +\r\n                \"]\" is invalid.\") ;\r\n    }\r\n    strDate = strDate.replace('-', '\/');\r\n    DateFormat format = DateFormat.getDateInstance();\r\n    \/\/ Set whether date\/time parsing is strictly performed or not.\r\n    format.setLenient(false);\r\n    try {\r\n        format.parse(strDate);\r\n        return true;\r\n    } catch (Exception e) {\r\n        return false; }\r\n    }\r\n}\r\n<\/pre>\n<h2 class=\"common_title\"><a name=\"2\">Execution Result<\/a><\/h2>\n<p>\u25c6Example of Execution<\/p>\n<pre class=\"sample_src\">public static void main(String[] args) {\r\n    System.out.println(checkDate(\"2007-01-01\"));\r\n    System.out.println(checkDate(\"2007\/02\/31\"));\r\n    System.out.println(checkDate(\"aaaa\/02\/31\"));\r\n}\r\n<\/pre>\n<p>\u25c6Output result<\/p>\n<pre class=\"console\">true\r\nfalse\r\nfalse\r\n<\/pre>","protected":false},"excerpt":{"rendered":"<p>Java date validity\/existence check Calendar.setLenient() Date validity\/existence check in 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\/en\/wp-json\/wp\/v2\/posts\/960"}],"collection":[{"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/comments?post=960"}],"version-history":[{"count":6,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/posts\/960\/revisions"}],"predecessor-version":[{"id":9055,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/posts\/960\/revisions\/9055"}],"wp:attachment":[{"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/media?parent=960"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/categories?post=960"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/tags?post=960"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}