feature/net-util
ZhouXY108 2023-06-28 01:05:56 +08:00
parent 59f5a1d17b
commit 522dbd3a32
1 changed files with 8 additions and 19 deletions

View File

@ -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<String, DateTimeFormatter> DATE_TIME_FORMATTER_CHCHE = MapWrapper
.<String, DateTimeFormatter>wrapHashMap()
.keyChecker(StringUtils::isNotBlank)
.valueChecker(Objects::nonNull)
private static final MapWrapper<String, DateTimeFormatter> DATE_TIME_FORMATTER_CACHE = MapWrapper
.<String, DateTimeFormatter>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 {
*
* </p>
*
* @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
*/