mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
fix date bug
This commit is contained in:
parent
78b1844590
commit
81dbc75ba3
@ -8,6 +8,7 @@
|
||||
### 新特性
|
||||
### Bug修复
|
||||
* 【setting】 修复Props.toBean方法null的问题
|
||||
* 【core 】 修复DataUtil.parseLocalDateTime无时间部分报错问题(issue#I1B18H@Gitee)
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
@ -17,6 +17,7 @@ import java.time.format.DateTimeFormatter;
|
||||
import java.time.temporal.TemporalAccessor;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* JDK8中新加入的java.time包对象解析转换器<br>
|
||||
@ -114,7 +115,7 @@ public class TemporalAccessorConverter extends AbstractConverter<TemporalAccesso
|
||||
zoneId = formatter.getZone();
|
||||
} else {
|
||||
final DateTime dateTime = DateUtil.parse(value);
|
||||
instant = dateTime.toInstant();
|
||||
instant = Objects.requireNonNull(dateTime).toInstant();
|
||||
zoneId = dateTime.getZoneId();
|
||||
}
|
||||
return parseFromInstant(instant, zoneId);
|
||||
|
@ -23,6 +23,7 @@ import java.time.OffsetTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.time.temporal.TemporalAccessor;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
@ -513,22 +514,23 @@ public class DateUtil {
|
||||
}
|
||||
|
||||
// ------------------------------------ Format start ----------------------------------------------
|
||||
|
||||
|
||||
/**
|
||||
* 格式化日期时间<br>
|
||||
* 格式 yyyy-MM-dd HH:mm:ss
|
||||
*
|
||||
*
|
||||
* @param localDateTime 被格式化的日期
|
||||
* @return 格式化后的字符串
|
||||
*/
|
||||
public static String formatLocalDateTime(LocalDateTime localDateTime) {
|
||||
return format(localDateTime, DatePattern.NORM_DATETIME_PATTERN);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 根据特定格式格式化日期
|
||||
*
|
||||
* @param localDateTime 被格式化的日期
|
||||
* @param format 日期格式,常用格式见: {@link DatePattern}
|
||||
* @param format 日期格式,常用格式见: {@link DatePattern}
|
||||
* @return 格式化后的字符串
|
||||
*/
|
||||
public static String format(LocalDateTime localDateTime, String format) {
|
||||
@ -538,7 +540,7 @@ public class DateUtil {
|
||||
DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
|
||||
return localDateTime.format(df);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 根据特定格式格式化日期
|
||||
*
|
||||
@ -693,30 +695,36 @@ public class DateUtil {
|
||||
// ------------------------------------ Format end ----------------------------------------------
|
||||
|
||||
// ------------------------------------ Parse start ----------------------------------------------
|
||||
|
||||
|
||||
/**
|
||||
* 构建LocalDateTime对象<br>
|
||||
* 格式:yyyy-MM-dd HH:mm:ss
|
||||
*
|
||||
*
|
||||
* @param dateStr 时间字符串(带格式)
|
||||
* @return LocalDateTime对象
|
||||
*/
|
||||
public static LocalDateTime parseLocalDateTime(CharSequence dateStr) {
|
||||
return parseLocalDateTime(dateStr, DatePattern.NORM_DATETIME_PATTERN);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 构建LocalDateTime对象
|
||||
*
|
||||
* @param dateStr 时间字符串(带格式)
|
||||
* @param format 使用{@link DatePattern}定义的格式
|
||||
* @param format 使用{@link DatePattern}定义的格式
|
||||
* @return LocalDateTime对象
|
||||
*/
|
||||
public static LocalDateTime parseLocalDateTime(CharSequence dateStr, String format) {
|
||||
dateStr = normalize(dateStr);
|
||||
DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
|
||||
return LocalDateTime.parse(dateStr,df);
|
||||
try {
|
||||
return LocalDateTime.parse(dateStr, df);
|
||||
} catch (DateTimeParseException e) {
|
||||
// 在给定日期字符串没有时间部分时,LocalDateTime会报错,此时使用LocalDate中转转换
|
||||
return LocalDate.parse(dateStr, df).atStartOfDay();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 构建DateTime对象
|
||||
*
|
||||
|
@ -680,4 +680,12 @@ public class DateUtilTest {
|
||||
strDate1 = DateUtil.format(ldt, DatePattern.NORM_DATETIME_PATTERN);
|
||||
Assert.assertEquals(strDate, strDate1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void localDateTimeTest2() {
|
||||
// 测试字符串与LocalDateTime的互相转换
|
||||
String strDate = "2019-12-01";
|
||||
final LocalDateTime localDateTime = DateUtil.parseLocalDateTime(strDate, "yyyy-MM-dd");
|
||||
Assert.assertEquals(strDate, DateUtil.format(localDateTime, DatePattern.NORM_DATE_PATTERN));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user