{"id":907,"date":"2023-03-11T16:54:18","date_gmt":"2023-03-11T08:54:18","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=907"},"modified":"2023-04-29T15:51:23","modified_gmt":"2023-04-29T07:51:23","slug":"spring-boot-time-format-time-zone-conversion","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/11\/spring-boot-time-format-time-zone-conversion\/","title":{"rendered":"Spring Boot\u65f6\u95f4\u683c\u5f0f\u7684\u65f6\u533a\u8f6c\u6362"},"content":{"rendered":"<p>\u524d\u8a00\uff1a\u9700\u8981\u505a\u65f6\u533a\u8f6c\u6362\uff0c\u77e5\u9053\u5317\u4eac\u4e3aUTC+8\uff0c\u7f8e\u897f\u4e3aUTC-8\uff0c\u4e16\u754c\u6807\u51c6\u65f6\u95f4\u4e3aUTC\uff0c\u6240\u4ee5\u53ea\u9700\u8981\u77e5\u9053\u65f6\u533a\u662f+8\u8fd8\u662f-8\u8fd8\u662f0\u5373\u53ef\uff0c\u4e0d\u9700\u8981\u4f7f\u7528&quot;CTT&quot;\u3001&quot;Asia\/Shanghai&quot;\u8fd9\u79cd\u5f62\u5f0f\u3002<\/p>\n<h3>\u65f6\u533a\u8f6c\u6362\u8f85\u52a9\u7c7b<\/h3>\n<p><!-- more --><\/p>\n<pre><code class=\"language-java\">@Component\npublic class DateHelper {\n    private SimpleDateFormat dateSdf = new SimpleDateFormat(&quot;yyyy-MM-dd HH:mm:ss&quot;);\n    private SimpleDateFormat dateUtcSdf = new SimpleDateFormat(&quot;yyyy-MM-dd HH:mm:ss&quot;);\n    private SimpleDateFormat dateE8Sdf = new SimpleDateFormat(&quot;yyyy-MM-dd HH:mm:ss&quot;);  \/\/\u5317\u4eac\u65f6\u95f4\n    private SimpleDateFormat dateW8Sdf = new SimpleDateFormat(&quot;yyyy-MM-dd HH:mm:ss&quot;);  \/\/\u7f8e\u56fd\u897f\u90e8\u65f6\u95f4\n\n    @PostConstruct\n    public void init() {\n        dateUtcSdf.setTimeZone(TimeZone.getTimeZone(&quot;GMT+0&quot;));\n        dateE8Sdf.setTimeZone(TimeZone.getTimeZone(&quot;GMT+8&quot;));\n        dateW8Sdf.setTimeZone(TimeZone.getTimeZone(&quot;GMT-8&quot;));\n    }\n\n    public String formatDate(Date date) {\n        if (date == null) return &quot;&quot;;\n        return dateSdf.format(date);\n    }\n\n    public Date parseDate(String dateStr) throws ParseException {\n        if (StringUtils.isBlank(dateStr)) return null;\n        return dateSdf.parse(dateStr);\n    }\n\n    public Date parseUtcDate(String dateStr) throws ParseException {\n        if (StringUtils.isBlank(dateStr)) return null;\n        return dateUtcSdf.parse(dateStr);\n    }\n\n    public String formatUtcDate(Date date) {\n        if (date == null) return &quot;&quot;;\n        return dateUtcSdf.format(date);\n    }\n\n    public Date parseE8Date(String dateStr) throws ParseException {\n        if (StringUtils.isBlank(dateStr)) return null;\n        return dateE8Sdf.parse(dateStr);\n    }\n\n    public String formatE8Date(Date date) {\n        if (date == null) return &quot;&quot;;\n        return dateE8Sdf.format(date);\n    }\n\n    public Date parseW8Date(String dateStr) throws ParseException {\n        if (StringUtils.isBlank(dateStr)) return null;\n        return dateW8Sdf.parse(dateStr);\n    }\n\n    public String formatW8Date(Date date) {\n        if (date == null) return &quot;&quot;;\n        return dateW8Sdf.format(date);\n    }\n}<\/code><\/pre>\n<h3>\u8bbf\u95ee\u63a7\u5236\u5668<\/h3>\n<pre><code class=\"language-java\">@RestController\n@RequestMapping(&quot;datetime&quot;)\npublic class DateController {\n\n    @Autowired\n    private DateHelper dateHelper;\n\n    \/\/\u5317\u4eac\u65f6\u95f4\n    @GetMapping(&quot;tze8&quot;)\n    @ResponseBody\n    public String tze8(String datetime) {\n        try {\n            return dateHelper.formatE8Date(dateHelper.parseDate(datetime));\n        } catch (Exception e) {\n            return &quot;&quot;;\n        }\n    }\n\n    \/\/0\u65f6\u533a\n    @GetMapping(&quot;tz0&quot;)\n    @ResponseBody\n    public String tz0(String datetime) {\n        try {\n            return dateHelper.formatUtcDate(dateHelper.parseDate(datetime));\n        } catch (Exception e) {\n            return &quot;&quot;;\n        }\n    }\n\n    \/\/\u7f8e\u56fd\u897f\u90e8\u65f6\u95f4\n    @GetMapping(&quot;tzw8&quot;)\n    @ResponseBody\n    public String tzw8(String datetime) {\n        try {\n            return dateHelper.formatW8Date(dateHelper.parseDate(datetime));\n        } catch (Exception e) {\n            return &quot;&quot;;\n        }\n    }\n\n}<\/code><\/pre>\n<h2>\u8bbf\u95ee\u6d4b\u8bd5<\/h2>\n<p>\u8bf7\u6c42\uff1a<code>http:\/\/127.0.0.1:8080\/datetime\/tz0?datetime=2019-05-20%2015:00:00<\/code><br \/>\n\u8fd4\u56de\uff1a<code>2019-05-20 07:00:00<\/code><\/p>\n<p>\u8bf7\u6c42\uff1a<code>http:\/\/127.0.0.1:8080\/datetime\/tze8?datetime=2019-05-20%2015:00:00<\/code><br \/>\n\u8fd4\u56de\uff1a<code>2019-05-20 15:00:00<\/code><\/p>\n<p>\u8bf7\u6c42\uff1a<code>http:\/\/127.0.0.1:8080\/datetime\/tzw8?datetime=2019-05-20%2015:00:00<\/code><br \/>\n\u8fd4\u56de\uff1a<code>2019-05-19 23:00:00<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00\uff1a\u9700\u8981\u505a\u65f6\u533a\u8f6c\u6362\uff0c\u77e5\u9053\u5317\u4eac\u4e3aUTC+8\uff0c\u7f8e\u897f\u4e3aUTC-8\uff0c\u4e16\u754c\u6807\u51c6\u65f6\u95f4\u4e3aUTC\uff0c\u6240\u4ee5\u53ea\u9700\u8981\u77e5\u9053\u65f6\u533a\u662f+8\u8fd8 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[],"class_list":["post-907","post","type-post","status-publish","format-standard","hentry","category-spring-boot"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/907","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=907"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/907\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=907"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=907"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=907"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}