Search code examples
javadatetimetimestamplocaltime

Error parsing datetime, DateTimeParseException with valid timestamp


I'm trying to parse a timestamp object from sql and am having problems, heres what I have:

 DateTimeFormatter dateDTF = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);


 String start = c.getStartTime().getValue();
 //Start: 2018-12-01 16:00:00
 System.out.println("Start: " + start);
 LocalDateTime startDatetime = LocalDateTime.parse(start, dateDTF);

As you can see in the commented code my with the value of start to trim down my working example.I'm getting the following error:

java.time.format.DateTimeParseException: Text '2018-12-01 16:00:00' could not be parsed at index 4

Solution

  • Use

    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    

    Here you specify the pattern of the DateTimeFormat