【问题标题】:How can I change the language of the months provided by LocalDate?如何更改 LocalDate 提供的月份的语言?
【发布时间】:2019-01-21 10:46:24
【问题描述】:

我需要找到当前月份并打印出来。我有以下代码:

this.currentDate=LocalDate.now();
this.month = this.currentDate.getMonth();

问题是月份是英文的,我需要用法文打印,以匹配网站的其余语言。如何选择LocalDate.now()方法提供的月份的语言,而无需每次需要显示时都进行人工翻译?

【问题讨论】:

    标签: java translation localdate


    【解决方案1】:

    您可以使用getDisplayName()Month 类型转换为String,这可以将语言环境更改为法语,如下所示:

    this.currentDate = LocalDate.now();
    this.month = this.currentDate.getMonth().getDisplayName(TextStyle.FULL, Locale.FRANCE);
    

    【讨论】:

      【解决方案2】:

      您可以使用DateTimeFormatter 为法语创建格式化程序,如下所示:

      final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE, dd MMMM, yyyy", Locale.FRENCH);
      final String month = LocalDate.now().format(formatter);
      

      【讨论】:

        猜你喜欢
        • 2018-08-10
        • 2021-07-13
        • 1970-01-01
        • 1970-01-01
        • 2019-05-21
        • 2020-12-28
        • 1970-01-01
        • 2013-11-11
        • 2019-03-14
        相关资源
        最近更新 更多