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

How can I change the language of the months provided by LocalDate?

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

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

问题是月份是英文的,我需要用法语打印它,以匹配网站其他语言。怎样才能select显示LocalDate.now()方法提供的当月语言,而不需要每次显示都手动翻译?

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

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

您可以使用 getDisplayName()Month 类型转换为 String,这可以将区域设置更改为法语,如下所示:

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