SimpleDateFormat .format()在Java8和Java11中给出不同的结果 [英] SimpleDateFormat .format() gives different results in Java8 vs. Java11

查看:117
本文介绍了SimpleDateFormat .format()在Java8和Java11中给出不同的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这小段代码:

import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {

    public static void main(String[] args) {

        SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz");

        System.out.println(format.format(new Date()));

    }
}

对于Java 8,输出为:

With Java 8, the output is:

Tue, 16 Jul 2019 13:16:54 AEST

使用Java 11:

Tue., 16 Jul. 2019 13:16:54 AEST

为什么会有区别,我应该如何修改代码以使其在两个Java版本中均能正常工作?

Why is there a difference and how should I modify my code to make it work evenly in both Java versions?

在发现此问题时,我实际上正在使用车把,我可以将其范围缩小到Java版本-但是知道如何使用Handlebars为相同格式的字符串产生相同的结果会很酷...)

I was actually using Handlebars while finding this issue, and I could narrow it down to the Java version - but it would be cool to know how to use Handlebars to produce the same result for the same format string...)

推荐答案

TL; DR

使用如下定义的系统属性java.locale.providers运行Java 9和更高版本(包括Java 11):

TL;DR

Run your Java 9 and later (including Java 11) with the system property java.locale.providers defined like this:

java -Djava.locale.providers=COMPAT,CLDR YourApp

现在输出的结果中没有点,其格式与Java 8相同,例如:

Now output is without the dots, in the same format as on Java 8, for example:

2019年7月16日,星期二14:24:15 AEST

Tue, 16 Jul 2019 14:24:15 AEST

CLDR

Java从多达四个来源获取其语言环境数据,包括一周中的几天和不同语言中数月的缩写.最多Java 8 Java自己的语言环境数据是默认设置.在Java 8中,还包括Unicode Common Locale Data Repository(CLDR;请参阅底部的链接)中的数据,而从Java 9中,它们是默认值.通过在上述系统属性中指定COMPAT,仍然可以包含和访问Java自己的数据.我们需要将其放在字符串的开头,因为依次尝试了源代码.

CLDR

Java gets its locale data, including abbreviations used for days of the week and for months in different languages, from up to four sources. Up to Java 8 Java’s own locale data were the default. From Java 8 locale data from Unicode Common Locale Data Repository (CLDR; see links at the bottom) are included too, and from Java 9 they are the default. Java’s own data are still included and accessible by specifying COMPAT in the above system property. We need to put it first in the string as the sources are tried in turn.

一个人可能已经期望另一种(甚至可能更好)的解决方案是在所有Java版本中使用CLDR.奇怪的是,在这种情况下,这并不能为我们提供所有Java版本相同的格式.这是将属性设置为CLDR,JRE时的输出(JRE是COMPAT的旧名称,在Java 8上,我们需要改用它).

One might have expected that another (and perhaps even a nicer) solution would be to use CLDR in all Java versions. Curiously this doesn’t give us the same format on all Java versions in this case. Here is the output when setting the property to CLDR,JRE (JRE is the old name for COMPAT, on Java 8 we need to use this instead).

在Java 8上:

2019年7月16日,星期二14:35:02 AEST

Tue, 16 Jul 2019 14:35:02 AEST

在Java 9和11上:

On Java 9 and 11:

星期二,2019年7月16日14:35:52 AEST

Tue., 16 Jul. 2019 14:35:52 AEST

CLDR具有版本,不同的Java版本不包含相同的版本.

CLDR comes in versions, and not the same version is included with the different Java versions.

这是我用于上述输出的摘录.

Here’s the snippet I have used for the above outputs.

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern(
            "EEE, dd MMM yyyy HH:mm:ss zzz", Locale.forLanguageTag("en-AU"));
    ZonedDateTime now = ZonedDateTime.now(ZoneId.of("Australia/Sydney"));
    System.out.println(now.format(formatter));

我正在使用并推荐Java.time,这是现代的Java日期和时间API.您使用的日期时间类SimpleDateFormatDate早已过时,并且设计总是很差,所以我建议避免使用它们.在Java 8及更高版本上,当然没有理由要使用它们,并且java.time也已反向移植到Java 6和7.

I am using and recommending java.time, the modern Java date and time API. The date-time classes that you used, SimpleDateFormat and Date, are long outdated and were always poorly designed, so I recommend avoiding them. On Java 8 and later there’s certainly no reason why we should use them, and java.time has been backported to Java 6 and 7 too.

  • CLDR - Unicode Common Locale Data Repository
  • Wikipedia article: Common Locale Data Repository
  • Use CLDR Locale Data by Default in Java Platform, Standard Edition Oracle JDK 9 Migration Guide
  • LocaleServiceProvider documentation spelling out the possible locale data sources: CLDR, COMPAT and more.
  • Oracle tutorial: Date Time explaining how to use java.time.

这篇关于SimpleDateFormat .format()在Java8和Java11中给出不同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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