mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix parse bug
This commit is contained in:
parent
b5036b4b0b
commit
a5ea0ef1ed
@ -13,6 +13,7 @@
|
||||
### 🐞Bug修复
|
||||
* 【core 】 修复CollUtil.isEqualList两个null返回错误问题(issue#1885@Github)
|
||||
* 【poi 】 修复ExcelWriter多余调试信息导致的问题(issue#1884@Github)
|
||||
* 【poi 】 修复TemporalAccessorUtil.toInstant使用DateTimeFormatter导致问题(issue#1891@Github)
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
@ -167,7 +167,7 @@ public class DateTime extends Date {
|
||||
* @since 5.0.5
|
||||
*/
|
||||
public DateTime(Instant instant, ZoneId zoneId) {
|
||||
this(instant.toEpochMilli(), TimeZone.getTimeZone(ObjectUtil.defaultIfNull(zoneId, ZoneId.systemDefault())));
|
||||
this(instant.toEpochMilli(), ZoneUtil.toTimeZone(zoneId));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -177,7 +177,7 @@ public class DateTime extends Date {
|
||||
* @since 5.0.0
|
||||
*/
|
||||
public DateTime(TemporalAccessor temporalAccessor) {
|
||||
this(DateUtil.toInstant(temporalAccessor));
|
||||
this(TemporalAccessorUtil.toInstant(temporalAccessor));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -276,7 +276,7 @@ public class DateTime extends Date {
|
||||
* @since 5.0.0
|
||||
*/
|
||||
public DateTime(CharSequence dateStr, DateTimeFormatter formatter) {
|
||||
this(Instant.from(formatter.parse(dateStr)), formatter.getZone());
|
||||
this(TemporalAccessorUtil.toInstant(formatter.parse(dateStr)), formatter.getZone());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -138,7 +138,10 @@ public class TemporalAccessorUtil extends TemporalUtil{
|
||||
// 指定本地时间转换 为Instant,取当天日期
|
||||
result = ((OffsetTime) temporalAccessor).atDate(LocalDate.now()).toInstant();
|
||||
} else {
|
||||
result = Instant.from(temporalAccessor);
|
||||
// issue#1891@Github
|
||||
// Instant.from不能完成日期转换
|
||||
//result = Instant.from(temporalAccessor);
|
||||
result = toInstant(LocalDateTimeUtil.of(temporalAccessor));
|
||||
}
|
||||
|
||||
return result;
|
||||
|
41
hutool-core/src/main/java/cn/hutool/core/date/ZoneUtil.java
Normal file
41
hutool-core/src/main/java/cn/hutool/core/date/ZoneUtil.java
Normal file
@ -0,0 +1,41 @@
|
||||
package cn.hutool.core.date;
|
||||
|
||||
import java.time.ZoneId;
|
||||
import java.util.TimeZone;
|
||||
|
||||
/**
|
||||
* {@link ZoneId}和{@link TimeZone}相关封装
|
||||
*
|
||||
* @author looly
|
||||
* @since 5.7.15
|
||||
*/
|
||||
public class ZoneUtil {
|
||||
|
||||
/**
|
||||
* {@link ZoneId}转换为{@link TimeZone},{@code null}则返回系统默认值
|
||||
*
|
||||
* @param zoneId {@link ZoneId},{@code null}则返回系统默认值
|
||||
* @return {@link TimeZone}
|
||||
*/
|
||||
public static TimeZone toTimeZone(ZoneId zoneId) {
|
||||
if (null == zoneId) {
|
||||
return TimeZone.getDefault();
|
||||
}
|
||||
|
||||
return TimeZone.getTimeZone(zoneId);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link TimeZone}转换为{@link ZoneId},{@code null}则返回系统默认值
|
||||
*
|
||||
* @param timeZone {@link TimeZone},{@code null}则返回系统默认值
|
||||
* @return {@link ZoneId}
|
||||
*/
|
||||
public static ZoneId toZoneId(TimeZone timeZone) {
|
||||
if (null == timeZone) {
|
||||
return ZoneId.systemDefault();
|
||||
}
|
||||
|
||||
return timeZone.toZoneId();
|
||||
}
|
||||
}
|
@ -989,4 +989,10 @@ public class DateUtilTest {
|
||||
Assert.assertNotNull(parse);
|
||||
Assert.assertEquals("2021-01-01 00:00:00", parse.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseByDateTimeFormatterTest(){
|
||||
final DateTime parse = DateUtil.parse("2021-12-01", DatePattern.NORM_DATE_FORMATTER);
|
||||
Assert.assertEquals("2021-12-01 00:00:00", parse.toString());
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user