2016-03-08 51 views
2

我試圖從import java.time.LocalDate; 使LocalDate一個實例,我跟着thisjava.time.format.DateTimeParseException:文本「2016年2月2日」未能在指數解析5

,這裏是我的代碼:

 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 
+2

您提供的鏈接是關於'org.joda.time.LocalDate'不是Java的' .time.LocalDate' – beresfordt

回答

4

您需要使用格式化解析單個字符日/月字段java.time.LocalDate

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-M-d"); 
LocalDate date = LocalDate.parse("2016-2-2", formatter); 
相關問題