{"id":969,"date":"2019-04-22T18:55:54","date_gmt":"2019-04-22T09:55:54","guid":{"rendered":"https:\/\/chat-messenger.com\/?p=969"},"modified":"2019-10-19T13:22:04","modified_gmt":"2019-10-19T04:22:04","slug":"simpledateformat-parse","status":"publish","type":"post","link":"https:\/\/chat-messenger.com\/zh\/\u535a\u5ba2\/java\/\u7b80\u5316\u683c\u5f0f\u89e3\u6790","title":{"rendered":"\u5c06 Java \u65e5\u671f\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65e5\u671f\u7c7b\u578b"},"content":{"rendered":"<h2><strong>Java<\/strong> <strong>\u65e5\u671f\u5b57\u7b26\u4e32<\/strong>\u8868\u793a\u6e34\u671b\u3001\u559c\u6b22\u3001\u618e\u6068\u7b49\u7684\u5bf9\u8c61\u3002 <strong>java.util.Date.<\/strong> \u6309\u7c7b\u578b\u83b7\u53d6<\/h2>\n<p>\nJava \u4e2d\u7684\u65e5\u671f\u5b57\u7b26\u4e32\uff08\u683c\u5f0f\u4e3a yyyy\/MM\/dd <strong>java.util.Date.<\/strong> \u8f6c\u6362\u4e3a\u7c7b\u578b\u7684\u793a\u4f8b\u7a0b\u5e8f\u3002\n<\/p>\n<h2>\u793a\u4f8b\u4ee3\u7801<\/h2>\n<pre class=\"sample_src\">\r\n\/**\r\n * \u5c06\u65e5\u671f\u5b57\u7b26\u4e32 \"yyyy\/MM\/dd \"\u8f6c\u6362\u4e3a java.util.Date \u683c\u5f0f\u3002\r\n * @param str \u8981\u8f6c\u6362\u7684\u5b57\u7b26\u4e32\u3002\r\n * \u8f6c\u6362\u540e\u7684\u65e5\u671f\u5bf9\u8c61\u3002\r\n * @throws ParseException \u65e5\u671f\u5b57\u7b26\u4e32\u4e0d\u662f \"yyyy\/MM\/dd\"\u3002\r\n *\/\r\npublic static Date toDate(String str) throws ParseException {\r\n    Date date = DateFormat.getDateInstance().parse(str)\uff1b\r\n    \u8fd4\u56de date\uff1b\r\n}\r\n<\/pre>\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>\u6267\u884c\u7ed3\u679c<\/h2>\n<h4>\u793a\u4f8b\u7a0b\u5e8f<\/h4>\n<pre class=\"sample_src\">\r\npublic static void main(String[] args) {\r\n    try {\r\n        \/\/ \u6b63\u5e38\u6a21\u5f0f\r\n        Date date = toDate(\"2007\/01\/01\")\uff1b\r\n        System.out.println(date)\uff1b\r\n\r\n        \/\/ \u4e0d\u540c\u683c\u5f0f\u7684\u6a21\u5f0f\r\n        date = toDate(\"2007-01-01\")\uff1b\r\n        System.out.println(date)\uff1b\r\n    } catch (ParseException e) {\r\n        e.printStackTrace()\uff1b\r\n    }\r\n}\r\n<\/pre>\n<h4>\u8f93\u51fa\uff08\u7ed3\u679c\uff09<\/h4>\n<pre class=\"console\">01\/01\/2007='Mon Jan 01 00:00:00 JST 2007'\r\n<\/pre>\n<p>\u3000yyyy\/MM\/dd\u683c\u5f0f\u7684\u65e5\u671f\u5b57\u7b26\u4e32\u4f3c\u4e4e\u5df2\u7ecf\u6b63\u786e\u8f6c\u6362\uff0c\u4f46yyyy-MM-dd\u5462\uff1f<\/p>\n<pre class=\"console\">java.text.ParseException\uff1a\u65e0\u6cd5\u89e3\u6790\u7684\u65e5\u671f\uff1a\"2007-01-01\r\n    at java.text.DateFormat.parse(DateFormat.java:335)\r\n    at Main.toDate(DateUtil.java:627)\r\n    at Main.main(DateUtil.java:639)\r\n<\/pre>\n<p>\u3000\u5728 yyyy-MM-dd \u4e2d\u51fa\u73b0 ParseException\u3002<\/p>\n<p><strong>\u65e5\u671f\u683c\u5f0f<\/strong> \u7684\u4e00\u4e2a\u5b50\u7c7b <strong>java.text.SimpleDateFormat<\/strong> \u5e76\u5199\u51fa\u4ee5\u4e0b\u5185\u5bb9\u8fdb\u884c\u8f6c\u6362\u3001<\/p>\n<pre class=\"sample_src\">SimpleDateFormat format = new SimpleDateFormat(\"yyyy-MM-dd\")\uff1b\r\nformat.parse(\"2007-01-01\");  \r\n<\/pre>\n<p>\u4f60\u60f3\u8981\u4e00\u4e2a\u66f4\u901a\u7528\u7684\u65b9\u6848\u3002<br \/>\n\u4e0b\u4e00\u8282\u5c06\u4ecb\u7ecd\u5982\u4f55\u5c3d\u53ef\u80fd\u5c06\u4efb\u4f55\u65e5\u671f\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a java.util.Date \u7c7b\u578b\u3002<br \/>\n<a href=\"https:\/\/chat-messenger.com\/zh\/\u535a\u5ba2\/java\/\u6bcf\u4e2a\u65e5\u671f\u5b57\u7b26\u4e32\u5230\u65e5\u5386\/\">\u5c06\u4efb\u4f55\u65e5\u671f\/\u65f6\u95f4\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65e5\u671f\u6216\u65e5\u5386\u7c7b\u578b<\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>\u5c06 Java \u65e5\u671f\u5b57\u7b26\u4e32\u83b7\u53d6\u4e3a java.util.Date \u7c7b\u578b Java \u4e2d\u7684\u65e5\u671f\u5b57\u7b26\u4e32\uff08\u683c\u5f0f yyy [...]<\/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\/zh\/wp-json\/wp\/v2\/posts\/969"}],"collection":[{"href":"https:\/\/chat-messenger.com\/zh\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chat-messenger.com\/zh\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chat-messenger.com\/zh\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/chat-messenger.com\/zh\/wp-json\/wp\/v2\/comments?post=969"}],"version-history":[{"count":10,"href":"https:\/\/chat-messenger.com\/zh\/wp-json\/wp\/v2\/posts\/969\/revisions"}],"predecessor-version":[{"id":4620,"href":"https:\/\/chat-messenger.com\/zh\/wp-json\/wp\/v2\/posts\/969\/revisions\/4620"}],"wp:attachment":[{"href":"https:\/\/chat-messenger.com\/zh\/wp-json\/wp\/v2\/media?parent=969"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chat-messenger.com\/zh\/wp-json\/wp\/v2\/categories?post=969"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chat-messenger.com\/zh\/wp-json\/wp\/v2\/tags?post=969"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}