ZonedDateTime.parse() 在 Windows 中对完全相同的字符串失败,但在 Mac 和 Linux 中有效

ZonedDateTime.parse() fails for exact the same string in Windows but works in Mac and Linux

以下 UnitTest 在 Windows 机器(Java 11.0.9)中通过 IntelliJ IDE 执行时失败,但在 Mac 或Linux机器与Java相同版本。

@Test
public void rfc1123JaveTimeUtilParsing(){
    final String rfc1123Pattern = "EEE, dd MMM yyyy HH:mm:ss z";
    final String responseTimeStamp = "Mon, 14 Dec 2020 20:34:37 GMT";

    DateTimeFormatter javaTimeDateTimeFormatter = DateTimeFormatter.ofPattern(rfc1123Pattern);
    ZonedDateTime javaFinalTime = ZonedDateTime.parse(responseTimeStamp, javaTimeDateTimeFormatter);
    Assert.assertNotNull(javaFinalTime);
}

对于 windows 结果是以下异常:

java.time.format.DateTimeParseException:无法在索引 0

处解析文本 'Mon, 14 Dec 2020 20:34:37 GMT'

切勿在没有 Locale 的情况下使用 DateTimeFormatter

由于给定的日期时间是英文的,您应该使用 DateTimeFormatterLocale.ENGLISH

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        final String responseTimeStamp = "Mon, 14 Dec 2020 20:34:37 GMT";
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);
        ZonedDateTime zdt = ZonedDateTime.parse(responseTimeStamp, dtf);
        System.out.println(zdt);
    }
}

输出:

2020-12-14T20:34:37Z[GMT]

默认情况下,DateTimeFormatter#ofPattern uses the default FORMAT locale JVM 根据主机环境在启动时设置的。我试图通过以下演示来说明问题:

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        final String responseTimeStamp = "Mon, 14 Dec 2020 20:34:37 GMT";
        DateTimeFormatter dtfWithDefaultLocale = null;

        System.out.println("JVM's Locale: " + Locale.getDefault());
        // DateTimeFormatter with the default Locale
        dtfWithDefaultLocale = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss z");
        System.out.println("DateTimeFormatter's Locale: " + dtfWithDefaultLocale.getLocale());
        System.out.println(
                "Parsed with JVM's default locale: " + ZonedDateTime.parse(responseTimeStamp, dtfWithDefaultLocale));

        // DateTimeFormatter with Locale.ENGLISH explicitly (recommended)
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);
        ZonedDateTime zdt = ZonedDateTime.parse(responseTimeStamp, dtf);
        System.out.println("Parsed with Locale.ENGLISH: " + zdt);

        // Setting the JVM's default locale to Locale.FRANCE
        Locale.setDefault(Locale.FRANCE);
        System.out.println("JVM's Locale: " + Locale.getDefault());
        // DateTimeFormatter with the default Locale
        dtfWithDefaultLocale = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss z");
        System.out.println("DateTimeFormatter's Locale: " + dtfWithDefaultLocale.getLocale());
        System.out.println(
                "Parsed with JVM's default locale: " + ZonedDateTime.parse(responseTimeStamp, dtfWithDefaultLocale));
    }
}

输出:

JVM's Locale: en_GB
DateTimeFormatter's Locale: en_GB
Parsed with JVM's default locale: 2020-12-14T20:34:37Z[GMT]
Parsed with Locale.ENGLISH: 2020-12-14T20:34:37Z[GMT]
JVM's Locale: fr_FR
DateTimeFormatter's Locale: fr_FR
Exception in thread "main" java.time.format.DateTimeParseException: Text 'Mon, 14 Dec 2020 20:34:37 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 java.base/java.time.ZonedDateTime.parse(ZonedDateTime.java:598)
    at Main.main(Main.java:29)