如何更改 LocalDate 提供的月份的语言?

问题描述 投票:0回答:3

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

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

问题是月份是英文的,我需要用法文打印,以匹配网站语言的其余部分。如何选择

LocalDate.now()
方法提供的月份语言,而不需要每次需要显示时都做手动翻译?

java translation localdate
3个回答
10
投票

您可以使用

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

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

6
投票

您可以使用

DateTimeFormatter
创建法语格式化程序,如下所示:

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

0
投票

就我而言,没有 ENUM 国家/地区选项,然后我使用默认值并解决了:

LocalDate.now().getMonth().getDisplayName(TextStyle.FULL, Locale.getDefault())
© www.soinside.com 2019 - 2024. All rights reserved.