Java 8中不同语言环境的命名月份名称

时间:2016-11-15 20:30:10

标签: java java-8 locale

我需要能够以月份格式支持名义月份名称,例如Listopad 2016年用波兰语。我仍然支持其他地方的日期格式作为完整的MMM/dd/YYYY日期,因此我不想失去该功能。我使用的是Java 8,我相信Java 8默认使用genitive格式。

我已尝试将jodaTime用于MonthYear,但Java 8更新似乎迫使它在任何地方显示所有格式。我需要支持其他语言支持数月的变形,如斯洛伐克语,捷克语等。

任何建议都将不胜感激!

1 个答案:

答案 0 :(得分:2)

您可以将DateTimeFormatterwithLocale一起使用。请参阅以下代码:

DateTimeFormatter df_en = DateTimeFormatter.ofPattern("MMMM/dd/yyyy").withLocale(Locale.ENGLISH)
DateTimeFormatter df_pl = DateTimeFormatter.ofPattern("MMMM/dd/yyyy").withLocale(new Locale("pl"))
DateTimeFormatter df_cs = DateTimeFormatter.ofPattern("MMMM/dd/yyyy").withLocale(new Locale("cs"))
DateTimeFormatter df_sk = DateTimeFormatter.ofPattern("MMMM/dd/yyyy").withLocale(new Locale("sk"))

LocalDate d = LocalDate.now()
=> java.time.LocalDate d = 2016-11-15
d.format(df_en)
=> "November/15/2016"
d.format(df_pl)
=> "listopada/15/2016"
d.format(df_cs)
=> "listopadu/15/2016"
d.format(df_sk)
=> "novembra/15/2016"