mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix code
This commit is contained in:
parent
26d550dbab
commit
4e8060e441
@ -42,11 +42,14 @@ import java.net.URI;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.file.Path;
|
||||
import java.time.DayOfWeek;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.time.Month;
|
||||
import java.time.MonthDay;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.OffsetTime;
|
||||
import java.time.Period;
|
||||
@ -201,6 +204,10 @@ public class RegisterConverter implements Converter, Serializable {
|
||||
defaultConverterMap.put(ZonedDateTime.class, TemporalAccessorConverter.INSTANCE);
|
||||
defaultConverterMap.put(OffsetDateTime.class, TemporalAccessorConverter.INSTANCE);
|
||||
defaultConverterMap.put(OffsetTime.class, TemporalAccessorConverter.INSTANCE);
|
||||
defaultConverterMap.put(DayOfWeek.class, TemporalAccessorConverter.INSTANCE);
|
||||
defaultConverterMap.put(Month.class, TemporalAccessorConverter.INSTANCE);
|
||||
defaultConverterMap.put(MonthDay.class, TemporalAccessorConverter.INSTANCE);
|
||||
|
||||
defaultConverterMap.put(Period.class, new PeriodConverter());
|
||||
defaultConverterMap.put(Duration.class, new DurationConverter());
|
||||
|
||||
|
@ -12,10 +12,13 @@ import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.time.Month;
|
||||
import java.time.MonthDay;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.OffsetTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.chrono.Era;
|
||||
import java.time.chrono.IsoEra;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.temporal.TemporalAccessor;
|
||||
import java.util.Calendar;
|
||||
@ -113,6 +116,16 @@ public class TemporalAccessorConverter extends AbstractConverter {
|
||||
return null;
|
||||
}
|
||||
|
||||
if(DayOfWeek.class == targetClass){
|
||||
return DayOfWeek.valueOf(StrUtil.toString(value));
|
||||
} else if(Month.class == targetClass){
|
||||
return Month.valueOf(StrUtil.toString(value));
|
||||
} else if(Era.class == targetClass){
|
||||
return IsoEra.valueOf(StrUtil.toString(value));
|
||||
} else if(MonthDay.class == targetClass){
|
||||
return MonthDay.parse(value);
|
||||
}
|
||||
|
||||
final Instant instant;
|
||||
final ZoneId zoneId;
|
||||
if (null != this.format) {
|
||||
@ -139,6 +152,8 @@ public class TemporalAccessorConverter extends AbstractConverter {
|
||||
return Month.of(Math.toIntExact(time));
|
||||
} else if(targetClass == DayOfWeek.class){
|
||||
return DayOfWeek.of(Math.toIntExact(time));
|
||||
} else if(Era.class == targetClass){
|
||||
return IsoEra.of(Math.toIntExact(time));
|
||||
}
|
||||
|
||||
return parseFromInstant(targetClass, Instant.ofEpochMilli(time), null);
|
||||
@ -151,6 +166,14 @@ public class TemporalAccessorConverter extends AbstractConverter {
|
||||
* @return java.time中的对象
|
||||
*/
|
||||
private TemporalAccessor parseFromTemporalAccessor(final Class<?> targetClass, final TemporalAccessor temporalAccessor) {
|
||||
if(DayOfWeek.class == targetClass){
|
||||
return DayOfWeek.from(temporalAccessor);
|
||||
} else if(Month.class == targetClass){
|
||||
return Month.from(temporalAccessor);
|
||||
} else if(MonthDay.class == targetClass){
|
||||
return MonthDay.from(temporalAccessor);
|
||||
}
|
||||
|
||||
TemporalAccessor result = null;
|
||||
if (temporalAccessor instanceof LocalDateTime) {
|
||||
result = parseFromLocalDateTime(targetClass, (LocalDateTime) temporalAccessor);
|
||||
|
@ -3,15 +3,18 @@ package cn.hutool.core.date;
|
||||
import cn.hutool.core.date.format.GlobalCustomFormat;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
|
||||
import java.time.DayOfWeek;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.time.Month;
|
||||
import java.time.MonthDay;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.OffsetTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.chrono.Era;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.temporal.TemporalAccessor;
|
||||
import java.time.temporal.TemporalField;
|
||||
@ -94,7 +97,7 @@ public class TemporalAccessorUtil extends TemporalUtil{
|
||||
return null;
|
||||
}
|
||||
|
||||
if(time instanceof Month){
|
||||
if(time instanceof DayOfWeek || time instanceof java.time.Month || time instanceof Era || time instanceof MonthDay){
|
||||
return time.toString();
|
||||
}
|
||||
|
||||
|
@ -16,6 +16,9 @@ import cn.hutool.json.JSONException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.time.DayOfWeek;
|
||||
import java.time.MonthDay;
|
||||
import java.time.chrono.Era;
|
||||
import java.time.temporal.TemporalAccessor;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
@ -237,6 +240,14 @@ public class JSONWriter extends Writer {
|
||||
} else if (value instanceof Number) {
|
||||
writeNumberValue((Number) value);
|
||||
} else if (value instanceof Date || value instanceof Calendar || value instanceof TemporalAccessor) {
|
||||
// issue#2572@Github
|
||||
if(value instanceof TemporalAccessor){
|
||||
if(value instanceof DayOfWeek || value instanceof java.time.Month || value instanceof Era || value instanceof MonthDay){
|
||||
writeQuoteStrValue(value.toString());
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
final String format = (null == config) ? null : config.getDateFormat();
|
||||
//noinspection resource
|
||||
writeRaw(formatDate(value, format));
|
||||
|
53
hutool-json/src/test/java/cn/hutool/json/Issue2572Test.java
Executable file
53
hutool-json/src/test/java/cn/hutool/json/Issue2572Test.java
Executable file
@ -0,0 +1,53 @@
|
||||
package cn.hutool.json;
|
||||
|
||||
import cn.hutool.core.reflect.TypeReference;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.time.DayOfWeek;
|
||||
import java.time.Month;
|
||||
import java.time.MonthDay;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public class Issue2572Test {
|
||||
@Test
|
||||
public void putDayOfWeekTest(){
|
||||
final Set<DayOfWeek> weeks = new HashSet<>();
|
||||
weeks.add(DayOfWeek.MONDAY);
|
||||
final JSONObject obj = new JSONObject();
|
||||
obj.set("weeks", weeks);
|
||||
Assert.assertEquals("{\"weeks\":[\"MONDAY\"]}", obj.toString());
|
||||
|
||||
final Map<String, Set<DayOfWeek>> monthDays1 = obj.toBean(new TypeReference<Map<String, Set<DayOfWeek>>>() {
|
||||
});
|
||||
Assert.assertEquals("{weeks=[MONDAY]}", monthDays1.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void putMonthTest(){
|
||||
final Set<Month> months = new HashSet<>();
|
||||
months.add(Month.DECEMBER);
|
||||
final JSONObject obj = new JSONObject();
|
||||
obj.set("months", months);
|
||||
Assert.assertEquals("{\"months\":[\"DECEMBER\"]}", obj.toString());
|
||||
|
||||
final Map<String, Set<Month>> monthDays1 = obj.toBean(new TypeReference<Map<String, Set<Month>>>() {
|
||||
});
|
||||
Assert.assertEquals("{months=[DECEMBER]}", monthDays1.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void putMonthDayTest(){
|
||||
final Set<MonthDay> monthDays = new HashSet<>();
|
||||
monthDays.add(MonthDay.of(Month.DECEMBER, 1));
|
||||
final JSONObject obj = new JSONObject();
|
||||
obj.set("monthDays", monthDays);
|
||||
Assert.assertEquals("{\"monthDays\":[\"--12-01\"]}", obj.toString());
|
||||
|
||||
final Map<String, Set<MonthDay>> monthDays1 = obj.toBean(new TypeReference<Map<String, Set<MonthDay>>>() {
|
||||
});
|
||||
Assert.assertEquals("{monthDays=[--12-01]}", monthDays1.toString());
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user