Dinesh Kumar
Dinesh Kumar

Reputation: 1499

LocalDateTime parsing throws "java.lang.IllegalArgumentException: Unknown pattern letter: T"

The below one works:

LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
LocalDateTime.now().plusDays(1).format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)

but the below item does not work.

LocalDateTime.now().format(DateTimeFormatter.ofPattern("YYYY-MM-DDTHH:mm:ss"))
LocalDateTime.parse("2019-11-14T16:48:48.288", DateTimeFormatter.ofPattern("YYYY-MM-DDTHH:mm:ss"));

LocalDateTime.now() gives me date like 2019-11-13T17:12:47.494. I have tried parsing it and verified online a lot to fix but no luck can someone help me to understand why the parsing is throwing exception and how to fix this.

Upvotes: 0

Views: 2478

Answers (1)

Eng.Fouad
Eng.Fouad

Reputation: 117627

You need to add single quotes '' around any literals:

LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"));
LocalDateTime.parse("2019-11-14T16:48:48.288", DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS"));

Also:

  • Use yyyy for year.
  • Use dd for day-of-month, instead of DD which is for day-of-year.
  • Use SSS for second fractions.
  • See DateTimeFormatter Javadoc for more information.

Upvotes: 2

Related Questions