From a8afe6e4241966188a093d976eb67fcd21caa584 Mon Sep 17 00:00:00 2001 From: xyiege Date: Tue, 27 Sep 2022 22:43:23 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=9F=E6=88=90=E7=9A=84=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E8=BD=AC=E4=B8=BALocalDateTime?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/chjyj/szwh/utils/DateUtils.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/main/java/cn/chjyj/szwh/utils/DateUtils.java b/src/main/java/cn/chjyj/szwh/utils/DateUtils.java index 773670d..948158d 100644 --- a/src/main/java/cn/chjyj/szwh/utils/DateUtils.java +++ b/src/main/java/cn/chjyj/szwh/utils/DateUtils.java @@ -2,6 +2,10 @@ package cn.chjyj.szwh.utils; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.ZonedDateTime; import java.util.Calendar; import java.util.Date; @@ -67,4 +71,19 @@ public class DateUtils { return mon.getTime(); } + /** + * 日期本地化 + * note:Date对象表示特定的日期和时间,而LocalDate(Java8)对象只包含没有任何时间信息的日期。 + * @param date + * @return + */ + public static LocalDateTime convertDateToLocalDateTime(Date date){ + // 系统时区 + ZoneId zoneId = ZoneId.systemDefault(); + Instant instant = date.toInstant(); + // atZone()方法返回在指定时区从此Instant生成的ZonedDateTime。 + LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime(); + return localDateTime; + } + }