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

3

9

I need to find the current month and print it. I have the following code:

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

The problem is that the month is in English and I need to print it in French, to match the rest of the website language. How can I select the language of the month provided by the LocalDate.now() method without needing to do a manual translation each time I need to display it?

Aspasia answered 14/8, 2018 at 14:25 Comment(0)
A
12

You can convert the Month type into a String using getDisplayName(), which can change the locale to French as follows:

this.currentDate = LocalDate.now();
this.month = this.currentDate.getMonth().getDisplayName(TextStyle.FULL, Locale.FRANCE);
Aspasia answered 14/8, 2018 at 14:42 Comment(0)
C
6

You can use the DateTimeFormatter to create a formatter for French as follows:

final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE, dd MMMM, yyyy", Locale.FRENCH);
final String month = LocalDate.now().format(formatter);
Cu answered 14/8, 2018 at 14:34 Comment(0)
P
0

In my case there was not the ENUM country option then I used default and its solved:

LocalDate.now().getMonth().getDisplayName(TextStyle.FULL, Locale.getDefault())
Packer answered 10/1, 2024 at 1:43 Comment(1)
Just read The Java Tutorials section Creating a Locale. Free of cost, by Oracle.Monadelphous

© 2022 - 2025 — McMap. All rights reserved.