今天使用Java8的日期类LocalDateTime和DateTimeFormatter处理String型的日期类型yyyy.MM.dd
String validDate = “2008.04.30-2018.04.30”;
String end = validDate.substring(validDate.indexOf("-")+1, validDate.length());
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(“yyyy.MM.dd”);
LocalDateTime localDateTime = LocalDateTime.parse(end, formatter);
System.out.println(localDateTime);
报了一个异常:
Exception in thread “main” java.time.format.DateTimeParseException: Text ‘2018.04.30’ could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {},ISO resolved to 2018-04-30 of type java.time.format.Parsed
at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1920)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1855)
at java.time.LocalDateTime.parse(LocalDateTime.java:492)
at com.fqgj.jkzj.xjopenapi.service.impl.user.ZyxjUserServiceImpl.main(ZyxjUserServiceImpl.java:211)
Caused by: java.time.DateTimeException: Unable to obtain LocalDateTime from TemporalAccessor: {},ISO resolved to 2018-04-30 of type java.time.format.Parsed
at java.time.LocalDateTime.from(LocalDateTime.java:461)
at java.time.format.Parsed.query(Parsed.java:226)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
… 2 more
Caused by: java.time.DateTimeException: Unable to obtain LocalTime from TemporalAccessor: {},ISO resolved to 2018-04-30 of type java.time.format.Parsed
at java.time.LocalTime.from(LocalTime.java:409)
at java.time.LocalDateTime.from(LocalDateTime.java:457)
… 4 more
日期转换异常,不能从TemporalAccessor中获取LocalDateTime类型的日期值。