java.time.format.DateTimeParseException: Text 'Tue Jan 08 00:00:00 IST 2019' could not be parsed at index 0 [duplicate]

浪尽此生 提交于 2021-02-11 13:32:53

问题


I am facing DateTimeParseException even after giving appropriate format

DateTimeFormatter ft = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss z yyyy");
LocalDateTime.parse("Tue Jan 08 00:00:00 IST 2019", ft);

Please help if I am missing anything?


回答1:


This is probably due to the locale setting on your computer. You might provide a Locale when creating the DateTimeFormatter.

DateTimeFormatter ft = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss z yyyy", Locale.US);

This will ensure that the date is always parsed right.




回答2:


This could be caused by your system locale not using Mon-Sun for week day short names, e.g. same exception will be thrown for German locale:

Locale.setDefault(Locale.GERMAN);
DateTimeFormatter ft = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss z yyyy");
LocalDateTime.parse("Tue Jan 08 00:00:00 IST 2019", ft);

The code will work if you use the matching locale e.g. US:

Locale.setDefault(Locale.US);
DateTimeFormatter ft = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss z yyyy");
LocalDateTime.parse("Tue Jan 08 00:00:00 IST 2019", ft);


来源:https://stackoverflow.com/questions/54095470/java-time-format-datetimeparseexception-text-tue-jan-08-000000-ist-2019-cou

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