{"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>\u65e5\u4ed8<\/strong>\u306e\u59a5\u5f53\u6027\u30fb\u5b58\u5728\u30c1\u30a7\u30c3\u30af <strong>Calendar.setLenient()<\/strong><\/a><\/h2>\n<p>\n<strong>Java<\/strong>\u3067<strong>\u65e5\u4ed8\u306e\u59a5\u5f53\u6027\u30fb\u5b58\u5728\u30c1\u30a7\u30c3\u30af<\/strong>\u3092\u884c\u3046\u30b5\u30f3\u30d7\u30eb\u30d7\u30ed\u30b0\u30e9\u30e0\u3067\u3059\u3002<br \/>\n\u6307\u5b9a\u3057\u305f\u65e5\u4ed8\u6587\u5b57\u5217\uff08yyyy\/MM\/dd or yyyy-MM-dd\uff09\u304c\u30ab\u30ec\u30f3\u30c0\u30fc\u4e0a\u306b\u5b58\u5728\u3059\u308b\u304b\u3069\u3046\u304b\u3092\u30c1\u30a7\u30c3\u30af\u3057\u307e\u3059\u3002<\/p>\n<p><strong>java.text.DateFormat<\/strong>\u30af\u30e9\u30b9\u306e<strong>setLenient()<\/strong>\u306bfalse\u3092\u6307\u5b9a\u3059\u308b\u3068\u3001\u65e5\u4ed8\u89e3\u6790\u3092\u53b3\u5bc6\u306b\u884c\u3046\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\uff08\u203bDateFormat\u30af\u30e9\u30b9\u306f\u5185\u90e8\u7684\u306b\u306f<strong>java.util.Calendar\u30af\u30e9\u30b9\u306esetLenient()<\/strong>\u3092\u547c\u3073\u51fa\u3057\u3066\u3044\u307e\u3059\u3002\uff09<br \/>\n\u4e0d\u6b63\u306a\u65e5\u4ed8\u3084\u3001\u5b58\u5728\u3057\u306a\u3044\u65e5\u4ed8\u3067parse()\u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3059\u3068\u3001ParseException\u304c\u767a\u751f\u3059\u308b\u3053\u3068\u3092\u5229\u7528\u3057\u305f\u30c1\u30a7\u30c3\u30af\u65b9\u6cd5\u3068\u306a\u308a\u307e\u3059\u3002\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\">\u30b5\u30f3\u30d7\u30eb\u30d7\u30ed\u30b0\u30e9\u30e0<\/a><\/h2>\n<pre class=\"sample_src\">\/**\r\n * \u65e5\u4ed8\u306e\u59a5\u5f53\u6027\u30c1\u30a7\u30c3\u30af\u3092\u884c\u3044\u307e\u3059\u3002\r\n * \u6307\u5b9a\u3057\u305f\u65e5\u4ed8\u6587\u5b57\u5217\uff08yyyy\/MM\/dd or yyyy-MM-dd\uff09\u304c\r\n * \u30ab\u30ec\u30f3\u30c0\u30fc\u306b\u5b58\u5728\u3059\u308b\u304b\u3069\u3046\u304b\u3092\u8fd4\u3057\u307e\u3059\u3002\r\n * @param strDate \u30c1\u30a7\u30c3\u30af\u5bfe\u8c61\u306e\u6587\u5b57\u5217\r\n * @return \u5b58\u5728\u3059\u308b\u65e5\u4ed8\u306e\u5834\u5408true\r\n *\/\r\npublic static boolean checkDate(String strDate) {\r\n    if (strDate == null || strDate.length() != 10) {\r\n        throw new IllegalArgumentException(\r\n                \"\u5f15\u6570\u306e\u6587\u5b57\u5217[\"+ strDate +\"]\" +\r\n                \"\u306f\u4e0d\u6b63\u3067\u3059\u3002\");\r\n    }\r\n    strDate = strDate.replace('-', '\/');\r\n    DateFormat format = DateFormat.getDateInstance();\r\n    \/\/ \u65e5\u4ed8\/\u6642\u523b\u89e3\u6790\u3092\u53b3\u5bc6\u306b\u884c\u3046\u304b\u3069\u3046\u304b\u3092\u8a2d\u5b9a\u3059\u308b\u3002\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\">\u5b9f\u884c\u7d50\u679c<\/a><\/h2>\n<p>\u25c6\u5b9f\u884c\u4f8b<\/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>\u25c6\u51fa\u529b\u7d50\u679c<\/p>\n<pre class=\"console\">true\r\nfalse\r\nfalse\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Java \u65e5\u4ed8\u306e\u59a5\u5f53\u6027\u30fb\u5b58\u5728\u30c1\u30a7\u30c3\u30af Calendar.setLenient() Java\u3067\u65e5\u4ed8\u306e\u59a5\u5f53\u6027\u30fb\u5b58\u5728 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"swell_btn_cv_data":"","footnotes":""},"categories":[19],"tags":[],"class_list":["post-960","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/posts\/960","targetHints":{"allow":["GET"]}}],"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}]}}