date Java解析字符串到LocalDateTime而不提供时间
基本上我有一个领域
private LocalDateTime date;
这是从字符串中解析出来的。当提供的字符串末尾追加了时间时,它可以正常工作,但如果没有,它将抛出异常
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%确定格式化程序是由第二个条件分支设置的
# 1 楼答案
你的问题是LocalDateTime需要时间
您有两个主要选择:
LocalDateTime d = LocalDate.parse(value, formatter).atStartOfDay()
(例如)第二个选项可能看起来像:
它可以解析
2016-10-01
和2016-10-01 10:15