{"id":3749,"date":"2019-01-17T14:39:22","date_gmt":"2019-01-17T06:39:22","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=3749"},"modified":"2019-01-17T14:39:22","modified_gmt":"2019-01-17T06:39:22","slug":"simpledateformat-%e7%ba%bf%e7%a8%8b%e5%ae%89%e5%85%a8%e9%97%ae%e9%a2%98%e8%ae%a9%e6%88%91%e4%bb%ac%e7%a9%bf%e8%b6%8a%e5%88%b0%e8%bf%9c%e5%8f%a4%e5%92%8c%e6%9c%aa%e6%9d%a5","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=3749","title":{"rendered":"SimpleDateFormat \u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u8ba9\u6211\u4eec\u7a7f\u8d8a\u5230\u8fdc\u53e4\u548c\u672a\u6765"},"content":{"rendered":"<p>\n\t\u6700\u8fd1\u4e00\u76f4\u5728\u5fd9\u4e8e\u6551\u706b\uff0c\u9646\u9646\u7eed\u7eed\u7684\u6709\u4e0d\u5c11\u7528\u6237\u53cd\u9988\u6211\u4eec\u7684\u7cfb\u7edf\u4f1a\u51fa\u9519\uff0c\u4e0b\u5355\u65f6\u95f4\u4e0d\u662f\u5728\u8fdc\u53e4\u5c31\u662f\u5728\u672a\u6765\u3002\u800c\u8d1f\u8d23\u540e\u53f0\u7ba1\u7406\u7cfb\u7edf\u7684\u5458\u5de5\u4e5f\u63d0\u51fa\u4e86\u8d28\u7591\uff0c\u8fd9\u4e2a\u8ba2\u5355\u4e0b\u5355\u65f6\u95f4\u662f 19xx \u5e74\uff0c\u8fd8\u6709\u8fd9\u4e2a\u8ba2\u5355\u521b\u5efa\u65f6\u95f4\u662f 2187 \u5e74\uff0c\u8fd9\u4e9b\u90fd\u662f\u975e\u6b63\u5e38\u7684\u8ba2\u5355\uff0c\u662f\u4e0d\u662f\u6709\u4eba\u653b\u51fb\u6211\u4eec\uff1f\u8fd9\u4e9b\u8ba2\u5355\u6211\u4eec\u662f\u5426\u8981\u53d6\u6d88\uff1f\n<\/p>\n<p>\n\t\u4e00\u8fde\u4e32\u7684\u95ee\u9898\uff0c\u5fae\u4fe1\u5de5\u5355\u7fa4\u91cc\u90fd\u4e0a\u5343\u6761\u6d88\u606f\u4e86\uff0c\u4e0d\u5f97\u5df2\uff0c\u5927\u534a\u591c\u7684\u8d77\u6765\u67e5\u770b\u670d\u52a1\u5668\u65e5\u5fd7\u3002\u539f\u6765\u662f SimpleDateFormat \u5728\u6363\u9b3c\u3002\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"SimpleDateFormat \u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/wx1.sinaimg.cn\/large\/8fa5dcfcgy1fz9j7ohcecj20bn07fjsb.jpg\" style=\"width: 419px;height: 267px\" \/>\n<\/p>\n<p>\n\tSimpleDateFormat \u7ebf\u7a0b\u5b89\u5168\u662f\u4e00\u4e2a\u8001\u751f\u5e38\u8c08\u7684\u95ee\u9898\u4e86\u3002\u4e3a\u4ec0\u4e48\u8fd8\u53d1\u751f\u5728\u6211\u4eec\u516c\u53f8\uff1f\u4e3a\u4ec0\u4e48\u6d4b\u8bd5\u6ca1\u6d4b\u51fa\u6765\uff1f\u7b49\u7b49\u8fd9\u4e9b\u95ee\u9898\uff0c\u5c5e\u4e8e\u7ba1\u7406\u95ee\u9898\uff0c\u6211\u4e0d\u5728\u7ec6\u8bf4\u3002\u4eca\u5929\u6211\u4e3b\u8981\u6765\u590d\u724c\uff0c\u5185\u90e8\u57f9\u8bad\uff0c\u6211\u4e5f\u62ff\u51fa\u8fd9\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u770b\u7684\u3002\n<\/p>\n<h2>\n\t\u4ec0\u4e48\u662f\u7ebf\u7a0b\u5b89\u5168\uff1f<br \/>\n<\/h2>\n<p>\n\t\u5efa\u8bae\u9605\u8bfb\u6211\u7684\u8fd9\u7bc7\u6587\u7ae0\u300a<a href=\"https:\/\/www.xttblog.com\/?p=3326\">Java \u7ebf\u7a0b\u5b89\u5168\u76843\u5927\u6838\u5fc3\uff1a\u539f\u5b50\u6027\u3001\u53ef\u89c1\u6027\u3001\u6709\u5e8f\u6027<\/a>\u300b\u3002\n<\/p>\n<h2>\n\tSimpleDateFormat \u7ebf\u7a0b\u5b89\u5168\u95ee\u9898<br \/>\n<\/h2>\n<p>\n\t\u5728\u963f\u91cc\u5df4\u5df4\u7684 Java \u5f00\u53d1\u624b\u518c\u4e2d\uff0c\u5df2\u7ecf\u660e\u786e\u7684\u5199\u660e\u4e86\u7981\u6b62\u4f7f\u7528 static \u7684&nbsp;SimpleDateFormat\uff0c\u4f46\u662f\u6211\u4eec\u8fd8\u6709\u4eba\u7528\u3002\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"\u963f\u91cc\u5df4\u5df4java\u5f00\u53d1\u624b\u518c SimpleDateFormat\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/wx1.sinaimg.cn\/large\/8fa5dcfcgy1fz9jgrdoy9j20u002qwf2.jpg\" style=\"width: 1080px;height: 98px\" \/>\n<\/p>\n<p>\n\t\u6211\u4ece\u65e5\u5fd7\u4e2d\u770b\u5230\u4e86\u975e\u5e38\u591a\u7684 java.lang.NumberFormatException: multiple points \u5f02\u5e38\u4fe1\u606f\u3002\u6253\u5370\u7684\u65e5\u671f\u6709 0014-01-17\uff0c2214-01-17 \u7b49\u3002\u5b9a\u4f4d\u5230\u76f8\u5173\u4ee3\u7801\u540e\uff0c\u5148\u8ba9\u4e1a\u52a1\u9650\u5236\u5728\u591c\u95f4\u8fdb\u884c\u79d2\u6740\u3001\u6d3b\u52a8\u63a8\u5e7f\u7b49\u64cd\u4f5c\uff0c\u76f4\u5230\u7b2c\u4e8c\u5929\u95ee\u9898\u88ab\u4fee\u590d\uff0c\u91cd\u65b0\u53d1\u7248\u3002\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"SimpleDateFormat \u975e\u7ebf\u7a0b\u5b89\u5168\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/wx1.sinaimg.cn\/large\/8fa5dcfcgy1fz9jnh4wfmj20m5063wgi.jpg\" style=\"width: 797px;height: 219px\" \/>\n<\/p>\n<h2>\n\t\u95ee\u9898\u4ee3\u7801<br \/>\n<\/h2>\n<p>\n\t\u4e3a\u4e86\u8bb2\u6e05\u695a\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4e13\u95e8\u7b80\u5316\u4e86\u4e1a\u52a1\u903b\u8f91\uff0c\u6574\u7406\u4e86\u5173\u952e\u70b9\u7ed9\u5404\u4f4d\u540c\u4e8b\u548c\u7f51\u53cb\u6f14\u793a\u3002\n<\/p>\n<p>\n\t\u9996\u5148\uff0c\u6211\u4eec\u6709\u4e00\u4e2a&nbsp;DateUtil \u7684\u5de5\u5177\u7c7b\uff0c\u5185\u5bb9\u5927\u81f4\u5982\u4e0b\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\n\/**\r\n * DateUtil\r\n * @author xtt\r\n * @date 2019\/1\/17 \u4e0b\u53481:14\r\n *\/\r\npublic class DateUtil {\r\n    private static SimpleDateFormat sdf = new SimpleDateFormat(&quot;yyyy-MM-dd&quot;);\r\n    private static SimpleDateFormat timeSdf = new SimpleDateFormat(&quot;yyyy-MM-dd HH:mm:ss&quot;);\r\n    public static String xttblog(String timeString) throws ParseException {\r\n        Date date = timeSdf.parse(timeString);\r\n        return sdf.format(date);\r\n    }\r\n}<\/pre>\n<p>\n\t\u7136\u540e\u5728\u79d2\u6740\u7b49\u6d3b\u52a8\u4e0b\u5355\u65f6\uff0c\u521b\u5efa\u8ba2\u5355\u6709\u4e00\u4e2a\u683c\u5f0f\u5316\u65f6\u95f4\u64cd\u4f5c\uff0c\u8c03\u7528\u4e86 DateUtil \u7c7b\u3002\u4ee3\u7801\u7b80\u5316\u5982\u4e0b\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\npackage com.xttblog.test;\r\nimport java.util.concurrent.CountDownLatch;\r\n\/**\r\n * SimpleDateFormatTest\r\n * @author xtt\r\n * @date 2019\/1\/17 \u4e0b\u53481:14\r\n *\/\r\npublic class SimpleDateFormatTest {\r\n    static int threadCount = 20;\r\n    final static CountDownLatch latch = new CountDownLatch(threadCount);\r\n    public static void main(String[] args) {\r\n        for (int i = 0;i &lt; threadCount; i++){\r\n            new Thread(){\r\n                public void run() {\r\n                    try {\r\n                        System.out.println(&quot;\u5b50\u7ebf\u7a0b &quot; + Thread.currentThread().getName() + &quot; \u6b63\u5728\u6267\u884c&quot;);\r\n                        System.out.println(DateUtil.xttblog(&quot;2019-01-17 14:00:00&quot;));\r\n                        System.out.println(&quot;\u5b50\u7ebf\u7a0b &quot; + Thread.currentThread().getName() + &quot; \u6267\u884c\u5b8c\u6bd5&quot;);\r\n                        latch.countDown();\r\n                    } catch (Exception e) {\r\n                        e.printStackTrace();\r\n                    }\r\n                };\r\n            }.start();\r\n        }\r\n    }\r\n}<\/pre>\n<p>\n\t\u8fd9\u624d\u76f8\u5f53\u4e8e 20 \u4e2a\u5e76\u53d1\uff0c\u5e76\u4e0d\u9ad8\u3002\u5b9e\u9645\u4e0a\u53ea\u6709\u4e24\u4e2a 2 \u7ebf\u7a0b\u5e76\u53d1\u65f6\uff0c\u4e5f\u4f1a\u5076\u5c14\u53d1\u751f\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u3002\n<\/p>\n<h2>\n\t\u4e3a\u4ec0\u4e48&nbsp;SimpleDateFormat \u4e0d\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff1f<br \/>\n<\/h2>\n<p>\n\t\u6211\u4eec\u5148\u6765\u770b\u4e00\u5f20\u7c7b\u56fe\uff1a\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"SimpleDateFormat \u7c7b\u7684\u7ed3\u6784\u56fe\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/wx1.sinaimg.cn\/large\/8fa5dcfcgy1fz9jxs5ntxj20h0093t8p.jpg\" style=\"width: 612px;height: 327px\" \/>\n<\/p>\n<p>\n\t\u518d\u7ed3\u5408&nbsp;SimpleDateFormat \u7c7b\u7684\u6e90\u7801\uff0c\u53ef\u77e5\u6bcf\u4e2a SimpleDateFormat \u5b9e\u4f8b\u91cc\u9762\u6709\u4e00\u4e2a Calendar \u5bf9\u8c61\uff0cSimpleDateFormat \u4e4b\u6240\u4ee5\u662f\u7ebf\u7a0b\u4e0d\u5b89\u5168\u7684\u5c31\u662f\u56e0\u4e3a Calendar \u662f\u7ebf\u7a0b\u4e0d\u5b89\u5168\u7684\uff0c\u540e\u8005\u4e4b\u6240\u4ee5\u662f\u7ebf\u7a0b\u4e0d\u5b89\u5168\u7684\u662f\u56e0\u4e3a\u5176\u4e2d\u5b58\u653e\u65e5\u671f\u6570\u636e\u7684\u53d8\u91cf\u90fd\u662f\u7ebf\u7a0b\u4e0d\u5b89\u5168\u7684\uff0c\u6bd4\u5982\u91cc\u9762\u7684 fields\uff0ctime \u7b49\u3002\n<\/p>\n<p>\n\t\u8bf4\u767d\u4e86\uff0cparse \u5e76\u4e0d\u662f\u4e00\u4e2a\u539f\u5b50\u6027\u7684\u64cd\u4f5c\u3002\u5f53\u51fa\u73b0\u4e0b\u9762\u7684\u60c5\u51b5\u65f6\uff0c\u5c31\u4f1a\u53d1\u751f\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u3002\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"Calendar \u7ebf\u7a0b\u4e0d\u5b89\u5168\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/wx1.sinaimg.cn\/large\/8fa5dcfcgy1fz9k3hkj1rj20ic04zt9x.jpg\" style=\"width: 660px;height: 179px\" \/>\n<\/p>\n<h2>\n\t\u89e3\u51b3\u65b9\u6848<br \/>\n<\/h2>\n<p>\n\t\u8fd9\u7c7b\u95ee\u9898\u7684\u89e3\u51b3\u65b9\u6848\u6709\u5f88\u591a\uff0c\u5927\u5bb6\u6309\u7167\u963f\u91cc\u5df4\u5df4 java \u5f00\u53d1\u89c4\u8303\u4e2d\u63a8\u8350\u7684\u7528\u6cd5\u53bb\u7528\u5373\u53ef\u3002\u6211\u7684\u6848\u4f8b\u4ee3\u7801\uff0c\u53ea\u9700\u8981\u7a0d\u4f5c\u8c03\u6574\u5373\u53ef\u3002\n<\/p>\n<pre class=\"brush:java;\">\r\npublic class DateUtil {\r\n    private static ThreadLocal&lt;SimpleDateFormat&gt; sdf = new ThreadLocal&lt;SimpleDateFormat&gt;(){\r\n        @Override\r\n        protected SimpleDateFormat initialValue(){\r\n            return new SimpleDateFormat(&quot;yyyy-MM-dd&quot;);\r\n        }\r\n    };\r\n    private static ThreadLocal&lt;SimpleDateFormat&gt; timeSdf = new ThreadLocal&lt;SimpleDateFormat&gt;(){\r\n        @Override\r\n        protected SimpleDateFormat initialValue(){\r\n            return new SimpleDateFormat(&quot;yyyy-MM-dd HH:mm:ss&quot;);\r\n        }\r\n    };\r\n    public static String xttblog(String timeString) throws ParseException {\r\n        Date date = timeSdf.get().parse(timeString);\r\n        return sdf.get().format(date);\r\n    }\r\n}<\/pre>\n<p>\n\t\u8fd0\u884c\u6548\u679c\u5982\u4e0b\uff1a\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"ThreadLocal\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/wx1.sinaimg.cn\/large\/8fa5dcfcgy1fz9kp48w1jj20c80630tp.jpg\" style=\"width: 440px;height: 219px\" \/>\n<\/p>\n<p>\n\t\u4f60\u8bf4\u8fd9\u4e2a\u95ee\u9898\u96be\u5417\uff1f\u4e00\u70b9\u4e5f\u4e0d\u96be\u5427\uff0c\u96be\u7684\u662f\u4e00\u4e9b\u4eba\u4e0d\u613f\u610f\u53bb\u5b66\u4e60\uff0c\u5fc5\u987b\u8981\u63a8\u7740\u8f6c\uff0c\u5f3a\u8feb\u4ed6\u4eec\u3002\u51fa\u4e86\u95ee\u9898\u4e86\uff0c\u9996\u5148\u627e\u522b\u4eba\uff0c\u8d23\u4efb\u63a8\u7684\u4e00\u5e72\u4e8c\u51c0\u3002<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=3749\">SimpleDateFormat \u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u8ba9\u6211\u4eec\u7a7f\u8d8a\u5230\u8fdc\u53e4\u548c\u672a\u6765<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u4e00\u76f4\u5728\u5fd9\u4e8e\u6551\u706b\uff0c\u9646\u9646\u7eed\u7eed\u7684\u6709\u4e0d\u5c11\u7528\u6237\u53cd\u9988\u6211\u4eec\u7684\u7cfb\u7edf\u4f1a\u51fa\u9519\uff0c\u4e0b\u5355\u65f6\u95f4\u4e0d\u662f\u5728\u8fdc\u53e4\u5c31\u662f\u5728\u672a\u6765\u3002\u800c\u8d1f\u8d23\u540e\u53f0\u7ba1\u7406\u7cfb\u7edf [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[565,983,532,202,1104],"class_list":["post-3749","post","type-post","status-publish","format-standard","hentry","category-java","tag-java-","tag-shiro-","tag-threadlocal","tag-locked","tag-1104"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3749","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3749"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3749\/revisions"}],"predecessor-version":[{"id":3750,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3749\/revisions\/3750"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3749"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3749"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3749"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}