javadatetimelocaldateinvocationtargetexception

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.


Solution

  • 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: