Why am I getting a parse exception when I try to parse the current LocalDateTime [duplicate]

你说的曾经没有我的故事 提交于 2021-02-05 12:36:45

问题


I am trying to parse a LocalDate with a custom formatter but it is throwing following error. What am I doing wrong?

Error:

// The local date time now value is 2019-09-19T14:42:23.837 java.time.format.DateTimeParseException: Text '2019-09-19T14:42:23.837' could not be parsed at index 10

Code:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
        LocalDateTime timeStamp = LocalDateTime.parse(LocalDateTime.now().toString(), formatter);

回答1:


You can change it to ISO_DATE_TIME which gives you the "T"

DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;

        LocalDateTime timeStamp=  LocalDateTime.parse(LocalDateTime.now().toString(),formatter);
        System.out.println(timeStamp);



回答2:


You are using a wrong formatter pattern, you should use ISO date-time formatter:

DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;


来源:https://stackoverflow.com/questions/58012504/why-am-i-getting-a-parse-exception-when-i-try-to-parse-the-current-localdatetime

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!