我在使用 Java 中的 DateTimeFormatter 时遇到问题。
我有以下代码:
DateTimeFormatter format = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDateTime startDate = LocalDateTime.now();
LocalDateTime endDate = LocalDateTime.parse(ceremonyDetails.getDate(), format);
System.out.println(ChronoUnit.DAYS.between(startDate, endDate)); format);
应该从字符串中打印从现在到日期之间的天数,格式为“dd/MM/yyyy”,例如“29/09/2016”。
但是,我收到了这个错误:
java.time.format.DateTimeParseException: Text '29/09/2016' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {},ISO resolved to 2016-09-29 of type java.time.format.Parsed] with root cause java.time.DateTimeException: Unable to obtain LocalTime from TemporalAccessor: {},ISO resolved to 2016-09-29 of type java.time.format.Parsed
我错过了什么?
最佳答案
你应该使用 LocalDate
而不是 LocalDateTime
.一个用于仅日期值,另一个用于具有时间值的日期。
LocalDate.parse( "29/09/2016" , DateTimeFormatter.ofPattern("dd/MM/yyyy") )
请参阅 DateTimeFormatter
上的示例类文档页面。
关于java - DateTimeFormatter 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39655060/