I'm trying to format Threeten datetime, from yyyy-MM-dd'T'HH:mm:ss
to yyyy-MM-dd HH:mm:ss
. Below is the code, I'm using to achieve the task.
public void testChangeFormat() {
DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime date1 = LocalDateTime.parse("2020-03-10T15:14:05", inputFormatter);
System.out.println(date1); // prints 2020-03-10T15:14:05
String formattedDate = outputFormatter.format(date1);
System.out.println(formattedDate); // prints 2020-03-10 15:14:05
LocalDateTime newFormattedDateTime = LocalDateTime.parse(formattedDate);
System.out.println(newFormattedDateTime);
}
Everything seems to work as expected until I try to parse the formattedDate
to LocalDateTime, at LocalDateTime newFormattedDateTime = LocalDateTime.parse(formattedDate);
I even get the datetime formatted as 2020-03-10 15:14:05
using outputFormatter
, but when I try to parse that to LocalDateTime, it gives me the following exception:
org.threeten.bp.format.DateTimeParseException: Text '2020-03-10 15:14:05' could not be parsed at index 10
Can somebody help me with this?