mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
fix localdate for json bug
This commit is contained in:
parent
54749abfcc
commit
97f1e8b3dd
@ -3,7 +3,7 @@
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
|
||||
# 5.5.3 (2020-12-06)
|
||||
# 5.5.3 (2020-12-07)
|
||||
|
||||
### 新特性
|
||||
* 【core 】 IdcardUtil增加行政区划83(issue#1277@Github)
|
||||
@ -14,6 +14,9 @@
|
||||
### Bug修复
|
||||
* 【cache 】 修复Cache中get重复misCount计数问题(issue#1281@Github)
|
||||
* 【poi 】 修复sax读取自定义格式单元格无法识别日期类型的问题(issue#1283@Github)
|
||||
* 【core 】 修复CollUtil.get越界问题(issue#1292@Github)
|
||||
* 【json 】 修复TemporalAccessorUtil无法格式化LocalDate带时间问题(issue#1289@Github)
|
||||
* 【json 】 修复自定义日期格式的LocalDateTime没有包装引号问题(issue#1289@Github)
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
@ -1870,7 +1870,7 @@ public class CollUtil {
|
||||
/**
|
||||
* Iterator转换为Enumeration
|
||||
* <p>
|
||||
* Adapt the specified <code>Iterator</code> to the <code>Enumeration</code> interface.
|
||||
* Adapt the specified {@link Iterator} to the {@link Enumeration} interface.
|
||||
*
|
||||
* @param <E> 集合元素类型
|
||||
* @param iter {@link Iterator}
|
||||
@ -1883,7 +1883,7 @@ public class CollUtil {
|
||||
/**
|
||||
* Enumeration转换为Iterator
|
||||
* <p>
|
||||
* Adapt the specified <code>Enumeration</code> to the <code>Iterator</code> interface
|
||||
* Adapt the specified {@code Enumeration} to the {@code Iterator} interface
|
||||
*
|
||||
* @param <E> 集合元素类型
|
||||
* @param e {@link Enumeration}
|
||||
@ -2186,7 +2186,7 @@ public class CollUtil {
|
||||
}
|
||||
|
||||
// 检查越界
|
||||
if (index >= size) {
|
||||
if (index >= size || index < 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -13,6 +13,7 @@ import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.temporal.TemporalAccessor;
|
||||
import java.time.temporal.TemporalField;
|
||||
import java.time.temporal.UnsupportedTemporalTypeException;
|
||||
|
||||
/**
|
||||
* {@link TemporalAccessor} 工具类封装
|
||||
@ -54,7 +55,19 @@ public class TemporalAccessorUtil extends TemporalUtil{
|
||||
formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
|
||||
}
|
||||
|
||||
return formatter.format(time);
|
||||
|
||||
try {
|
||||
return formatter.format(time);
|
||||
} catch (UnsupportedTemporalTypeException e){
|
||||
if(time instanceof LocalDate && e.getMessage().contains("HourOfDay")){
|
||||
// 用户传入LocalDate,但是要求格式化带有时间部分,转换为LocalDateTime重试
|
||||
return formatter.format(((LocalDate) time).atStartOfDay());
|
||||
}else if(time instanceof LocalTime && e.getMessage().contains("YearOfEra")){
|
||||
// 用户传入LocalTime,但是要求格式化带有日期部分,转换为LocalDateTime重试
|
||||
return formatter.format(((LocalTime) time).atDate(LocalDate.now()));
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -0,0 +1,23 @@
|
||||
package cn.hutool.core.date;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalTime;
|
||||
|
||||
public class TemporalAccessorUtilTest {
|
||||
|
||||
@Test
|
||||
public void formatLocalDateTest(){
|
||||
final String format = TemporalAccessorUtil.format(LocalDate.of(2020, 12, 7), DatePattern.NORM_DATETIME_PATTERN);
|
||||
Assert.assertEquals("2020-12-07 00:00:00", format);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void formatLocalTimeTest(){
|
||||
final String today = TemporalAccessorUtil.format(LocalDate.now(), DatePattern.NORM_DATE_PATTERN);
|
||||
final String format = TemporalAccessorUtil.format(LocalTime.MIN, DatePattern.NORM_DATETIME_PATTERN);
|
||||
Assert.assertEquals(today + " 00:00:00", format);
|
||||
}
|
||||
}
|
@ -240,11 +240,14 @@ final class InternalJSONUtil {
|
||||
*/
|
||||
private static String formatDate(Object dateObj, String format) {
|
||||
if (StrUtil.isNotBlank(format)) {
|
||||
final String dateStr;
|
||||
if(dateObj instanceof TemporalAccessor){
|
||||
return TemporalAccessorUtil.format((TemporalAccessor) dateObj, format);
|
||||
dateStr = TemporalAccessorUtil.format((TemporalAccessor) dateObj, format);
|
||||
} else{
|
||||
dateStr = DateUtil.format(Convert.toDate(dateObj), format);
|
||||
}
|
||||
//用户定义了日期格式
|
||||
return JSONUtil.quote(DateUtil.format(Convert.toDate(dateObj), format));
|
||||
return JSONUtil.quote(dateStr);
|
||||
}
|
||||
|
||||
//默认使用时间戳
|
||||
|
Loading…
Reference in New Issue
Block a user