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

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

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

  1. @Test
  2. public void rfc1123JaveTimeUtilParsing(){
  3. final String rfc1123Pattern = "EEE, dd MMM yyyy HH:mm:ss z";
  4. final String responseTimeStamp = "Mon, 14 Dec 2020 20:34:37 GMT";
  5. DateTimeFormatter javaTimeDateTimeFormatter = DateTimeFormatter.ofPattern(rfc1123Pattern);
  6. ZonedDateTime javaFinalTime = ZonedDateTime.parse(responseTimeStamp, javaTimeDateTimeFormatter);
  7. Assert.assertNotNull(javaFinalTime);
  8. }

对于windows,结果是以下异常:
java.time.format.datetimeparseexception:无法在索引0处分析文本“mon,14 dec 2020 20:34:37 gmt”

wmomyfyw

wmomyfyw1#

不要在没有区域设置的情况下使用datetimeformatter。

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

  1. import java.time.ZonedDateTime;
  2. import java.time.format.DateTimeFormatter;
  3. import java.util.Locale;
  4. public class Main {
  5. public static void main(String[] args) {
  6. final String responseTimeStamp = "Mon, 14 Dec 2020 20:34:37 GMT";
  7. DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);
  8. ZonedDateTime zdt = ZonedDateTime.parse(responseTimeStamp, dtf);
  9. System.out.println(zdt);
  10. }
  11. }

输出:

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

默认情况下, DateTimeFormatter#ofPattern 使用jvm根据主机环境在启动期间设置的默认格式区域设置。我试图通过以下演示来说明这个问题:

  1. import java.time.ZonedDateTime;
  2. import java.time.format.DateTimeFormatter;
  3. import java.util.Locale;
  4. public class Main {
  5. public static void main(String[] args) {
  6. final String responseTimeStamp = "Mon, 14 Dec 2020 20:34:37 GMT";
  7. DateTimeFormatter dtfWithDefaultLocale = null;
  8. System.out.println("JVM's Locale: " + Locale.getDefault());
  9. // DateTimeFormatter with the default Locale
  10. dtfWithDefaultLocale = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss z");
  11. System.out.println("DateTimeFormatter's Locale: " + dtfWithDefaultLocale.getLocale());
  12. System.out.println(
  13. "Parsed with JVM's default locale: " + ZonedDateTime.parse(responseTimeStamp, dtfWithDefaultLocale));
  14. // DateTimeFormatter with Locale.ENGLISH explicitly (recommended)
  15. DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);
  16. ZonedDateTime zdt = ZonedDateTime.parse(responseTimeStamp, dtf);
  17. System.out.println("Parsed with Locale.ENGLISH: " + zdt);
  18. // Setting the JVM's default locale to Locale.FRANCE
  19. Locale.setDefault(Locale.FRANCE);
  20. System.out.println("JVM's Locale: " + Locale.getDefault());
  21. // DateTimeFormatter with the default Locale
  22. dtfWithDefaultLocale = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss z");
  23. System.out.println("DateTimeFormatter's Locale: " + dtfWithDefaultLocale.getLocale());
  24. System.out.println(
  25. "Parsed with JVM's default locale: " + ZonedDateTime.parse(responseTimeStamp, dtfWithDefaultLocale));
  26. }
  27. }

输出:

  1. JVM's Locale: en_GB
  2. DateTimeFormatter's Locale: en_GB
  3. Parsed with JVM's default locale: 2020-12-14T20:34:37Z[GMT]
  4. Parsed with Locale.ENGLISH: 2020-12-14T20:34:37Z[GMT]
  5. JVM's Locale: fr_FR
  6. DateTimeFormatter's Locale: fr_FR
  7. Exception in thread "main" java.time.format.DateTimeParseException: Text 'Mon, 14 Dec 2020 20:34:37 GMT' could not be parsed at index 0
  8. at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2046)
  9. at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948)
  10. at java.base/java.time.ZonedDateTime.parse(ZonedDateTime.java:598)
  11. at Main.main(Main.java:29)
展开查看全部

相关问题