Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.time.format.DateTimeParseException: Text '2016-2-2' could not be parsed at index 5

Tags:

java

date

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
like image 720
Sal-laS Avatar asked Nov 28 '25 15:11

Sal-laS


1 Answers

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);
like image 144
Reimeus Avatar answered Nov 30 '25 04:11

Reimeus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!