Reputation: 185
I am parsing normal date into LocalDate formate.While converting I am getting One exception called.
Caused by: java.time.format.DateTimeParseException: Text '2017-01-12 00:00:00.0' could not be parsed at index 2 at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949) [rt.jar:1.8.0_111] at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851) [rt.jar:1.8.0_111] at java.time.LocalDate.parse(LocalDate.java:400) [rt.jar:1.8.0_111]
As I know I am getting the same formate.So while converting I am getting this exception.Am I correct. If I am correct How to check the input is the same format or not.
This is my code:-
DateTimeFormatter DATE_FORMAT = new DateTimeFormatterBuilder().appendPattern("dd/MM/yyyy").toFormatter();
LocalDate localDate = LocalDate.parse(myClass.getDate, DATE_FORMAT);
System.out.println(localDate.format(DATE_FORMAT));
Upvotes: 0
Views: 5413
Reputation: 44061
The pattern "dd/MM/yyyy" does not match your input. Use the pattern
"uuuu-MM-dd HH:mm:ss.S" instead. And also important: You should not parse such an input to a LocalDate
because your input has time information, too. A more edaquate type is LocalDateTime
. Full example making a difference between given input (as indicated by your exception) and wished output:
DateTimeFormatter DATE_FORMAT =
new DateTimeFormatterBuilder().appendPattern("uuuu-MM-dd HH:mm:ss.S").toFormatter();
LocalDateTime ldt = LocalDateTime.parse("2017-01-12 00:00:00.0", DATE_FORMAT);
System.out.println(ldt); // 2017-01-12T00:00
System.out.println(ldt.format(DateTimeFormatter.ofPattern("dd/MM/yyyy"))); // 12/01/2017
Upvotes: 2
Reputation: 48258
this pattern appendPattern("dd/MM/yyyy") can not format this string 2017-01-12 00:00:00.0
use instead appendPattern("yyyy-MM-dd HH:mm.ss.S")
Upvotes: 0