DateTimeParseException在Java 11上但在Java 10上有效 [英] DateTimeParseException on Java 11 but works on Java 10

查看:80
本文介绍了DateTimeParseException在Java 11上但在Java 10上有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下测试用例可以在Java 10上完美运行:

The following testcase runs perfectly under Java 10:

import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;

class Test
{
    public static void main (String[] args) throws java.lang.Exception
    {
        DateTimeFormatter dateFormatter = new DateTimeFormatterBuilder().
          appendPattern("EEE, dd MMM yyyy HH:mm:ss zzz").
          toFormatter();
        Instant result = dateFormatter.parse("Sat, 29 Sep 2018 20:49:02 GMT", Instant::from);
        System.out.println("Result: " + result);
    }
}

但是在Java 11下,我得到了:

but under Java 11 I get:

Exception in thread "main" java.time.format.DateTimeParseException: Text 'Sat, 29 Sep 2018 20:49:02 GMT' could not be parsed at index 0
        at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2046)
        at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948)
        at Test.main(Test.java:13)

这是怎么回事?

更新:将toFormatter()替换为toFormatter(Locale.US)可解决此问题.我猜这个问题与 https://bugs.openjdk.java.net/有关浏览/JDK-8206980 .这个问题在Java 11 build 23中被标记为已修复,但是我正在运行

UPDATE: Replacing toFormatter() with toFormatter(Locale.US) fixes the problem. I am guessing this issue is related to https://bugs.openjdk.java.net/browse/JDK-8206980. This issue is marked as fixed in Java 11 build 23 but I am running

openjdk version "11" 2018-09-25
OpenJDK Runtime Environment 18.9 (build 11+28)
OpenJDK 64-Bit Server VM 18.9 (build 11+28, mixed mode)

这个版本不应该解决这个问题吗?

Shouldn't this be fixed in this version?

UPDATE2 :如果无法重现该问题,请尝试将toFormatter()替换为toFormatter(Locale.CANADA).

UPDATE2: If you are unable to reproduce the problem, try replacing toFormatter() with toFormatter(Locale.CANADA).

推荐答案

DateTimeFormatter.RFC_1123_DATE_TIME

您的日期时间字符串位于 RFC 822 /

DateTimeFormatter.RFC_1123_DATE_TIME

Your date-time string is in RFC 822/RFC 1123 format. Instead of building your own formatter use the built-in DateTimeFormatter.RFC_1123_DATE_TIME:

    Instant result = DateTimeFormatter.RFC_1123_DATE_TIME
            .parse("Sat, 29 Sep 2018 20:49:02 GMT", Instant::from);
    System.out.println("Result: " + result);

输出为:

结果:2018-09-29T20:49:02Z

Result: 2018-09-29T20:49:02Z

根据RFC规范的要求,此RFC 1123格式化程序始终为英文.我什至尝试将默认语言环境设置为 Locale.CANADA_FRENCH ,并且代码仍然有效.

This RFC 1123 formatter is always in English, as required by the RFC specification. I even tried setting my default locale to Locale.CANADA_FRENCH, and the code still worked.

在Java 11中,Java希望星期几和月份的缩写在Locale.CANADA中用一个点写:Sat.Sep.而不是SatSep.在Java 10中,它们应不带点,因此在这里进行解析.区别可能在于CLDR数据的不同版本,在任何上述Java版本中都很难将其视为错误.由于Java 9 CLDR一直是Java中的默认语言环境数据-包括不同语言环境中的日期和月份缩写.

In Java 11 Java expects that the abbreviations for day of week and for month are written with a dot in Locale.CANADA: Sat. and Sep. rather than Sat and Sep. In Java 10 they are expected without dots, so here parsing works. The difference probably lies in different versions of CLDR data and can hardly be considered a bug in any of the mentioned Java versions. Since Java 9 CLDR has been the default locale data in Java — including what day and month abbreviations look like in different locales.

演示:使用格式化程序,但按照您所说的那样使用Locale.CANADA对其进行修改:

Demonstration: Using your formatter, but modifying it to use Locale.CANADA as you said:

    DateTimeFormatter dateFormatter = new DateTimeFormatterBuilder().
            appendPattern("EEE, dd MMM yyyy HH:mm:ss zzz").
            toFormatter(Locale.CANADA);
    System.out.println("Sample: " + ZonedDateTime.now(ZoneId.of("America/Toronto"))
            .format(dateFormatter));

在Java 10.0.2上运行,不会打印任何点:

Running on Java 10.0.2 this printed no dots:

示例:Sun,2018年9月30日10:39:28 EDT

Sample: Sun, 30 Sep 2018 10:39:28 EDT

在Java 11 build 11 + 28上:

And on Java 11 build 11+28:

示例:2018年9月30日,星期日10:50:29 EDT

Sample: Sun., 30 Sep. 2018 10:50:29 EDT

因此,我认为该行为与您链接到的错误报告无关.

So I believe that the behaviour has nothing to do with the bug report you linked to.

  • DateTimeFormatter.RFC_1123_DATE_TIME documentation
  • CLDR - Unicode Common Locale Data Repository home page
  • JDK Bug System: DateTimeFormatter throws parsing a valid string depending on the locale

这篇关于DateTimeParseException在Java 11上但在Java 10上有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆