-1

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?

Roshan Upreti
  • 1,782
  • 3
  • 21
  • 34
  • 1
    [`LocalDateTime`](https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html) is a date/time *value*, it doesn't have a format. If you **read the documentation**, i.e. the javadoc of [`toString()`](https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html#toString--), you will see that it always renders the date/time value using the `T` separator. If you want the date/time value in a different format, use the [`format(DateTimeFormatter)`](https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html#format-java.time.format.DateTimeFormatter-) method. – Andreas Apr 14 '20 at 08:53
  • 1
    Same for the [`parse(String)`](https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html#parse-java.lang.CharSequence-) method. It can only parse strings with the `T` separator. If you want to parse a text in a different format, use the [`parse(String, DateTimeFormatter)`](https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html#parse-java.lang.CharSequence-java.time.format.DateTimeFormatter-) method. – Andreas Apr 14 '20 at 08:55

2 Answers2

1

LocalDateTime.parse(formattedDate) are using DateTimeFormatter.ISO_LOCAL_DATE_TIME (that is format yyyy-MM-dd'T'HH:mm:ss). That's why you get the exception when trying to parse string that has format yyyy-MM-dd HH:mm:ss. You should use:

LocalDateTime.parse(formattedDate, outputFormatter) if you wnat to do the parse to LocalDateTime again for some reason.

Note: You have the printed format you at line: outputFormatter.format(date1) right?

akcode
  • 76
  • 3
  • using `LocalDateTime.parse(formattedDate, outputFormatter)` gives me the datetime like `2020-03-10T15:14:05` – Roshan Upreti Apr 14 '20 at 08:27
  • 1
    What do you wnat exactly? you have the format you want on this line `outputFormatter.format(date1)` right? – akcode Apr 14 '20 at 08:35
  • `outputFormatter.format(date1)` has the datetime format I want in String, and I want to parse that to actual DateTime. – Roshan Upreti Apr 14 '20 at 08:38
  • That you have already on your 3rd line `LocalDateTime date1 = LocalDateTime.parse("2020-03-10T15:14:05", inputFormatter);` or do you want it to a `ZonedDateTime` or what? (LocalDateTime is a DateTime in the current timezone) – akcode Apr 14 '20 at 08:42
1

You seem to be confused between LocalDateTime and format (which is a string representation).

A LocalDateTime always has T in it when you print its object using System.out.println (which implicitly calls toString as you most likely already know) e.g.

System.out.println(LocalDateTime.now());

will output 2020-04-14T09:36:04.723994.

See below how the toString of LocalDateTime has been implemented:

@Override
public String toString() {
    return date.toString() + 'T' + time.toString();
}

and therefore your following statement will always show 'T' in it:

System.out.println(newFormattedDateTime);

It's up to you to format a LocalDateTime into the String representation of your choice. As I have mentioned in the first line, formats are strings i.e. you format a LocalDateTime into a String representation where you can apply all the options provided by DateTimeFormatter.

The correct way of converting the formattedDate to LocalDateTime is by applying the corresponding format which is specified in outputFormatter.

LocalDateTime newFormattedDateTime = LocalDateTime.parse(formattedDate,outputFormatter);

How date and time are stored in a LocalDateTime object shouldn't be a concern. We can always create the string in the required format from it.

halfer
  • 19,824
  • 17
  • 99
  • 186
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110