Я просто намагаюся перетворити рядок дати в об’єкт DateTime на Java 8. Після запуску наступних рядків:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
LocalDateTime dt = LocalDateTime.parse("20140218", formatter);
Я отримую таку помилку:
Exception in thread "main" java.time.format.DateTimeParseException:
Text '20140218' could not be parsed:
Unable to obtain LocalDateTime from TemporalAccessor:
{},ISO resolved to 2014-02-18 of type java.time.format.Parsed
at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1918)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1853)
at java.time.LocalDateTime.parse(LocalDateTime.java:492)
Синтаксис ідентичний тому, що було запропоновано тут , але мені подають виняток. Я використовую JDK-8u25.
LocalDateі ніLocalDateTime. Проблема полягала в тому, що я створив своєDateTimeFormatterвикористання.withResolverStyle(ResolverStyle.STRICT);, тому мені довелося використовувати шаблон датиuuuuMMddзамістьyyyyMMdd(тобто "рік" замість "рік ери")!