我对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:文本'29/09/2016‘无法解析:无法从TemporalAccessor获得LocalDateTime:{},LocalDateTime已解析为java.time.format.Parsed类型的2016-09-29 ],其根本原因是java.time.DateTimeException:无法从TemporalAccessor获得LocalTime:{},ISO解析为java.time.format.Parsed类型的2016-09-29
我错过了什么?
发布于 2016-09-23 07:37:08
您应该使用LocalDate而不是LocalDateTime。一种是指纯日期的值,另一种是指具有日期的日期.
LocalDate.parse( "29/09/2016" , DateTimeFormatter.ofPattern("dd/MM/yyyy") )请参阅DateTimeFormatter类文档页面中的示例。
https://stackoverflow.com/questions/39655060
复制相似问题