From 522dbd3a32750146beb78f12f9cd7a39f7e76795 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Wed, 28 Jun 2023 01:05:56 +0800 Subject: [PATCH] fix bug. --- .../plusone/commons/util/DateTimeUtil.java | 27 ++++++------------- 1 file changed, 8 insertions(+), 19 deletions(-) diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/DateTimeUtil.java b/src/main/java/xyz/zhouxy/plusone/commons/util/DateTimeUtil.java index ffebe8a..bb78567 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/DateTimeUtil.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/DateTimeUtil.java @@ -9,31 +9,20 @@ import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.util.Calendar; import java.util.Date; -import java.util.Objects; import java.util.TimeZone; -import org.apache.commons.lang3.StringUtils; +import xyz.zhouxy.plusone.commons.collection.SafeConcurrentHashMap; public class DateTimeUtil { - private static final MapWrapper DATE_TIME_FORMATTER_CHCHE = MapWrapper - .wrapHashMap() - .keyChecker(StringUtils::isNotBlank) - .valueChecker(Objects::nonNull) + private static final MapWrapper DATE_TIME_FORMATTER_CACHE = MapWrapper + .wrap(new SafeConcurrentHashMap<>()) + .keyChecker(pattern -> Assert.isNotBlank(pattern, "The pattern could not be blank.")) + .valueChecker(formatter -> Assert.notNull(formatter, "The formatter could not be null.")) .build(); public static DateTimeFormatter getDateTimeFormatter(String pattern) { - if (!DATE_TIME_FORMATTER_CHCHE.containsKey(pattern)) { - synchronized (DateTimeUtil.class) { - if (!DATE_TIME_FORMATTER_CHCHE.containsKey(pattern)) { - DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern); - DATE_TIME_FORMATTER_CHCHE.put(pattern, formatter); - return formatter; - } - } - } - return DATE_TIME_FORMATTER_CHCHE.get(pattern) - .orElseThrow(() -> new IllegalStateException("Formatter does not exist.")); + return DATE_TIME_FORMATTER_CACHE.computeIfAbsent(pattern, DateTimeFormatter::ofPattern); } public static String toString(String pattern, ZonedDateTime dateTime) { @@ -124,7 +113,7 @@ public class DateTimeUtil { * 只是不同时区的表示。 *

* - * @param dateTime {@link Date} 对象 + * @param timeMillis 时间戳 * @param zone 时区 * @return 带时区信息的地区时间 */ @@ -189,7 +178,7 @@ public class DateTimeUtil { /** * 获取时间戳在指定时区的地区时间。 * - * @param dateTime {@link Date} 对象 + * @param timeMillis 时间戳 * @param zone 时区 * @return 地区时间 */