首页 文章

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

提问于
浏览
2

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

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

任何建议,将不胜感激!

1 回答

  • 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"
    

相关问题