【问题标题】: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);