DateTimeParseException

DateTimeParseException

本文介绍了DateTimeParseException:无法在索引2处解析文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在真的很困惑,为什么以下代码片段导致DateTimeParseException.

I am really confused now why the following snippet results in 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));
}

抛出以下异常:

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)

我非常感谢您提供进一步的帮助.

I would really appreciate further help.

谢谢,阿米特(Amit)

Thanks,Amit

推荐答案

我没有收到异常.因此,检查您的个人资料后,我发现您的语言环境在德国,因此我尝试了此操作并得到了例外.

I didn't get the exception. So Checking your profile I saw that your locale is in Germany so i tried this and got the exception.

    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));

德语的学习日是:

尝试使用此代码,我敢打赌它将起作用

Try with this code and I bet it will work

    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));

但是要使您的字符串日期有效,只需通过传递参数即可使用英国或美国语言环境

But for your String Date to work just use UK or US Locale by passing an argument

    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));

这篇关于DateTimeParseException:无法在索引2处解析文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-18 08:33