简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM