mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
修复TemporalAccessorConverter自定义格式转换问题
This commit is contained in:
parent
7a5f910aa7
commit
2b6326ca94
@ -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)
|
||||
|
@ -117,7 +117,7 @@ public class TemporalAccessorConverter extends AbstractConverter<TemporalAccesso
|
||||
return LocalDate.of(Convert.toInt(map.get("year")), Convert.toInt(map.get("month")), Convert.toInt(map.get("day")));
|
||||
} else if (LocalDateTime.class.equals(this.targetType)) {
|
||||
return LocalDateTime.of(Convert.toInt(map.get("year")), Convert.toInt(map.get("month")), Convert.toInt(map.get("day")),
|
||||
Convert.toInt(map.get("hour")), Convert.toInt(map.get("minute")), Convert.toInt(map.get("second")), Convert.toInt(map.get("second")));
|
||||
Convert.toInt(map.get("hour")), Convert.toInt(map.get("minute")), Convert.toInt(map.get("second")), Convert.toInt(map.get("second")));
|
||||
} else if (LocalTime.class.equals(this.targetType)) {
|
||||
return LocalTime.of(Convert.toInt(map.get("hour")), Convert.toInt(map.get("minute")), Convert.toInt(map.get("second")), Convert.toInt(map.get("nano")));
|
||||
}
|
||||
@ -138,13 +138,13 @@ public class TemporalAccessorConverter extends AbstractConverter<TemporalAccesso
|
||||
return null;
|
||||
}
|
||||
|
||||
if(DayOfWeek.class.equals(this.targetType)){
|
||||
if (DayOfWeek.class.equals(this.targetType)) {
|
||||
return DayOfWeek.valueOf(StrUtil.toString(value));
|
||||
} else if(Month.class.equals(this.targetType)){
|
||||
} else if (Month.class.equals(this.targetType)) {
|
||||
return Month.valueOf(StrUtil.toString(value));
|
||||
} else if(Era.class.equals(this.targetType)){
|
||||
} else if (Era.class.equals(this.targetType)) {
|
||||
return IsoEra.valueOf(StrUtil.toString(value));
|
||||
} else if(MonthDay.class.equals(this.targetType)){
|
||||
} else if (MonthDay.class.equals(this.targetType)) {
|
||||
return MonthDay.parse(value);
|
||||
}
|
||||
|
||||
@ -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中的对象
|
||||
*
|
||||
@ -169,20 +196,20 @@ public class TemporalAccessorConverter extends AbstractConverter<TemporalAccesso
|
||||
* @return java.time中的对象
|
||||
*/
|
||||
private TemporalAccessor parseFromLong(Long time) {
|
||||
if(DayOfWeek.class.equals(this.targetType)){
|
||||
if (DayOfWeek.class.equals(this.targetType)) {
|
||||
return DayOfWeek.of(Math.toIntExact(time));
|
||||
} else if(Month.class.equals(this.targetType)){
|
||||
} else if (Month.class.equals(this.targetType)) {
|
||||
return Month.of(Math.toIntExact(time));
|
||||
} else if(Era.class.equals(this.targetType)){
|
||||
} else if (Era.class.equals(this.targetType)) {
|
||||
return IsoEra.of(Math.toIntExact(time));
|
||||
}
|
||||
|
||||
final Instant instant;
|
||||
if(GlobalCustomFormat.FORMAT_SECONDS.equals(this.format)){
|
||||
if (GlobalCustomFormat.FORMAT_SECONDS.equals(this.format)) {
|
||||
// https://gitee.com/dromara/hutool/issues/I6IS5B
|
||||
// Unix时间戳
|
||||
instant = Instant.ofEpochSecond(time);
|
||||
}else{
|
||||
} else {
|
||||
instant = Instant.ofEpochMilli(time);
|
||||
}
|
||||
return parseFromInstant(instant, null);
|
||||
@ -195,11 +222,11 @@ public class TemporalAccessorConverter extends AbstractConverter<TemporalAccesso
|
||||
* @return java.time中的对象
|
||||
*/
|
||||
private TemporalAccessor parseFromTemporalAccessor(TemporalAccessor temporalAccessor) {
|
||||
if(DayOfWeek.class.equals(this.targetType)){
|
||||
if (DayOfWeek.class.equals(this.targetType)) {
|
||||
return DayOfWeek.from(temporalAccessor);
|
||||
} else if(Month.class.equals(this.targetType)){
|
||||
} else if (Month.class.equals(this.targetType)) {
|
||||
return Month.from(temporalAccessor);
|
||||
} else if(MonthDay.class.equals(this.targetType)){
|
||||
} else if (MonthDay.class.equals(this.targetType)) {
|
||||
return MonthDay.from(temporalAccessor);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user