zoneddatetime.parse()在windows中对完全相同的字符串失败,但在mac和linux中工作

os8fio9y  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(276)

下面的unittest在windows机器(java11.0.9)中通过intellij ide执行时失败,但在具有相同java版本的mac或linux机器中执行时通过。

@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”

wmomyfyw

wmomyfyw1#

不要在没有区域设置的情况下使用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 使用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)

相关问题