Ami*_*dal 0 java datetime java-8
现在我真的很困惑,为什么以下代码片段导致DateTimeParseException。
public static void main(String[] args) {
java.time.format.DateTimeFormatter dtf = java.time.format.DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss zzz");
String date = "Mon, 10 Sep 2018 23:57:09 UTC";
System.out.println(dtf.parse(date));
}
Run Code Online (Sandbox Code Playgroud)
引发以下异常:
Exception in thread "main" java.time.format.DateTimeParseException: Text 'Mon, 10 Sep 2018 23:57:09 UTC' could not be parsed at index 2
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1777)
at com.sample.binding.bitronvideo.driver.BitronVideoRecordingDriver.main(BitronVideoRecordingDriver.java:448)
Run Code Online (Sandbox Code Playgroud)
我将非常感谢您的进一步帮助。
谢谢阿米特
我没有例外。因此,检查您的个人资料后,我发现您的语言环境在德国,因此我尝试了此操作并得到了例外。
java.time.format.DateTimeFormatter dtf =
java.time.format.DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss zzz",
Locale.GERMANY);
String date = "Mon, 10 Sep 2018 23:57:09 UTC";
System.out.println(dtf.parse(date));
Run Code Online (Sandbox Code Playgroud)
短工作日So,Mo,Di,Mi,Do,Fr,Sa
尝试使用此代码,我敢打赌它将起作用
java.time.format.DateTimeFormatter dtf =
java.time.format.DateTimeFormatter.ofPattern("EE, dd MMM yyyy HH:mm:ss zzz");
String date = "Mo, 10 Sep 2018 23:57:09 UTC";
System.out.println(dtf.parse(date));
Run Code Online (Sandbox Code Playgroud)
但是为了使String Date正常工作,只需通过传递参数即可使用UK或US Locale
java.time.format.DateTimeFormatter dtf =
java.time.format.DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss zzz",
Locale.UK);
String date = "Mon, 10 Sep 2018 23:57:09 UTC";
System.out.println(dtf.parse(date));
Run Code Online (Sandbox Code Playgroud)