diff --git a/mica-core/src/main/java/net/dreamlu/mica/core/format/FastDateFormat.java b/mica-core/src/main/java/net/dreamlu/mica/core/format/FastDateFormat.java new file mode 100644 index 0000000000000000000000000000000000000000..6a4a1cbbce283419bd23fe38d2b0bf16921aa806 --- /dev/null +++ b/mica-core/src/main/java/net/dreamlu/mica/core/format/FastDateFormat.java @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net). + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.dreamlu.mica.core.format; + +import lombok.Getter; +import lombok.Setter; +import lombok.SneakyThrows; +import net.dreamlu.mica.core.utils.DateUtil; + +import java.text.DateFormat; +import java.text.FieldPosition; +import java.text.ParsePosition; +import java.time.format.DateTimeFormatter; +import java.util.Date; +import java.util.Locale; + +/** + * 线程安全、高性能的 DateFormat + * + *

+ * 用于某些参数为 DateFormat 的组件中,对于业务代码推荐直接使用 DateUtil 性能会更好。 + *

+ * + * @author L.cm + */ +@Getter +@Setter +public class FastDateFormat extends DateFormat { + private static final java.lang.reflect.Field FIELD = getToStringCacheField(); + private final DateTimeFormatter formatter; + + public FastDateFormat(DateTimeFormatter formatter) { + this.formatter = formatter; + } + + public FastDateFormat(String pattern) { + this(DateTimeFormatter.ofPattern(pattern)); + } + + public FastDateFormat(String pattern, Locale locale) { + this(DateTimeFormatter.ofPattern(pattern, locale)); + } + + @SneakyThrows + @Override + public StringBuffer format(Date date, StringBuffer stringBuffer, FieldPosition fieldPosition) { + FIELD.set(stringBuffer, DateUtil.format(date, formatter)); + return stringBuffer; + } + + @Override + public Date parse(String source) { + return DateUtil.parse(source, formatter); + } + + @Override + public Date parse(String dateStr, ParsePosition parsePosition) { + return null; + } + + @SneakyThrows + private static java.lang.reflect.Field getToStringCacheField() { + java.lang.reflect.Field field = StringBuffer.class.getDeclaredField("toStringCache"); + field.setAccessible(true); + return field; + } + +} diff --git a/mica-core/src/main/java/net/dreamlu/mica/core/utils/DateUtil.java b/mica-core/src/main/java/net/dreamlu/mica/core/utils/DateUtil.java index f58292f2d2ff3b5f336eda49dfadf5f73bf44e47..468e2085d29eb2d57cfbb7ad05a49fadaae00382 100644 --- a/mica-core/src/main/java/net/dreamlu/mica/core/utils/DateUtil.java +++ b/mica-core/src/main/java/net/dreamlu/mica/core/utils/DateUtil.java @@ -310,10 +310,7 @@ public class DateUtil { */ @Nullable public static String formatDateTime(@Nullable Date date) { - if (date == null) { - return null; - } - return DATETIME_FORMATTER.format(date.toInstant()); + return format(date, DATETIME_FORMATTER); } /** @@ -324,10 +321,7 @@ public class DateUtil { */ @Nullable public static String formatDate(@Nullable Date date) { - if (date == null) { - return null; - } - return DATE_FORMATTER.format(date.toInstant()); + return format(date, DATE_FORMATTER); } /** @@ -338,10 +332,7 @@ public class DateUtil { */ @Nullable public static String formatTime(@Nullable Date date) { - if (date == null) { - return null; - } - return TIME_FORMATTER.format(date.toInstant()); + return format(date, TIME_FORMATTER); } /** @@ -353,10 +344,38 @@ public class DateUtil { */ @Nullable public static String format(@Nullable Date date, String pattern) { + return format(date, DateTimeFormatter.ofPattern(pattern)); + } + + /** + * 日期格式化 + * + * @param date 时间 + * @param formatter 格式化 + * @return 格式化后的时间 + */ + @Nullable + public static String format(@Nullable Date date, DateTimeFormatter formatter) { if (date == null) { return null; } - return DateTimeFormatter.ofPattern(pattern).withZone(ZoneId.systemDefault()).format(date.toInstant()); + return format(date.toInstant(), formatter); + } + + /** + * 日期格式化 + * + * @param instant 时间 + * @param formatter 格式化 + * @return 格式化后的时间 + */ + @Nullable + public static String format(Instant instant, DateTimeFormatter formatter) { + ZoneId zone = formatter.getZone(); + if (zone == null) { + return formatter.withZone(ZoneId.systemDefault()).format(instant); + } + return formatter.format(instant); } /** diff --git a/mica-core/src/main/java/net/dreamlu/mica/core/utils/JsonUtil.java b/mica-core/src/main/java/net/dreamlu/mica/core/utils/JsonUtil.java index c06a4309d063d0a5893984647cc3ac93fe4bfe4a..453e8bb011a4c2f38f5b2a7736cb854e90f2234a 100644 --- a/mica-core/src/main/java/net/dreamlu/mica/core/utils/JsonUtil.java +++ b/mica-core/src/main/java/net/dreamlu/mica/core/utils/JsonUtil.java @@ -26,12 +26,12 @@ import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.type.CollectionLikeType; import com.fasterxml.jackson.databind.type.MapType; import lombok.experimental.UtilityClass; +import net.dreamlu.mica.core.format.FastDateFormat; import net.dreamlu.mica.core.function.CheckedConsumer; import org.springframework.lang.Nullable; import java.io.IOException; import java.io.InputStream; -import java.text.SimpleDateFormat; import java.time.ZoneId; import java.util.*; @@ -564,7 +564,7 @@ public class JsonUtil { JacksonObjectMapper() { super(jsonFactory()); super.setLocale(CHINA); - super.setDateFormat(new SimpleDateFormat(DateUtil.PATTERN_DATETIME, CHINA)); + super.setDateFormat(new FastDateFormat(DateUtil.PATTERN_DATETIME, CHINA)); // 单引号 super.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); // 忽略json字符串中不识别的属性