java - DateTimeFormatter 异常

标签 java

我在使用 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/

相关文章:

java - 如何通过Java检查oracle中是否存在给定名称的表?

java - XML 的 StAX 格式正确性检查

java - 使用java的网页加载时间

java - 计算处理中沿线的旋转角度

java - 如何使用 Exception.printStackTrace(); 包含时间?

java - 在 O(log(N)) 时间内查找排序数组中一定范围内的整数数量的高效算法?

java - 模拟 RestTemplate API 调用

java - 使用图像向 JavaFX 按钮添加填充

java - 将自定义测试源文件夹添加到 eclipse 项目 View

java.io.StreamCorruptedException : invalid stream header: 00000001