{"id":1751,"date":"2023-03-26T21:28:14","date_gmt":"2023-03-26T13:28:14","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1751"},"modified":"2023-04-23T21:27:42","modified_gmt":"2023-04-23T13:27:42","slug":"sort-out-confusing-points-of-date-localdatetime-and-calendar-in-java","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/26\/sort-out-confusing-points-of-date-localdatetime-and-calendar-in-java\/","title":{"rendered":"Java\u4e2dDate\u3001LocalDateTime\u4e0eCalendar\u6613\u6df7\u6dc6\u70b9\u68b3\u7406"},"content":{"rendered":"<h3>Date\u4e0e\u65f6\u533a\u6709\u5173<\/h3>\n<blockquote>\n<p>Date\u6700\u597d\u53d6\u7cfb\u7edf\u65f6\u533a\uff0c\u5426\u5219\u5bb9\u6613\u6df7\u6dc6<\/p>\n<\/blockquote>\n<p><!-- more --><\/p>\n<pre><code class=\"language-java\">\/\/Date now = Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant());  \/\/\u53d6\u7cfb\u7edf\u65f6\u533a\nDate thDate = Date.from(LocalDateTime.now().atZone(TimeZone.getTimeZone(&quot;GMT+8&quot;).toZoneId()).toInstant());  \/\/\u4e0e\u65f6\u533a\u6709\u5173\nSystem.out.println(thDate);\nCalendar calendar = Calendar.getInstance(TimeZone.getTimeZone(&quot;GMT+0&quot;));  \/\/\u4e0e\u65f6\u533a\u65e0\u5173\ncalendar.setTime(thDate);\ncalendar.add(Calendar.HOUR, +1);\nDate cnDate = calendar.getTime();\nSystem.out.println(cnDate);\n\nString thday = DateUtil.formatDateTime(thDate);\nSystem.out.println(thday);\n\nString cnday = DateUtil.formatDateTime(cnDate);\nSystem.out.println(cnday);<\/code><\/pre>\n<pre><code class=\"language-java\">public static String formatDateTime(Date date) {\n    SimpleDateFormat sdf = new SimpleDateFormat(&quot;yyyy-MM-dd HH:mm:ss&quot;);\n    sdf.setTimeZone(TimeZone.getTimeZone(&quot;GMT+7&quot;));\n    return sdf.format(date);\n}<\/code><\/pre>\n<pre><code>Fri Oct 09 16:57:58 CST 2020\nFri Oct 09 17:57:58 CST 2020\n2020-10-09 15:57:58\n2020-10-09 16:57:58<\/code><\/pre>\n<h3>LocalDateTime\u65e5\u671f\u52a0\u51cf<\/h3>\n<pre><code class=\"language-java\">Date thDate = Date.from(LocalDateTime.now().atZone(TimeZone.getTimeZone(&quot;GMT+8&quot;).toZoneId()).toInstant());\nSystem.out.println(thDate);\nDate cnDate = DateUtil.addHours(thDate, 1);\nSystem.out.println(cnDate);\n\nString thday = DateUtil.formatDateTime(thDate);\nSystem.out.println(thday);\n\nString cnday = DateUtil.formatDateTime(cnDate);\nSystem.out.println(cnday);<\/code><\/pre>\n<pre><code class=\"language-java\">public static Date addHours(Date date, int hours) {\n    LocalDateTime dateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());\n    return Date.from(dateTime.plusHours(hours).atZone(ZoneId.systemDefault()).toInstant());\n}\n\npublic static Date addDays(Date date, int days) {\n    LocalDateTime dateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());\n    return Date.from(dateTime.minusDays(-days).atZone(ZoneId.systemDefault()).toInstant());\n}<\/code><\/pre>\n<pre><code>Fri Oct 09 17:11:02 CST 2020\nFri Oct 09 18:11:02 CST 2020\n2020-10-09 16:11:02\n2020-10-09 17:11:02<\/code><\/pre>\n<h3>Calendar\u5224\u65ad\u5468\u672b\u4e0e\u65f6\u533a\u6709\u5173<\/h3>\n<pre><code class=\"language-java\">Date date = new Date();\nSystem.out.println(date);\nCalendar calendar = Calendar.getInstance(TimeZone.getTimeZone(&quot;GMT-2&quot;));\nDate date2 = calendar.getTime();\nSystem.out.println(date2);\nif (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {\n    System.out.println(&quot;\u5468\u672b&quot;);\n} else {\n    System.out.println(&quot;\u975e\u5468\u672b&quot;);\n}<\/code><\/pre>\n<pre><code>Fri Oct 09 16:56:10 CST 2020\nFri Oct 09 16:56:10 CST 2020\n\u975e\u5468\u672b<\/code><\/pre>\n<h3>Calendar\u83b7\u53d6\u65f6\u95f4\u4e0e\u65f6\u533a\u65e0\u5173<\/h3>\n<pre><code class=\"language-java\">Calendar calendar = Calendar.getInstance();\ncalendar.setTimeZone(TimeZone.getTimeZone(&quot;GMT+8&quot;));\nDate cnDate = calendar.getTime();\nSystem.out.println(cnDate);\ncalendar.setTimeZone(TimeZone.getTimeZone(&quot;GMT+7&quot;));\nDate thDate = calendar.getTime();\nSystem.out.println(thDate);\n\nString thday = DateUtil.formatDateTime(thDate);\nSystem.out.println(thday);\n\nString cnday = DateUtil.formatDateTime(cnDate);\nSystem.out.println(cnday);<\/code><\/pre>\n<pre><code>Fri Oct 09 17:22:58 CST 2020\nFri Oct 09 17:22:58 CST 2020\n2020-10-09 16:22:58\n2020-10-09 16:22:58<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Date\u4e0e\u65f6\u533a\u6709\u5173 Date\u6700\u597d\u53d6\u7cfb\u7edf\u65f6\u533a\uff0c\u5426\u5219\u5bb9\u6613\u6df7\u6dc6 \/\/Date now = Date.from(Loc [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[],"class_list":["post-1751","post","type-post","status-publish","format-standard","hentry","category-java-basic"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1751","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=1751"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1751\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1751"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1751"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1751"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}