当我使用SimpleDateFormat
时,它可以解析。为什么我的模式(“yyyyMM”)无法用DateTimeFormatter解析(java 8)
SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
format.setLenient(false);
Date d = format.parse(date);
但是当我使用Java 8 DateTimeFormatter
,
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMM");
LocalDate localDate = LocalDate.parse(date, formatter);
它抛出
java.time.format.DateTimeParseException:文本 '201510' 无法解析:无法获取来自TemporalAccessor的LocalDate:{Year = 2015,MonthOfYear = 10},类型为java的ISO .time.format.Parsed
日期的字符串值为"201510"
。
它不是'LocalDate'因为你需要指定一天。 201510应该代表哪个日期?那个月的第一个? – zapl
它适用于我,编译并运行jdk1.8.0_25 –