{"id":6877,"date":"2022-08-05T17:26:23","date_gmt":"2022-08-05T09:26:23","guid":{"rendered":"http:\/\/123.57.164.21\/?p=6877"},"modified":"2022-09-06T13:54:22","modified_gmt":"2022-09-06T05:54:22","slug":"java%e4%b8%ad-localdate%e3%80%81localtime%e3%80%81localdatetime%e4%b8%89%e4%b8%aa%e6%97%b6%e9%97%b4%e5%b7%a5%e5%85%b7%e7%b1%bb%e7%9a%84%e4%bd%bf%e7%94%a8%e4%bb%8b%e7%bb%8d","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=6877","title":{"rendered":"Java\u4e2d LocalDate\u3001LocalTime\u3001LocalDateTime\u4e09\u4e2a\u65f6\u95f4\u5de5\u5177\u7c7b\u7684\u4f7f\u7528\u4ecb\u7ecd"},"content":{"rendered":"\n<p><strong>\u4e00\u3001\u80cc\u666f\uff1a<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u4e4b\u524d\u5728\u505a\u9879\u76ee\u7684\u8fc7\u7a0b\u4e2d\uff0c\u5bf9\u65e5\u671f\u65f6\u95f4\u7c7b\u6ca1\u6709\u4e00\u4e2a\u7cfb\u7edf\u7684\u4e86\u89e3\uff0c\u603b\u662f\u5728\u7528\u7684\u65f6\u5019\u53bb\u641c\u7d22\u4e00\u4e0b\uff0c\u89e3\u51b3\u95ee\u9898\u5373\u5b8c\u4e8b\uff0c\u4e45\u800c\u4e45\u4e4b\uff0c\u5bfc\u81f4\u5bf9\u8fd9\u4e2a\u6982\u5ff5\u7279\u522b\u6a21\u7cca\u3002\u76f4\u5230\u8fd1\u671f\uff0c\u505a\u9879\u76ee\u7684\u8fc7\u7a0b\u4e2d\u4f7f\u7528\u4e86mybatis-plus\u6846\u67b6\uff0c\u8fd9\u4e2a\u6846\u67b6\u81ea\u52a8\u751f\u6210\u6620\u5c04\u6587\u4ef6\u7684\u5de5\u5177\u4f1a\u5c06MySQL\u4e2d\u7684datetime\u7c7b\u578b\u8f6c\u5316\u6210Java\u4e2d\u7684LocalDateTime\u7c7b\u578b\uff0c\u7531\u4e8e\u51e0\u6b21\u90fd\u51fa\u73b0\u4e86\u8f6c\u5316\u9519\u8bef\u3001\u8f6c\u5316\u7e41\u7410\u7684\u95ee\u9898\uff0c\u56e0\u6b64\uff0c\u5c31\u6253\u7b97\u8be6\u7ec6\u7684\u4e86\u89e3\u4e00\u4e0bJava\u4e2d\u7684\u65f6\u95f4\u7c7b\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u4e0b\u6b21\u518d\u4f7f\u7528\u80fd\u591f\u505a\u5230\u5fc3\u4e2d\u6709\u5e95\u3002\u7ecf\u8fc7\u4e86\u51e0\u5929\u7684\u4e86\u89e3\uff0c\u5bf9\u8fd9\u4e2a\u65f6\u95f4\u6982\u5ff5\u7b97\u662f\u6709\u4e86\u4e00\u4e2a\u5927\u81f4\u7684\u4e86\u89e3\uff0c\u8bb0\u5f55\u4e0b\u6765\u4f9b\u4ee5\u540e\u53c2\u8003\u3002\u770b\u6765\u89e3\u51b3\u95ee\u9898\u8fd8\u5f97\u8981\u6293\u4f4f\u95ee\u9898\u7684\u672c\u8d28\uff0c\u4e86\u89e3\u6280\u672f\u7684\u6765\u9f99\u53bb\u8109\uff0c\u4e0d\u80fd\u6d6e\u4e8e\u8868\u9762\u3002 (\u66f4\u591a\u5185\u5bb9\uff0c\u53ef\u53c2\u9605\u7a0b\u5e8f\u5458\u5728\u65c5\u9014)<\/p>\n\n\n\n<p><strong>\u4e8c\u3001\u4ecb\u7ecd\uff1a<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u5728JDK8\u53d1\u5e03\u7684\u65f6\u5019\uff0c\u63a8\u51fa\u4e86LocalDate\u3001LocalTime\u3001LocalDateTime\u8fd9\u4e2a\u4e09\u4e2a\u65f6\u95f4\u5904\u7406\u7c7b\uff0c\u4ee5\u6b64\u6765\u5f25\u8865\u4e4b\u524d\u7684\u65e5\u671f\u65f6\u95f4\u7c7b\u7684\u4e0d\u8db3\uff0c\u7b80\u5316\u65e5\u671f\u65f6\u95f4\u7684\u64cd\u4f5c\u3002\u5728Java8\u4e4b\u524d\uff0c\u5904\u7406\u65e5\u671f\u65f6\u95f4\u7684\u7c7b\u662fDate\u3001Calendar\uff0c\u8fd9\u4e24\u4e2a\u5728\u4f7f\u7528\u8d77\u6765\u603b\u662f\u8ba9\u4eba\u611f\u89c9\u4e0d\u662f\u5f88\u8212\u670d\uff0c\u5728\u8bbe\u8ba1\u4e0a\u9762\u6709\u4e00\u4e9b\u7f3a\u9677\uff0c\u5e76\u4e14\u8fd8\u4e0d\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002<\/p>\n\n\n\n<p>\u60f3\u8981\u8be6\u7ec6\u7684\u4e86\u89e3\u8fd9\u51e0\u4e2a\u7c7b\uff0c\u6211\u4eec\u5f97\u8981\u4e86\u89e3\u4e00\u70b9\u5173\u4e8e\u65f6\u95f4\u3001\u65f6\u533a\u5212\u5206\u7684\u4e00\u4e9b\u77e5\u8bc6\u30021884 \u5e74, \u56fd\u9645\u7ecf\u5ea6\u4f1a\u8bae\u5c06\u5730\u7403\u8868\u9762\u6309\u7ecf\u7ebf\u7b49\u5206\u4e3a24 \u533a,\u79f0\u4e3a\u65f6\u533a\u3002\u5373\u4ee5\u672c\u521d\u5b50\u5348\u7ebf\u4e3a\u57fa\u51c6, \u4e1c\u897f\u7ecf\u5ea6\u54047.5 \u5ea6\u7684\u8303\u56f4\u4f5c\u4e3a\u96f6\u65f6\u533a, \u7136\u540e\u6bcf\u969415\u5ea6\u4e3a\u4e00\u65f6\u533a\uff0c\u6bcf\u4e2a\u65f6\u533a\u76f8\u5dee\u4e00\u5c0f\u65f6\u3002\u5317\u4eac\u65f6\u533a\u4e3a\u4e1c\u516b\u533a\uff0c\u8981\u6bd4\u96f6\u65f6\u533a\u65e98\u4e2a\u5c0f\u65f6\u3002\u5982\u679c\u73b0\u5728\u96f6\u65f6\u533a\u7684\u65f6\u95f4\u662f10\u70b9\u7684\u8bdd\uff0c\u90a3\u5317\u4eac\u65f6\u95f4\u5c31\u662f18\u70b9\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u5e73\u65f6\u5728\u7a0b\u5e8f\u91cc\u9762\u6240\u89c1\u5230\u7684UTC\u65f6\u95f4\uff0c\u5c31\u662f\u96f6\u65f6\u533a\u7684\u65f6\u95f4\uff0c\u5b83\u7684\u5168\u79f0\u662fCoordinated Universal Time \uff0c\u5373\u4e16\u754c\u534f\u8c03\u65f6\u95f4\u3002\u53e6\u4e00\u4e2a\u5e38\u89c1\u7684\u7f29\u5199\u662fGMT\uff0c\u5373\u683c\u6797\u5a01\u6cbb\u6807\u51c6\u65f6\u95f4\uff0c\u683c\u6797\u5a01\u6cbb\u4f4d\u4e8e \u96f6\u65f6\u533a\uff0c\u56e0\u6b64\uff0c\u6211\u4eec\u5e73\u65f6\u8bf4\u7684UTC\u65f6\u95f4\u548cGMT\u65f6\u95f4\u5728\u6570\u503c\u4e0a\u9762\u90fd\u662f\u4e00\u6837\u7684\u3002<\/p>\n\n\n\n<p>\u53ef\u80fd\u4f1a\u6709\u4e00\u4e9b\u7591\u95ee\uff0c\u540c\u6837\u662f\u5728\u5730\u7403\u4e0a\u554a\uff0c\u4e0d\u540c\u7684\u5730\u533a\u65f6\u95f4\u600e\u4e48\u8fd8\u4e0d\u4e00\u6837\u4e86\uff1f\u96be\u9053\u6211\u73b0\u5728\u4ece\u5317\u4eac\u4e58\u4e2a\u98de\u673a\u98de\u5230\u683c\u6797\u5a01\u6cbb\uff0c\u8fd8\u53ef\u4ee5\u6765\u4e2a\u65f6\u5149\u5012\u6d41\uff1f\u5982\u679c\u8fd9\u6837\uff0c\u5c82\u4e0d\u5f88\u795e\u5947\uff1f\u54c8\u54c8\uff0c\u5f53\u7136\u4e8b\u60c5\u7684\u771f\u76f8\u5e76\u4e0d\u662f\u8fd9\u6837\u7684\u3002\u65f6\u95f4\u7684\u6d41\u901d\u3001\u7ec6\u80de\u7684\u8870\u8001\u5bf9\u4e8e\u6574\u4e2a\u5730\u7403\u6765\u8bf4\u90fd\u662f\u4e00\u6837\u7684\uff0c\u56e0\u6b64\uff0c\u4e0d\u7ba1\u4f60\u5728\u5730\u7403\u7684\u54ea\u91cc\uff0c\u8fd9\u4e00\u79d2\u8fc7\u53bb\u4e86\uff0c\u5c31\u662f\u8fc7\u53bb\u4e86\uff0c\u5b83\u6700\u4e3a\u516c\u5e73\uff0c\u4e0d\u4f1a\u591a\u4f60\u4e00\u5206\uff0c\u4e5f\u4e0d\u4f1a\u5c11\u4f60\u4e00\u79d2\u3002\u90a3\u600e\u4e48\u8bb0\u5f55\u8fd9\u6d41\u901d\u7684\u5206\u5206\u79d2\u79d2\u5462\uff1f\u6709\u4e00\u4e2a\u540d\u8bcd\uff0c\u53eb\u505a\u65f6\u95f4\u6233\uff0c\u5b83\u662f\u6307\u683c\u6797\u5a01\u6cbb\uff08\u5730\u7403\u96f6\u65f6\u533a\uff09\u65f6\u95f41970\u5e7401\u670801\u65e500\u65f600\u520600\u79d2(\u5317\u4eac\u65f6\u95f41970\u5e7401\u670801\u65e508\u65f600\u520600\u79d2)\u8d77\u81f3\u73b0\u5728\u7684\u603b\u79d2\u6570\uff0c\u8fd9\u4e2a\u65f6\u95f4\u6233\uff0c\u5728\u5730\u7403\u7684\u5404\u4e2a\u5730\u65b9\u90fd\u662f\u4e00\u81f4\u7684\u3002\u57281970\u5e74\u4ee5\u524d\uff0c\u6709\u5176\u4ed6\u7684\u8ba1\u65f6\u65b9\u5f0f\uff0c\u7531\u4e8e\u6ca1\u6709\u7edf\u4e00\uff0c\u8fd8\u9020\u6210\u4e86\u4e00\u4e9b\u8f6f\u4ef6\u7684\u8fd0\u884c\u5728\u65f6\u95f4\u4e0a\u7684\u9519\u4e71\uff0c\u9669\u4e9b\u917f\u6210\u4e86\u7684\u884c\u4e1a\u707e\u96be(\u5177\u4f53\u7684\u4e8b\u4ef6\u53ef\u4ee5\u7f51\u4e0a\u641c\u7d22)\u3002\u5982\u679c\u5355\u7eaf\u6307\u671b\u8fd9\u4e2a\u65f6\u95f4\u6233\u4f5c\u4e3a\u4eba\u4eec\u7684\u8ba1\u65f6\u6807\u51c6\uff0c\u90a3\u4e5f\u662f\u4e0d\u73b0\u5b9e\u7684\uff0c\u56e0\u4e3a\u8fd9\u5bf9\u4eba\u7c7b\u7684\u751f\u4ea7\u751f\u6d3b\u6765\u8bf4\u6ca1\u6709\u4efb\u4f55\u7684\u610f\u4e49\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff1a\u6bcf\u5929\u65e9\u4e0a\u516b\u70b9\u4e0a\u8bfe\uff08\u4e0a\u73ed\uff09\u7b26\u5408\u6211\u4eec\u7684\u4e60\u60ef\uff0c\u5982\u679c\u662f***\u79d2\u5230\u4e86\u4e0a\u8bfe\u65f6\u95f4\u5c31\u6709\u8fdd\u4eba\u7684\u6b63\u5e38\u8111\u56de\u8def\u4e86\u3002\u8fd9\u65f6\u5019\uff0c\u6839\u636e\u5730\u7403\u4e0a\u7684\u4e0d\u540c\u7ecf\u7eac\u5ea6\u3001\u5730\u7403\u81ea\u8f6c\u7684\u7279\u70b9\uff0c\u5c31\u5212\u5206\u4e86\u65f6\u533a\uff0c\u65f6\u95f4\u6233\u6839\u636e\u4e0d\u540c\u7684\u65f6\u533a\u8f6c\u5316\u6210\u5f53\u5730\u7684\u65f6\u95f4\uff0c\u4ee5\u6b64\u4f5c\u4e3a\u4f5c\u606f\u6807\u51c6\uff0c\u4ece\u800c\u65b9\u4fbf\u4eba\u4eec\u7684\u751f\u4ea7\u751f\u6d3b\u3002\u5317\u4eac\u65f6\u95f4\u76848\u70b9\u6211\u4eec\u53ef\u4ee5\u89c1\u5230\u592a\u9633\u7684\u5347\u8d77\uff0c\u4f26\u6566\u65f6\u95f4\u7684\u516b\u70b9\u4ed6\u4eec\u4e5f\u80fd\u89c1\u5230\u592a\u9633\u7684\u5347\u8d77\u3002<\/p>\n\n\n\n<p>\u5c31\u8fd9\u6837\uff0c\u53ef\u4ee5\u77e5\u9053\uff0c\u65f6\u95f4\u6233\u5bf9\u5730\u7403\u4e0a\u7684\u4efb\u4f55\u4e00\u4e2a\u5730\u65b9\u90fd\u662f\u4e00\u6837\u7684\uff0c\u5982\u679c\u6211\u4eec\u60f3\u8981\u628a\u65f6\u95f4\u6233\u8f6c\u5316\u6210\u5f53\u5730\u7684\u65f6\u95f4\uff0c\u5c31\u9700\u8981\u6839\u636e\u6240\u5728\u5730\u533a\u7684\u65f6\u533a\u8fdb\u884c\u8f6c\u5316\u3002\u4e0d\u540c\u65f6\u533a\u4e4b\u95f4\u8fdb\u884c\u65f6\u95f4\u8f6c\u5316\u4e5f\u662f\u4e00\u6837\u7684\u9053\u7406\uff0c\u6211\u4eec\u9700\u8981\u6839\u636e\u65f6\u533a\u7684\u5dee\u5f02\u6765\u8f6c\u5316\u5f53\u5730\u7684\u65f6\u95f4\u3002<\/p>\n\n\n\n<p><strong>\u4e09\u3001\u4f7f\u7528\u65b9\u6cd5\uff1a<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>&nbsp;3.1&nbsp; Java8 \u4ee5\u524d\u7684\u65f6\u95f4\u5904\u7406\uff1a<\/strong><\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp;1\uff0cDate\u7c7b\u7684\u4f7f\u7528\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import java.text.SimpleDateFormat;\nimport java.util.Date;\nimport java.util.TimeZone;\n \npublic class TestTime {\n\t\npublic static void main(String[] args) {\n\ttry {\n        \/\/\u83b7\u53d6Date\u5bf9\u8c61\uff0c\u5b58\u653e\u7684\u662f\u65f6\u95f4\u6233\n        Date date = new Date();\n        \/\/\u83b7\u53d6\u65f6\u95f4\u6233(\u6beb\u79d2)\n        long seconds = date.getTime();\n        System.out.println(\"\u5f53\u524d\u65f6\u95f4\u6233: \" + seconds);\n       \n        \/\/\u5f53\u524dGMT(\u683c\u6797\u5a01\u6cbb)\u65f6\u95f4\u3001\u5f53\u524d\u8ba1\u7b97\u673a\u7cfb\u7edf\u6240\u5728\u65f6\u533a\u7684\u65f6\u95f4\n        SimpleDateFormat beijingFormat = new SimpleDateFormat(\"yyyy-MM-dd HH:mm:ss\");\n        System.out.println(\"\u672c\u5730(\u4e1c\u516b\u533a)\u65f6\u95f4: \" + beijingFormat.format(date) +\"; GMT\u65f6\u95f4: \" + date.toGMTString());\n        \n        \/\/\u4e1c\u516b\u533a\u65f6\u95f4\u8f6c\u6362\u6210\u4e1c\u4e5d\u533a(\u4e1c\u4eac)\u65f6\u95f4,\u6bd4\u5317\u4eac\u65e9\u4e00\u4e2a\u5c0f\u65f6\n        SimpleDateFormat tokyoFormat = new SimpleDateFormat(\"yyyy-MM-dd HH:mm:ss\");\n        tokyoFormat.setTimeZone(TimeZone.getTimeZone(\"Asia\/Tokyo\"));\n        System.out.println(\"\u4e1c\u4eac(\u4e1c\u4e5d\u533a)\u65f6\u95f4: \"+tokyoFormat.format(date));\n        \n        \/\/\u65f6\u95f4\u6233\u8f6c\u5316\u6210Date\n        SimpleDateFormat timestampFormat = new SimpleDateFormat(\"yyyy-MM-dd HH:mm:ss\");\n        String fotmatString = timestampFormat.format(seconds);\n        Date parseDate = timestampFormat.parse(fotmatString);\n        System.out.println(\"\u65f6\u95f4\u6233\u8f6c\u5316\u6210Date\u4e4b\u540e\u7684\u65f6\u95f4: \"+parseDate + \";\u683c\u5f0f\u5316\u4e4b\u540e\u7684: \"+ fotmatString);\n\t            \n\t} catch (Exception e) {\n\t\te.printStackTrace();\n\t}\n}\n}<\/pre>\n\n\n\n<p>&nbsp;\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-34-1024x130.png\" alt=\"\" class=\"wp-image-6878\" width=\"625\" height=\"79\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-34-1024x130.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-34-300x38.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-34-768x97.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-34-1536x195.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-34-830x105.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-34-230x29.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-34-350x44.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-34-480x61.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-34.png 1642w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/figure><\/div>\n\n\n\n<p><br>Date\u7684\u65e0\u53c2\u6784\u9020\u51fd\u6570\u5982\u4e0b\u56fe\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-35-1024x908.png\" alt=\"\" class=\"wp-image-6879\" width=\"600\" height=\"531\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-35-1024x908.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-35-300x266.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-35-768x681.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-35-1536x1361.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-35-830x736.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-35-230x204.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-35-350x310.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-35-480x425.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-35.png 1636w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/figure><\/div>\n\n\n\n<p><br>\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0cDate\u4e2d\u5b58\u653e\u7684\u662f\u65f6\u95f4\u6233\uff0c\u56e0\u6b64\uff0c\u6211\u4eec\u5728\u4e16\u754c\u5404\u5730\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u83b7\u53d6\u5230\u7684\u503c\u90fd\u662f\u4e00\u6837\u7684\u3002\u53ea\u662f\u5728\u4e0d\u540c\u7684\u5730\u65b9\uff0c\u8fd9\u4e2a\u503c\u4f1a\u6839\u636e\u65f6\u533a\u8f6c\u5316\u6210\u5f53\u5730\u7684\u65f6\u95f4\u800c\u5df2\u3002<\/p>\n\n\n\n<p><strong>3.2&nbsp; Java8 \u4e4b\u540e\u7684\u65f6\u95f4\u5904\u7406\uff1a<\/strong><\/p>\n\n\n\n<p>\u4eceJava8\u5f00\u59cb\uff0c\u63a8\u51fa\u4e86LocalDate\u3001LocalTime\u3001LocalDateTime\u8fd9\u4e09\u4e2a\u5de5\u5177\u7c7b\uff0c\u5b9e\u73b0\u4e86\u66f4\u597d\u5730\u65f6\u95f4\u5904\u7406\uff0c\u6211\u4eec\u5148\u770b\u5982\u4f55\u4f7f\u7528\uff0c\u7136\u540e\uff0c\u518d\u8fdb\u884c\u6bd4\u8f83\u3002<\/p>\n\n\n\n<p>&nbsp;1\uff0c\u5de5\u5177\u7c7b\u7684\u83b7\u53d6\u4e0e\u4f7f\u7528\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import java.time.Instant;\nimport java.time.LocalDate;\nimport java.time.LocalDateTime;\nimport java.time.LocalTime;\nimport java.time.ZoneOffset;\nimport java.time.format.DateTimeFormatter;\n \npublic class TestLocalTime {\npublic static void main(String[] args) {\n\t\/\/\u83b7\u53d6\u5f53\u524d\u65f6\u533a\u7684\u65e5\u671f\n\tLocalDate localDate = LocalDate.now();\n\tSystem.out.println(\"localDate: \" + localDate);\n\t\/\/\u65f6\u95f4\n\tLocalTime localTime = LocalTime.now();\n\tSystem.out.println(\"localTime: \" + localTime);\n\t\/\/\u6839\u636e\u4e0a\u9762\u4e24\u4e2a\u5bf9\u8c61\uff0c\u83b7\u53d6\u65e5\u671f\u65f6\u95f4\n\tLocalDateTime localDateTime = LocalDateTime.of(localDate,localTime);\n\tSystem.out.println(\"localDateTime: \" + localDateTime);\n\t\/\/\u4f7f\u7528\u9759\u6001\u65b9\u6cd5\u751f\u6210\u6b64\u5bf9\u8c61\n\tLocalDateTime localDateTime2 = LocalDateTime.now();\n\tSystem.out.println(\"localDateTime2: \" + localDateTime2);\n\t\/\/\u683c\u5f0f\u5316\u65f6\u95f4\n\tDateTimeFormatter formatter = DateTimeFormatter.ofPattern(\"YYYY-MM-dd HH:mm:ss\");\n\tSystem.out.println(\"\u683c\u5f0f\u5316\u4e4b\u540e\u7684\u65f6\u95f4: \" + localDateTime2.format(formatter));\n\t\n\t\/\/\u8f6c\u5316\u4e3a\u65f6\u95f4\u6233(\u79d2)\n\tlong epochSecond = localDateTime2.toEpochSecond(ZoneOffset.of(\"+8\"));\n\t\/\/\u8f6c\u5316\u4e3a\u6beb\u79d2\n\tlong epochMilli = localDateTime2.atZone(ZoneOffset.systemDefault()).toInstant().toEpochMilli();\n\tSystem.out.println(\"\u65f6\u95f4\u6233\u4e3a:(\u79d2) \" + epochSecond + \"; (\u6beb\u79d2): \" + epochMilli);\n\t\n\t\/\/\u65f6\u95f4\u6233(\u6beb\u79d2)\u8f6c\u5316\u6210LocalDateTime\n\tInstant instant = Instant.ofEpochMilli(epochMilli);\n\tLocalDateTime localDateTime3 = LocalDateTime.ofInstant(instant, ZoneOffset.systemDefault());\n\tSystem.out.println(\"\u65f6\u95f4\u6233(\u6beb\u79d2)\u8f6c\u5316\u6210LocalDateTime: \" + localDateTime3.format(formatter));\n\t\/\/\u65f6\u95f4\u6233(\u79d2)\u8f6c\u5316\u6210LocalDateTime\n\tInstant instant2 = Instant.ofEpochSecond(epochSecond);\n\tLocalDateTime localDateTime4 = LocalDateTime.ofInstant(instant2, ZoneOffset.systemDefault());\n\tSystem.out.println(\"\u65f6\u95f4\u6233(\u79d2)\u8f6c\u5316\u6210LocalDateTime: \" + localDateTime4.format(formatter));\n}\n}<\/pre>\n\n\n\n<p>\u4e0a\u8ff0\u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-36-1024x334.png\" alt=\"\" class=\"wp-image-6880\" width=\"519\" height=\"168\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-36-1024x334.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-36-300x98.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-36-768x250.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-36-830x270.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-36-230x75.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-36-350x114.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-36-480x156.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-36.png 1154w\" sizes=\"(max-width: 519px) 100vw, 519px\" \/><\/figure><\/div>\n\n\n\n<p>\u65b0\u63a8\u51fa\u6765\u7684\u8fd9\u4e09\u4e2a\u7c7b\uff0c\u4e0eMySQL\u4e2d\u7684\u65e5\u671f\u65f6\u95f4\u7c7b\u578b\u6b63\u597d\u5bf9\u5e94\uff0c\u5982\u679c\u6211\u4eec\u4f7f\u7528MySQL\u6570\u636e\u5e93\u7684\u8bdd\uff0c\u5728\u63d2\u5165\u76f8\u5e94\u5b57\u6bb5\u7684\u65f6\u5019\uff0c\u90fd\u4e0d\u9700\u8981\u518d\u8fdb\u884c\u4efb\u4f55\u7684\u8f6c\u5316\u4e86\u3002\u5bf9\u5e94\u5173\u7cfb\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ul><li>&nbsp; &nbsp;LocalTime&nbsp; \u5bf9\u5e94&nbsp; time<\/li><li>&nbsp; &nbsp;LocalDate&nbsp; \u5bf9\u5e94&nbsp; date<\/li><li>&nbsp; &nbsp;LocalDateTime&nbsp; \u5bf9\u5e94&nbsp; <a rel=\"noreferrer noopener\" href=\"https:\/\/so.csdn.net\/so\/search?q=datetime&amp;spm=1001.2101.3001.7020\" target=\"_blank\">datetime<\/a>(timestamp)<\/li><\/ul>\n\n\n\n<p>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\uff08jdk\u8bfb\u53d6\u9ed8\u8ba4\u7cfb\u7edf\u65f6\u533a\uff09<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">LocalDateTime now = LocalDateTime.now();\nSystem.out.println(now.toString());\n\n\u8f93\u51fa\u7ed3\u679c\uff1a\n2022-07-23T12:42:44.183<\/pre>\n\n\n\n<p>\u83b7\u53d6\u6307\u5b9a\u65f6\u533a\u65f6\u95f4<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">LocalDateTime beijingTime = LocalDateTime.now(TimeZone.getTimeZone(\"Asia\/Shanghai\").toZoneId());\nLocalDateTime gmt8Time = LocalDateTime.now(TimeZone.getTimeZone(\"GMT+8\").toZoneId());\nLocalDateTime parisTime = LocalDateTime.now(TimeZone.getTimeZone(\"Europe\/Paris\").toZoneId());\nSystem.out.println(beijingTime.toString());\nSystem.out.println(gmt8Time);\nSystem.out.println(parisTime.toString());\n\n\u8f93\u51fa\u7ed3\u679c\uff1a\n2022-07-23T12:42:44.171\n2022-07-23T12:42:44.171\n2022-07-23T06:42:44.172<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\u83b7\u53d6\u6307\u5b9a\u65f6\u533a\u7684\u65f6\u95f4\u6709\u4e24\u79cd\u7528\u6cd5\uff1a\n1\u3001TimeZone.getTimeZone(\"Asia\/Shanghai\")\n2\u3001TimeZone.getTimeZone(\"GMT+8\")\n\u8fd9\u4e24\u79cd\u533a\u522b\u5728\u4e8e\u7b2c\u4e00\u79cd\u652f\u6301\u590f\u4ee4\u65f6\uff0c\u53ef\u4ee5\u83b7\u53d6\u590f\u4ee4\u65f6\u7684\u504f\u79fb\u91cf\u3002\n<\/pre>\n\n\n\n<p>\u83b7\u53d6\u6307\u5b9a\u683c\u5f0f\u7684\u65f6\u95f4<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">LocalDateTime now = LocalDateTime.now();\nSystem.out.println(now.format(DateTimeFormatter.ofPattern(\"yyyy\u5e74MM\u6708dd\u65e5 HH:mm:ss\")));\n\n\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a\n2022\u5e7407\u670823\u65e5 12:54:17<\/pre>\n\n\n\n<p>\u89e3\u6790\u6307\u5b9a\u683c\u5f0f\u7684\u65f6\u95f4<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">LocalDateTime parse = LocalDateTime.parse(\"2022\u5e7407\u670823\u65e5 12:54:17\", DateTimeFormatter.ofPattern(\"yyyy\u5e74MM\u6708dd\u65e5 HH:mm:ss\"));\nSystem.out.println(parse);\n\n\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a\n2022-07-23T12:54:17<\/pre>\n\n\n\n<p>\u8f93\u51fa\u6307\u5b9a\u65f6\u95f4<br>\u5982\uff1a\u8f93\u51fa2008\u5e7408\u70b908\u5206<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">LocalDateTime tem = LocalDateTime.of(2008, 8, 8, 8, 8, 8);\nSystem.out.println(tem);\n\n\u8f93\u51fa\u7ed3\u679c\uff1a\n2008-08-08T08:08:08<\/pre>\n\n\n\n<p>2\uff0c\u589e\u52a0\u3001\u51cf\u53bb\u65f6\u95f4\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-37-1024x374.png\" alt=\"\" class=\"wp-image-6881\" width=\"527\" height=\"192\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-37-1024x374.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-37-300x110.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-37-768x281.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-37-830x303.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-37-230x84.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-37-350x128.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-37-480x175.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-37.png 1522w\" sizes=\"(max-width: 527px) 100vw, 527px\" \/><\/figure><\/div>\n\n\n\n<p>\u770b\u770b\u4e0a\u56fe\uff0c\u662f\u4e0d\u662f\u975e\u5e38\u7684\u7b80\u5355\u65b9\u4fbf\u5462\u3002<\/p>\n\n\n\n<p>3\uff0cLocalDateTime \u4e0e Date \u7684\u76f8\u4e92\u8f6c\u5316<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ Date \u8f6c\u5316\u6210 LocalDateTime\npublic static LocalDateTime dateToLocalDate(Date date) {\n\tInstant instant = date.toInstant();\n\tZoneId zoneId = ZoneId.systemDefault();\n\treturn instant.atZone(zoneId).toLocalDateTime();\n}<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ LocalDateTime \u8f6c\u5316\u6210 Date\npublic static Date localDateTimeToDate(LocalDateTime localDateTime) {\n\tZoneId zoneId = ZoneId.systemDefault();\n\tZonedDateTime zdt = localDateTime.atZone(zoneId);\n\treturn Date.from(zdt.toInstant());\n}<\/pre>\n\n\n\n<p>\u7531\u4e8e LocalDate\u3001LocalTime \u6216\u8005\u53ea\u542b\u6709\u65e5\u671f\uff0c\u6216\u8005\u53ea\u542b\u6709\u65f6\u95f4\uff0c\u56e0\u6b64\uff0c\u4e0d\u80fd\u548cDate\u76f4\u63a5\u8fdb\u884c\u8f6c\u5316\u3002<\/p>\n\n\n\n<p><strong>\u56db\u3001\u603b\u7ed3\uff1a<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u4e86\u89e3\u597d\u65f6\u533a\uff0c\u65f6\u95f4\u6233\u7b49\u8fd9\u51e0\u4e2a\u6982\u5ff5\uff0c\u5bf9\u4e8e\u6211\u4eec\u4e86\u89e3\u7a0b\u5e8f\u8bed\u8a00\u4e2d\u65f6\u95f4\u7c7b\u7684\u6784\u9020\u975e\u5e38\u6709\u5e2e\u52a9\u3002Java8 \u65b0\u63a8\u51fa\u6765\u7684\u8fd9\u4e09\u4e2a\u7c7b\uff0c\u4e0d\u4ec5\u8ba9\u6211\u4eec\u5728\u65e5\u5e38\u7f16\u7a0b\u4e2d\u66f4\u4e3a\u9ad8\u6548\u65b9\u4fbf\uff0c\u800c\u4e14\uff0c\u5728\u4e0e\u6570\u636e\u5e93\u65f6\u95f4\u7c7b\u578b\u7684\u5bf9\u5e94\u4e0a\u66f4\u4e3a\u53cb\u597d\u3002<\/p>\n\n\n\n<p>\u6700\u540e\u603b\u7ed3\uff1a<\/p>\n\n\n\n<ul><li>1\uff0c\u65f6\u95f4\u6233\uff1a \u662f\u6307\u683c\u6797\u5a01\u6cbb\uff08\u5730\u7403\u96f6\u65f6\u533a\uff09\u65f6\u95f41970\u5e7401\u670801\u65e500\u65f600\u520600\u79d2\u8d77\u81f3\u73b0\u5728\u7684\u603b\u79d2\u6570\uff0c\u8fd9\u4e2a\u65f6\u95f4\u6233\uff0c\u5728\u5730\u7403\u7684\u5404\u4e2a\u5730\u65b9\u90fd\u662f\u4e00\u81f4\u7684;<\/li><\/ul>\n\n\n\n<ul><li>2\uff0c\u65f6\u533a\uff1a\u7531\u4e8e\u5730\u7403\u7684\u81ea\u8f6c\uff0c\u6839\u636e\u63a5\u6536\u592a\u9633\u5149\u7167\u7684\u987a\u5e8f\u5c06\u5730\u7403\u5212\u5206\u621024\u4e2a\u533a\uff0c\u4ece\u800c\u65b9\u4fbf\u5f53\u5730\u4eba\u7684\u751f\u4ea7\u751f\u6d3b\uff0c\u6bcf\u4e2a\u65f6\u533a\u76f8\u5dee\u4e00\u5c0f\u65f6\uff0c\u53ef\u4ee5\u6839\u636e\u65f6\u95f4\u6233\u548c\u65f6\u533a\u8ba1\u7b97\u5f53\u5730\u7684\u65f6\u95f4\u3002\u683c\u6797\u5a01\u6cbb\u5904\u4e8e\u96f6\u65f6\u533a\uff0c\u5317\u4eac\u5904\u4e8e\u4e1c\u516b\u533a\uff0c\u56e0\u6b64\uff0c\u5317\u4eac\u65f6\u95f4\u6bd4\u683c\u6797\u5a01\u6cbb\u65f6\u95f4\u65e98\u4e2a\u5c0f\u65f6\u3002<\/li><\/ul>\n\n\n\n<ul><li>3\uff0cLocalDateTime \u6bd4 Date\u4f7f\u7528\u8d77\u6765\u66f4\u4e3a\u65b9\u4fbf\uff0c\u4e24\u8005\u53ef\u4ee5\u76f8\u4e92\u8fdb\u884c\u8f6c\u5316\u3002<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u80cc\u666f\uff1a \u4e4b\u524d\u5728\u505a\u9879\u76ee\u7684\u8fc7\u7a0b\u4e2d\uff0c\u5bf9\u65e5\u671f\u65f6\u95f4\u7c7b\u6ca1\u6709\u4e00\u4e2a\u7cfb\u7edf\u7684\u4e86\u89e3\uff0c\u603b\u662f\u5728\u7528\u7684\u65f6\u5019\u53bb\u641c\u7d22\u4e00\u4e0b\uff0c\u89e3\u51b3\u95ee\u9898\u5373\u5b8c\u4e8b\uff0c\u4e45 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/6877"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6877"}],"version-history":[{"count":4,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/6877\/revisions"}],"predecessor-version":[{"id":7286,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/6877\/revisions\/7286"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6877"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6877"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6877"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}