Snow
Snow

Reputation: 447

Calendar failed to get the days names in different languages than English

I'm trying to get the days of the week in German using the Calendar function getDisplayNames() with German locale.

Calendar now = Calendar.getInstance();
Map<String,Integer> displayNames = now.getDisplayNames(Calendar.DAY_OF_WEEK, Calendar.LONG, new Locale("de_DE"));

Instead I get the day of week names in English: Sunday, Monday, .... etc.
Am I doing something wrong or it simply doesn't work? Maybe it got somthing to do with the toString() of my IDEA debugger console? I'm using the latest Intellij 12.1.2.

Upvotes: 4

Views: 3732

Answers (4)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79025

java.time

In March 2014, Java 8 introduced the modern, java.time date-time API which supplanted the error-prone legacy java.util date-time API. Any new code should use the java.time API.

Solution using modern date-time API

Use DayOfWeek#getDisplayName(TextStyle, Locale) to get the textual representation.

You can iterate on DayOfWeek#values and create List, Map etc. as per your requirement.

Note: In the below demo, I have used Stream#collect(Collectors.toList()) for backward compatibility. If you are using Java 16+, I suggest you replace it with Stream#toList(). Check this page to learn more about it.

Demo:

class Main {
    public static void main(String[] args) {
        // Display names as a List
        List<String> list = Arrays.stream(DayOfWeek.values())
                .map(dw -> dw.getDisplayName(TextStyle.FULL, Locale.GERMAN))
                .collect(Collectors.toList());

        // Display names as a Map
        Map<String, Integer> map = Arrays.stream(DayOfWeek.values())
                .collect(Collectors.toMap(
                        dw -> dw.getDisplayName(TextStyle.FULL, Locale.GERMAN),
                        dw -> dw.getValue(),
                        (dw1, dw2) -> dw1,
                        LinkedHashMap::new));

        System.out.println(list);
        System.out.println(map);
    }
}

Output:

[Montag, Dienstag, Mittwoch, Donnerstag, Freitag, Samstag, Sonntag]
{Montag=1, Dienstag=2, Mittwoch=3, Donnerstag=4, Freitag=5, Samstag=6, Sonntag=7}

Try it on Ideone

Learn more about the modern Date-Time API from Trail: Date Time

Upvotes: 2

user2264310
user2264310

Reputation:

Instead of using an instance of Calendar class you use the java.text.DateFormatSymbols to get information such as the month names, weekday names for a specific locale. Here is an example to get the weekday names in Germany.

String[] weekdays = new DateFormatSymbols(Locale.GERMANY).getWeekdays();
for (int i = 0; i < weekdays.length; i++) {
    System.out.println("weekday = " + weekdays[i]);
}

Upvotes: 2

AllTooSir
AllTooSir

Reputation: 49372

Use , Locale.GERMAN or use new Locale("de"), instead of new Locale("de_DE")

Map<String,Integer> displayNames = now.getDisplayNames(Calendar.DAY_OF_WEEK, 
            Calendar.LONG, Locale.GERMAN);

Map<String,Integer> displayNames = now.getDisplayNames(Calendar.DAY_OF_WEEK, 
            Calendar.LONG, new Locale("de"));

It is helpful to go through the Javadocs for Locale(String) constructor, which says "Construct a locale from a language code". The language code for German is "de" not "de_DE".

Upvotes: 4

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 135992

Locale is wrong, try

    Map<String, Integer> displayNames = now.getDisplayNames(Calendar.DAY_OF_WEEK,
            Calendar.LONG, new Locale("de"));

result

{Donnerstag=5, Mittwoch=4, Freitag=6, Dienstag=3, Samstag=7, Sonntag=1, Montag=2}

Locale with one arg means Locale(String language)

Upvotes: 2

Related Questions