SimpleDateFormat toPattern在Java 9中的行为有所不同 [英] SimpleDateFormat toPattern behaves differently in java 9

查看:66
本文介绍了SimpleDateFormat toPattern在Java 9中的行为有所不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, new Locale("SV", "SE"));
((SimpleDateFormat) dateFormat).toPattern();

在Java 8中,此行生成 yyyy-MM-dd 在Java 9中会生成 y-MM-dd

In Java 8 this line produces "yyyy-MM-dd" while in Java 9 it produces "y-MM-dd".

推荐答案

    System.setProperty("java.locale.providers", "COMPAT,CLDR");
    DateFormat dateFormat
            = DateFormat.getDateInstance(DateFormat.SHORT, new Locale("sv", "SE"));
    System.out.println(((SimpleDateFormat) dateFormat).toPattern());

在我的jdk-9.0.4上运行此打印

Running on my jdk-9.0.4 this prints


yyyy-MM-dd

yyyy-MM-dd

您可能希望将属性设置为<$ c而是在命令行上使用$ c> -D ,它应该没有任何区别。

You may want to set the property with -D on the command line instead, it shouldn’t make any difference.

在Java 9中,通用语言环境数据存储库( Unicode联盟提供的CLDR)被用作默认的语言环境数据源,而在早期的Java版本中则不是这种情况。如上所述设置系统属性将启用Java 8行为。正如nullpointer在评论中所说,您可以阅读更多此处:默认情况下使用CLDR语言环境数据

In Java 9 the Common Locale Data Repository (CLDR) from the Unicode Consortium is used as the default source of locale data, which wasn’t the case in earlier Java versions. Setting the system property as above enables the Java 8 behaviour. As nullpointer said in a comment, you can read a bit more here: Use CLDR Locale Data by Default.

Basil Bourque的注释正确,习惯于使用小写的语言缩写,因此您应该指定 sv 以确保不会混淆读者。

Basil Bourque is correct in his comment, it is customary to use lowercase abbrevation for language, so you should probably specify sv to make sure you don’t confuse your reader.

可能还会想知道,模式中是否有一个 y 或四个是否有任何区别。我阅读 SimpleDateFormat 文档的方式,一个 y 将根据80-20规则解释两位数的年份。 :它在过去80年内或在接下来的20年内。 yyyy 将2位数字的年份解释为公元一世纪的一年。假设您的年份在4位数范围(1000到9999)之间,那么我认为这不会成为问题。

One might also wonder whether it makes any difference whether there’s one y or four in the pattern. The way I read the SimpleDateFormat documentation, one y will interpret a 2-digit year according to the 80-20 rule: it’s within the last 80 years or within the next 20. yyyy will interpret a 2-digit year as a year in the first century AD. Assuming your years are in the 4-digit range (1000 through 9999), I wouldn’t expect it to be a problem, though.

如果这仅适用于瑞典语语言环境,现代版本的代码将产生相同的结果:

If this was only for Swedish locale, the modern version of the code would give the same result:

DateTimeFormatterBuilder.getLocalizedDateTimePattern(
        FormatStyle.SHORT, null, IsoChronology.INSTANCE, new Locale("SV", "SE")));

但是,对于许多语言环境( en-CY ,例如塞浦路斯英语)的结果不同于 DateFormat DateTimeFormatter ,因此,如果您的目标是最大向后兼容,请使用过时的 DateFormat 类。

However, for a number of locales (en-CY, Cyprus English, for example) the result differs from DateFormat to DateTimeFormatter, so if your goal was maximal backward compatibility, stick with the outmoded DateFormat class.

这篇关于SimpleDateFormat toPattern在Java 9中的行为有所不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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