mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
修复LocalDateTimeUtil.of 某些特殊TemporalAccessor无法返回正确结果的问题
This commit is contained in:
parent
e7d45eb9e0
commit
a04775781a
@ -167,10 +167,27 @@ public class TimeUtil extends TemporalAccessorUtil {
|
|||||||
|
|
||||||
if (temporalAccessor instanceof LocalDate) {
|
if (temporalAccessor instanceof LocalDate) {
|
||||||
return ((LocalDate) temporalAccessor).atStartOfDay();
|
return ((LocalDate) temporalAccessor).atStartOfDay();
|
||||||
} else if (temporalAccessor instanceof Instant) {
|
} else if(temporalAccessor instanceof Instant){
|
||||||
return LocalDateTime.ofInstant((Instant) temporalAccessor, ZoneId.systemDefault());
|
return LocalDateTime.ofInstant((Instant) temporalAccessor, ZoneId.systemDefault());
|
||||||
} else if (temporalAccessor instanceof ZonedDateTime) {
|
}
|
||||||
return ((ZonedDateTime) temporalAccessor).toLocalDateTime();
|
|
||||||
|
// issue#3301
|
||||||
|
try{
|
||||||
|
return LocalDateTime.from(temporalAccessor);
|
||||||
|
} catch (final Exception ignore){
|
||||||
|
//ignore
|
||||||
|
}
|
||||||
|
|
||||||
|
try{
|
||||||
|
return ZonedDateTime.from(temporalAccessor).toLocalDateTime();
|
||||||
|
} catch (final Exception ignore){
|
||||||
|
//ignore
|
||||||
|
}
|
||||||
|
|
||||||
|
try{
|
||||||
|
return LocalDateTime.ofInstant(Instant.from(temporalAccessor), ZoneId.systemDefault());
|
||||||
|
} catch (final Exception ignore){
|
||||||
|
//ignore
|
||||||
}
|
}
|
||||||
|
|
||||||
return LocalDateTime.of(
|
return LocalDateTime.of(
|
||||||
|
@ -0,0 +1,22 @@
|
|||||||
|
package org.dromara.hutool.core.date;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.ZonedDateTime;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
import java.time.temporal.TemporalAccessor;
|
||||||
|
|
||||||
|
public class Issue3301Test {
|
||||||
|
@Test
|
||||||
|
void ofTest() {
|
||||||
|
final ZonedDateTime now = ZonedDateTime.now();
|
||||||
|
// 获得一个特殊的 temporal
|
||||||
|
final String text = DateTimeFormatter.ISO_INSTANT.format(now);
|
||||||
|
final TemporalAccessor temporal = DateTimeFormatter.ISO_INSTANT.parse(text);
|
||||||
|
|
||||||
|
final LocalDateTime actual = TimeUtil.of(temporal);
|
||||||
|
Assertions.assertEquals(now.toLocalDateTime().toString(), actual.toString());
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user