问题
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