0

I have a date '2021-06-19 14:57:23.0' which I need to change it to LocalDateTime type. When I parse, it throws Text could not be parsed at index 10.

String date = "2021-06-19 14:57:23.0";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
LocalDateTime date = LocalDateTime.parse(date, formatter);

What am I missing here? I have a string in a format which I am converting to a different format by specififying. Any advise would be appreciated. Thanks

Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93
challengeAccepted
  • 7,106
  • 20
  • 74
  • 105
  • 3
    Your String does not contain a T and only a space, and the last part is only 1 digit long. So i guess `yyyy-MM-dd HH:mm:ss.S` should work. – OH GOD SPIDERS Jul 07 '21 at 12:58
  • You are right. I need the date to be in specific format, "yyyy-MM-dd'T'HH:mm:ss.SSS". Is it possible to change? – challengeAccepted Jul 07 '21 at 13:03
  • 2
    But that's not the format of the string you have. Keep in mind that `LocalDateTime` does not have a format—it's only date/time data. – Slaw Jul 07 '21 at 13:04
  • 3
    You need to differenciate between the LocalDateTime object and the String representation of it. a LocalDateTime itself does not have a format, the format only comes into play when converting from or to Strings. So you can parse a LocalDateTime in one format and then create a String from the parsed LocalDateTIme with another format. – OH GOD SPIDERS Jul 07 '21 at 13:06
  • 1
    This is what I was waiting to learn I guess.. "LOCALDATETIME doesn't have a format, rather its a dateTime data." – challengeAccepted Jul 07 '21 at 13:11
  • Related, somehow opposite [Can’t rid of 'T' in LocalDateTime](https://stackoverflow.com/questions/52311884/can-t-rid-of-t-in-localdatetime). Also related: [Why am I getting a parse exception when I try to parse the current LocalDateTime \[duplicate\]](https://stackoverflow.com/questions/58012504/why-am-i-getting-a-parse-exception-when-i-try-to-parse-the-current-localdatetime). – Ole V.V. Jul 07 '21 at 13:50

2 Answers2

2

You have the wrong pattern for your date. The right pattern for you would be:

String date = "2021-06-19 14:57:23.0"; // declaring the date variable
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S"); // declaring the pattern. Here was your error. 
LocalDateTime date = LocalDateTime.parse(date, formatter);
Kleysley
  • 505
  • 3
  • 12
1

Two points that have more or less been covered in the comments, but which I thought deserved to go into an answer:

  1. A LocalDateTime hasn’t got, as in cannot have a format.
  2. To change from one string format to another you generally need two formatters, one for parsing the format that you have got and one for formatting into the desired format.

So you will need to decide whether you need a LocalDateTime or you need the format that you specified since you cannot have both in the same object. But: in your particular case you can almost. Stealing the code from the answer by Ctrl_see:

    String dateString = "2021-06-19 14:57:23.0";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.S"); 
    LocalDateTime dateTime = LocalDateTime.parse(dateString, formatter);
    
    System.out.println("Parsed date and time: " + dateTime);

Output is:

Parsed date and time: 2021-06-19T14:57:23

Only the three decimals that you asked for were not output. The format that we get from LocalDateTime.toString() (implicitly called when we concatenate the LocalDateTime to a string) is ISO 8601, and according to the ISO 8601 standard the decimals are optional when they are 0. So please check if the above isn’t fine for your purpose.

If you do need the decimals, as I said, we will need a second formatter to format back into a string having the desired format:

    DateTimeFormatter targetFormatter
            = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSS");
    String formattedDateTimeString = dateTime.format(targetFormatter);
    System.out.println(formattedDateTimeString);

2021-06-19T14:57:23.000

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161