本地化java.time中`MonthDay`或`YearMonth`类的字符串? [英] Localize string from `MonthDay` or `YearMonth` classes in java.time?

查看:957
本文介绍了本地化java.time中`MonthDay`或`YearMonth`类的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java 8及更高版本内建的java.time类提供 MonthDay YearMonth 类。他们的 toString parse 方法使用标准 ISO 8601 格式( - MM-DD & YYYY-MM ),这是明智的。

The java.time classes built into Java 8 and later offer the MonthDay and YearMonth classes. Their toString and parse methods use standard ISO 8601 formats (--MM-DD & YYYY-MM), which is wise.

为了呈现给人类,标准格式可能不合适。有没有生成一个自动本地化的字符串来表示 MonthDay YearMonth 的对象中的值?

For presentation to humans, the standard formats may not be suitable. Is there anyway to generate an automatically localized string to represent the values in objects of either MonthDay or YearMonth?

例如,在美国,用户通常可能希望MM / DD为月,MM / YY为一个月。而在英国,用户可能希望DD / MM为期一个月。

For example, in the United States users might typically want MM/DD for month-day and MM/YY for year-month. While in the UK users might want DD/MM for month-day.

无论如何通过 Locale 而不是明确定义格式模式?

Anyway to automate such variations by Locale rather than explicitly define formatting patterns?

我尝试了以下代码,使用面向日期的本地化格式化程序。

I tried the following code, using a date-oriented localizing formatter.

Locale l = Locale.US;
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate ( FormatStyle.SHORT ).withLocale ( l );

YearMonth ym = YearMonth.of ( 2017 , Month.JANUARY );
MonthDay md = MonthDay.of ( Month.JANUARY , 29 );

String outputYm = ym.format ( f );
String outputMd = md.format ( f );

该代码失败,当用于 YearMonth MonthDay

That code fails, throwing an exception when used for either YearMonth or MonthDay.

对于 YearMonth

Exception in thread "main" java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: DayOfMonth
    at java.time.YearMonth.getLong(YearMonth.java:494)
    at java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:298)
    at java.time.format.DateTimeFormatterBuilder$NumberPrinterParser.format(DateTimeFormatterBuilder.java:2540)
    at java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.format(DateTimeFormatterBuilder.java:2179)
    at java.time.format.DateTimeFormatterBuilder$LocalizedPrinterParser.format(DateTimeFormatterBuilder.java:4347)
    at java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.format(DateTimeFormatterBuilder.java:2179)
    at java.time.format.DateTimeFormatter.formatTo(DateTimeFormatter.java:1746)
    at java.time.format.DateTimeFormatter.format(DateTimeFormatter.java:1720)
    at java.time.YearMonth.format(YearMonth.java:1073)
    at javatimestuff.App.doIt(App.java:56)
    at javatimestuff.App.main(App.java:45)

对于 MonthDay

Exception in thread "main" java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: YearOfEra
    at java.time.MonthDay.getLong(MonthDay.java:451)
    at java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:298)
    at java.time.format.DateTimeFormatterBuilder$NumberPrinterParser.format(DateTimeFormatterBuilder.java:2540)
    at java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.format(DateTimeFormatterBuilder.java:2179)
    at java.time.format.DateTimeFormatterBuilder$LocalizedPrinterParser.format(DateTimeFormatterBuilder.java:4347)
    at java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.format(DateTimeFormatterBuilder.java:2179)
    at java.time.format.DateTimeFormatter.formatTo(DateTimeFormatter.java:1746)
    at java.time.format.DateTimeFormatter.format(DateTimeFormatter.java:1720)
    at java.time.MonthDay.format(MonthDay.java:646)
    at javatimestuff.App.doIt(App.java:57)
    at javatimestuff.App.main(App.java:45)


推荐答案

请参阅 JDK- 8168532 。需要JDK中的增强功能来实现这一点。

See JDK-8168532. There needs an enhancement in the JDK to make this easy.

可以在JDK之外进行工作,但这是很多工作。您必须解析CLDR XML文件(相互链接并有许多引用)。然后,您提取 MonthYear YearMonth 的相关本地化模式,然后可以使用这些模式创建一个 DateTimeFormatter

It is possible to do the work outside the JDK, but it is a lot of work. You have to parse the CLDR XML files (which are interlinked and have many references). Then you extract the relevant localized patterns for MonthYear and YearMonth, Then those patterns can be used to create a DateTimeFormatter.

或者,您可以硬编码地图 - Map< Locale,DateTimeFormatter> - 根据您的业务需求。

Alternatively, you can hard code a map - Map<Locale, DateTimeFormatter> - based on your business needs.

这篇关于本地化java.time中`MonthDay`或`YearMonth`类的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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