2

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
Joe
  • 269
  • 3
  • 13

1 Answers1

1

Use

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

Here you specify the pattern of the DateTimeFormat

Nicholas K
  • 15,148
  • 7
  • 31
  • 57
  • 1
    I seriously hate that there's no proper wrapper for the SQL timestamp and I have to manually do this garbage. That being said thanks for the help! I'll accept it as soon as I can. – Joe Nov 30 '18 at 06:17
  • @Joe You shouldn’t fetch timestamp values as strings from your database. Depending on what your JDBC driver offers, see if you can fetch an `Instant`, an `OffsetDateTime` or a `LocalDateTime`. Use for example `yourResultSet.getObject(6, Instant.class)`. – Ole V.V. Nov 30 '18 at 09:27