Java将String解析为LocalDateTime而不提供时间

标签 java date datetime

基本上我有一个领域

private LocalDateTime date;

是从字符串中解析出来的。当提供的 String 末尾附加了时间时,它可以正常工作,但如果没有附加时间,则会抛出异常。

DateTimeFormatter formatter = null;
if (value.matches("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}")) {
    formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
} else if (value.matches("\\d{4}-\\d{2}-\\d{2}")) {
    formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
}
// Throws an exception for pattern "yyyy-MM-dd"
date = LocalDateTime.parse(value, formatter);

以及异常本身:

java.time.format.DateTimeParseException: Text '2000-06-29' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {},ISO resolved to 2000-06-29 of type java.time.format.Parsed
at pl.empirica.swissborg.service.util.CsvBeanUtils.copyList(CsvBeanUtils.java:75) ~[classes/:na]
at pl.empirica.swissborg.service.csv.CsvReaderService.persistCsvData(CsvReaderService.java:101) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_91]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_91]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_91]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_91]
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:354) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:305) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:133) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
... 18 common frames omitted
Caused by: java.lang.RuntimeException: java.time.format.DateTimeParseException: Text '2000-06-29' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {},ISO resolved to 2000-06-29 of type java.time.format.Parsed
at pl.empirica.swissborg.service.util.CsvBeanUtils.copyFields(CsvBeanUtils.java:58) ~[classes/:na]
at pl.empirica.swissborg.service.util.CsvBeanUtils.copyList(CsvBeanUtils.java:70) ~[classes/:na]
... 26 common frames omitted
Caused by: java.time.format.DateTimeParseException: Text '2000-06-29' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {},ISO resolved to 2000-06-29 of type java.time.format.Parsed
at java.time.format.DateTimeFormatter.createError(Unknown Source) ~[na:1.8.0_91]
at java.time.format.DateTimeFormatter.parse(Unknown Source) ~[na:1.8.0_91]
at java.time.LocalDateTime.parse(Unknown Source) ~[na:1.8.0_91]
at pl.empirica.swissborg.service.util.CsvBeanUtils.copyFields(CsvBeanUtils.java:47) ~[classes/:na]
... 27 common frames omitted
Caused by: java.time.DateTimeException: Unable to obtain LocalDateTime from TemporalAccessor: {},ISO resolved to 2000-06-29 of type java.time.format.Parsed
at java.time.LocalDateTime.from(Unknown Source) ~[na:1.8.0_91]
at java.time.format.Parsed.query(Unknown Source) ~[na:1.8.0_91]
... 30 common frames omitted
Caused by: java.time.DateTimeException: Unable to obtain LocalTime from TemporalAccessor: {},ISO resolved to 2000-06-29 of type java.time.format.Parsed
at java.time.LocalTime.from(Unknown Source) ~[na:1.8.0_91]
... 32 common frames omitted

编辑:为了澄清,我 100% 确定格式化程序是由第二个条件分支设置的。

最佳答案

您的问题是 LocalDateTime 需要时间!

您有两个主要选择:

  • 像您一样使用两个格式化程序,但第二个分支应该是LocalDateTime d = LocalDate.parse(value, formatter).atStartOfDay()(例如)
  • 创建一个可以处理这两种格式的格式化程序

第二个选项可能如下所示:

DateTimeFormatter fmt = new DateTimeFormatterBuilder()
        .appendPattern("yyyy-MM-dd")
        .optionalStart()
        .appendPattern(" HH:mm")
        .optionalEnd()
        .parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
        .parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)
        .toFormatter();

可以解析2016-10-012016-10-01 10:15

关于Java将String解析为LocalDateTime而不提供时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40175196/

相关文章:

date - 在 Go 中解析 RFC-3339/ISO-8601 日期时间字符串

excel - 将两个日期之间的天数计算为每天发生一次

javascript - 将时间插件更改为 24 小时

python - Pandas:转换一系列时间戳,仅保留 YYYY-MM-DD 格式

r - 将日期转换为该周的星期一

java - OpenJDK 发行说明 : does not include jar specification?

java - 子窗口关闭后是否可以在父窗口中执行操作JavaFX

java - 哪种方法更好地比较方法检查中的值或检查它是否存在于数组中?

java哈希函数冲突

java - 无法解析自定义Java日期: Can I set it to a default year?