This commit is contained in:
Looly 2022-01-15 19:22:40 +08:00
parent 4b44716b5f
commit 749c0f8727
6 changed files with 64 additions and 4 deletions

View File

@ -20,6 +20,7 @@
* 【core 】 修复OS中的拼写错误pr#500@Gitee
* 【core 】 修复CustomKeyMap的merge失效问题issue#2086@Github
* 【core 】 修复FileUtil.appendLines换行问题issue#I4QCEZ@Gitee
* 【core 】 修复java.time.Month解析问题issue#2090@Github
-------------------------------------------------------------------------------------------------------------
# 5.7.19 (2022-01-07)

View File

@ -35,6 +35,7 @@ public class BeanCopier<T> implements Copier<T>, Serializable {
private static final long serialVersionUID = 1L;
/** 源对象 */
@SuppressWarnings("NonSerializableFieldInSerializableClass")
private final Object source;
/** 目标对象 */
private final T dest;

View File

@ -7,6 +7,7 @@ import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.ZoneId;
@ -40,7 +41,8 @@ public class TemporalAccessorUtil extends TemporalUtil{
}
/**
* 格式化日期时间为指定格式
* 格式化日期时间为指定格式<br>
* 如果为{@link Month}调用{@link Month#toString()}
*
* @param time {@link TemporalAccessor}
* @param formatter 日期格式化器预定义的格式见{@link DateTimeFormatter}
@ -52,6 +54,10 @@ public class TemporalAccessorUtil extends TemporalUtil{
return null;
}
if(time instanceof Month){
return time.toString();
}
if(null == formatter){
formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
}
@ -74,7 +80,8 @@ public class TemporalAccessorUtil extends TemporalUtil{
}
/**
* 格式化日期时间为指定格式
* 格式化日期时间为指定格式<br>
* 如果为{@link Month}调用{@link Month#toString()}
*
* @param time {@link TemporalAccessor}
* @param format 日期格式
@ -86,6 +93,10 @@ public class TemporalAccessorUtil extends TemporalUtil{
return null;
}
if(time instanceof Month){
return time.toString();
}
// 检查自定义格式
if(GlobalCustomFormat.isCustomFormat(format)){
return GlobalCustomFormat.format(time, format);
@ -98,13 +109,17 @@ public class TemporalAccessorUtil extends TemporalUtil{
}
/**
* {@link TemporalAccessor}转换为 时间戳从1970-01-01T00:00:00Z开始的毫秒数
* {@link TemporalAccessor}转换为 时间戳从1970-01-01T00:00:00Z开始的毫秒数<br>
* 如果为{@link Month}调用{@link Month#getValue()}
*
* @param temporalAccessor Date对象
* @return {@link Instant}对象
* @since 5.4.1
*/
public static long toEpochMilli(TemporalAccessor temporalAccessor) {
if(temporalAccessor instanceof Month){
return ((Month) temporalAccessor).getValue();
}
return toInstant(temporalAccessor).toEpochMilli();
}

View File

@ -671,7 +671,6 @@ public class JSONObject implements JSON, JSONGetter<String>, Map<String, Object>
// 不支持对象类型转换为JSONObject
throw new JSONException("Unsupported type [{}] to JSONObject!", source.getClass());
}
}
/**

View File

@ -0,0 +1,43 @@
package cn.hutool.json;
import lombok.Data;
import org.junit.Assert;
import org.junit.Test;
import java.time.LocalDate;
import java.time.Month;
/**
* https://github.com/dromara/hutool/issues/2090
*/
public class Issue2090Test {
@Test
public void parseTest(){
final TestBean test = new TestBean();
test.setLocalDate(LocalDate.now());
final JSONObject json = JSONUtil.parseObj(test);
final TestBean test1 = json.toBean(TestBean.class);
Assert.assertEquals(test, test1);
}
@Test
public void parseLocalDateTest(){
LocalDate localDate = LocalDate.now();
final JSONObject jsonObject = JSONUtil.parseObj(localDate);
Assert.assertNotNull(jsonObject.toString());
}
@Test
public void monthTest(){
final JSONObject jsonObject = new JSONObject();
jsonObject.set("month", Month.JANUARY);
Assert.assertEquals("{\"month\":1}", jsonObject.toString());
}
@Data
public static class TestBean{
private LocalDate localDate;
}
}

View File

@ -30,6 +30,7 @@ import org.junit.Test;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.util.Date;
import java.util.HashMap;
import java.util.List;