3

I am trying to make an instance of LocalDate from import java.time.LocalDate; and i follow this

and here is my code:

     LocalDate sd=  LocalDate.parse("2016-2-2");

and i faced with the error:

java.time.format.DateTimeParseException: Text '2016-2-2' could not be parsed at index 5
    at java.time.format.DateTimeFormatter.parseResolved0(Unknown Source)

In another try to make an instance of LocalDate, i tried

LocalDate ed=  new LocalDate("2016-2-4");

but it again complains:

The constructor LocalDate(String) is undefined
Community
  • 1
  • 1
Sal-laS
  • 11,016
  • 25
  • 99
  • 169

2 Answers2

5

You need to use a formatter to parse single character day/month fields for java.time.LocalDate

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-M-d");
LocalDate date = LocalDate.parse("2016-2-2", formatter);
Reimeus
  • 158,255
  • 15
  • 216
  • 276
-7

LocalDate simply can't parse that string. Use SimpleDateFormat instead: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

Sason Ohanian
  • 795
  • 5
  • 16
  • 7
    LocalDate *can* parse that string, and recommending to use an obsolete class is not very helpful. – assylias Mar 09 '16 at 08:08