java.time.format.DateTimeParseException:无法在索引5处解析文本“2016-2-2”
我试图从import java.time.LocalDate;
创建一个LocalDate
实例import java.time.LocalDate;
我跟着这个
这是我的代码:
LocalDate sd= LocalDate.parse("2016-2-2");
我遇到了错误:
java.time.format.DateTimeParseException: Text '2016-2-2' could not be parsed at index 5 at java.time.format.DateTimeFormatter.parseResolved0(Unknown Source)
在另一个尝试创建LocalDate
的实例,我试过
LocalDate ed= new LocalDate("2016-2-4");
但它再次抱怨:
The constructor LocalDate(String) is undefined
您需要使用格式化程序来解析java.time.LocalDate
单个字符日/月字段
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-Md"); LocalDate date = LocalDate.parse("2016-2-2", formatter);
LocalDate根本无法解析该字符串。 请改用SimpleDateFormat: https : //docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html