提交 c9f60302 编写于 作者: 如梦技术's avatar 如梦技术 🐛

优化 mica-core DateUtil。

上级 5a8e29d9
/*
* Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net).
* <p>
* 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
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* 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
*
* <p>
* 用于某些参数为 DateFormat 的组件中,对于业务代码推荐直接使用 DateUtil 性能会更好。
* </p>
*
* @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;
}
}
......@@ -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);
}
/**
......
......@@ -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字符串中不识别的属性
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册