修复LocalDateTimeUtil.of 某些特殊TemporalAccessor无法返回正确结果的问题

This commit is contained in:
Looly
2023-09-13 20:33:52 +08:00
parent 8ee13fb5bf
commit 111898c640
4 changed files with 49 additions and 10 deletions

View File

@@ -0,0 +1,22 @@
package cn.hutool.core.date;
import org.junit.Assert;
import org.junit.Test;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
public class Issue3301Test {
@Test
public void ofTest() {
final ZonedDateTime now = ZonedDateTime.now();
// 获得一个特殊的 temporal
String text = DateTimeFormatter.ISO_INSTANT.format(now);
TemporalAccessor temporal = DateTimeFormatter.ISO_INSTANT.parse(text);
LocalDateTime actual = LocalDateTimeUtil.of(temporal);
Assert.assertEquals(now.toLocalDateTime().toString(), actual.toString());
}
}

View File

@@ -1,6 +1,5 @@
package cn.hutool.core.date;
import cn.hutool.core.lang.Console;
import org.junit.Assert;
import org.junit.Test;
@@ -133,6 +132,13 @@ public class LocalDateTimeUtilTest {
Assert.assertEquals("2020-01-22T12:23:56", offset.toString());
}
@Test
public void ofTest2(){
final Instant instant = Objects.requireNonNull(DateUtil.parse("2022-02-22")).toInstant();
final LocalDateTime of = LocalDateTimeUtil.of((TemporalAccessor) instant);
Assert.assertEquals("2022-02-22T00:00", of.toString());
}
@Test
public void between() {
final Duration between = LocalDateTimeUtil.between(
@@ -281,13 +287,6 @@ public class LocalDateTimeUtilTest {
Assert.assertEquals(5, weekOfYear2);
}
@Test
public void ofTest2(){
final Instant instant = Objects.requireNonNull(DateUtil.parse("2022-02-22")).toInstant();
final LocalDateTime of = LocalDateTimeUtil.of((TemporalAccessor) instant);
Console.log(of);
}
@Test
public void parseBlankTest(){
final LocalDateTime parse = LocalDateTimeUtil.parse("");