根据语言环境输出日期

Outputting the day period based on locale

我想格式化一个日期,它应该被本地化并包括 "day period"。

"Day period" 的意思不是 am/pm,而是 "afternoon"、"early morning" 等

启用了 12 小时设置的基于德语语言环境的格式化日期应类似于 03.08.18, 9:01 abends,"abends" 表示晚上。

同样的例子在西班牙语中应该是3/8/18 9:01 de la noche


为什么我认为它应该是可能的:

答:因为 WhatsApp Android 应用程序的日期格式是这样的

B: 因为Java 10 源代码包含以下内容(来自德语翻译文件):

<dayPeriods>
<dayPeriodContext type="format">
    <dayPeriodWidth type="wide">
        <dayPeriod type="afternoon">nachmittags</dayPeriod>
        <dayPeriod type="am">vorm.</dayPeriod>
        <dayPeriod type="earlyMorning">morgens</dayPeriod>
        <dayPeriod type="evening">abends</dayPeriod>
        <dayPeriod type="morning">vormittags</dayPeriod>
        <dayPeriod type="night">nachts</dayPeriod>
        <dayPeriod type="noon">Mittag</dayPeriod>
        <dayPeriod type="pm">nachm.</dayPeriod>
    </dayPeriodWidth>
</dayPeriodContext>
...
</dayPeriods>


我试过了:(当前是语言环境)

DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, current);

String formattedDate = df.format(System.currentTimeMillis());

输出:08.12.18 7:24 nachm8/12/18 7:54 p. m.


以下代码具有相同的输出:

DateFormat abc = SimpleDateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, current);

abc.format(System.currentTimeMillis());


我还尝试了 DateTimeFormatterBuilder(所有 AMPM_OF_DAY 版本):

DateTimeFormatter test = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM, FormatStyle.SHORT);

DateTimeFormatter fmt = new DateTimeFormatterBuilder()
                .append(test)
                .appendText(ChronoField.AMPM_OF_DAY, TextStyle.SHORT)
                .appendText(ChronoField.AMPM_OF_DAY, TextStyle.NARROW)
                .appendText(ChronoField.AMPM_OF_DAY, TextStyle.FULL)
                .appendText(ChronoField.AMPM_OF_DAY, TextStyle.FULL_STANDALONE)
                .toFormatter(current);

ZonedDateTime date = ZonedDateTime.now();
date.format(fmt);

输出:8 dic. 2018 19:54p. m.p. m.p. m.11


有什么方法可以这样输出日期吗?

谢谢

只有两个库支持白天,即 ICU4J(部分合并到 Android)和我的库 Time4A。 java.text.DateFormatjava.time.format.DateTimeFormatter 等其他格式化程序不受支持。

德语语言环境 Time4A 中的示例(这里有一个通往老式类型 java.util.Date 的桥梁):

 ChronoFormatter<java.util.Date> f =
      ChronoFormatter.ofPattern(
          "dd.MM.uuuu, h:mm BBBB",
          PatternType.CLDR,
          Locale.GERMAN,
          Moment.axis(TemporalType.JAVA_UTIL_DATE))
      .withStdTimezone();
 System.out.println(f.format(new java.util.Date()));

示例输出 => "11.12.2018, 6:03 morgens"

它使用由 CLDR 数据(由 Unicode 联盟管理)的 LDML 语法定义的 special pattern symbol "B"。日分区规则也取自 CLDR 数据并寻找德语 like:

# day-period-rules
T0000=night1
T0500=morning1
T1000=morning2
T1200=afternoon1
T1300=afternoon2
T1800=evening1

# day-period-translations
P(a)_morning1=morgens
P(a)_morning2=vormittags
P(a)_afternoon1=mittags
P(a)_afternoon2=nachmittags
P(a)_evening1=abends
P(a)_night1=nachts

如果您不同意这些数据,那么您可以通过 builder pattern.

构建您自己的自定义格式化程序

关于 ICU4J 的注意事项:我没有测试过该库,但也许它的 class com.ibm.icu.text.SimpleDateFormat 也支持模式符号 B(但 javadoc 已过时)。

另注:

如果您喜欢基于样式的格式而不指定具体模式,那么您可以使用日期和时间部分的组合来走这条路:

    Locale loc = Locale.US;

    // our concrete values to be formatted
    Moment moment = SystemClock.currentMoment();
    PlainDate calendarDate = moment.toLocalTimestamp().getCalendarDate();

    // get the localized clock pattern
    Map<String, String> textForms = CalendarText.getIsoInstance(loc).getTextForms();
    String pattern = 
            textForms.containsKey("F_Bhm") ? textForms.get("F_Bhm") : "h:mm B";

    // construct the style-based date formatter
    ChronoFormatter<PlainDate> f1 = 
            ChronoFormatter.ofDateStyle(DisplayMode.SHORT, loc);

    // construct the time formatter
    ChronoFormatter<Moment> f2 = ChronoFormatter.ofMomentPattern(
        pattern,
        PatternType.CLDR,
        loc,
        Timezone.ofSystem().getID());

    System.out.println(f1.format(calendarDate) + " " + f2.format(moment));
    // example output => "12/11/18 6:18 in the morning"