mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
修复TemporalAccessorConverter自定义格式转换问题
This commit is contained in:
@@ -20,6 +20,7 @@
|
||||
* 【core 】 解决CalendarUtil.isSameDay时区不同导致结果错误问题(pr#3548@Github)
|
||||
* 【core 】 修复RandomUtil.randomStringWithoutStr方法问题(pr#1209@Gitee)
|
||||
* 【http 】 修复HttpRequest.header相同key被覆盖问题(issue#I9I61C@Gitee)
|
||||
* 【core 】 修复TemporalAccessorConverter自定义格式转换问题(issue#I9HQQE@Gitee)
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
# 5.8.27(2024-03-29)
|
||||
|
@@ -152,6 +152,13 @@ public class TemporalAccessorConverter extends AbstractConverter<TemporalAccesso
|
||||
ZoneId zoneId;
|
||||
if (null != this.format) {
|
||||
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(this.format);
|
||||
|
||||
// issue#I9HQQE
|
||||
final TemporalAccessor temporalAccessor = parseWithFormat(this.targetType, value, formatter);
|
||||
if (null != temporalAccessor) {
|
||||
return temporalAccessor;
|
||||
}
|
||||
|
||||
instant = formatter.parse(value, Instant::from);
|
||||
zoneId = formatter.getZone();
|
||||
} else {
|
||||
@@ -162,6 +169,26 @@ public class TemporalAccessorConverter extends AbstractConverter<TemporalAccesso
|
||||
return parseFromInstant(instant, zoneId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 对于自定义格式的字符串,单独解析为{@link TemporalAccessor}
|
||||
*
|
||||
* @param targetClass 目标类型
|
||||
* @param value 日期字符串
|
||||
* @param formatter 格式
|
||||
* @return {@link TemporalAccessor}
|
||||
*/
|
||||
private TemporalAccessor parseWithFormat(final Class<?> targetClass, final CharSequence value, final DateTimeFormatter formatter) {
|
||||
// issue#I9HQQE
|
||||
if (LocalDate.class == targetClass) {
|
||||
return LocalDate.parse(value, formatter);
|
||||
} else if (LocalDateTime.class == targetClass) {
|
||||
return LocalDateTime.parse(value, formatter);
|
||||
} else if (LocalTime.class == targetClass) {
|
||||
return LocalTime.parse(value, formatter);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将Long型时间戳转换为java.time中的对象
|
||||
*
|
||||
|
Reference in New Issue
Block a user