Calendar could not get day names in languages โ€‹โ€‹other than English

I am trying to get the days of the week in German using the Calendar getDisplayNames () function 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 names of the days of the week in English: Sunday, Monday, etc.
Am I doing something wrong or just not working? Maybe this is due to toString () of my IDEA debugger console? I am using the latest version of Intellij 12.1.2.

+6
source share
3 answers

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 useful to go through the Javadocs constructor for Locale (String) , which says: "Build a locale from a locale code." The language code for the German language is "de" not "de_DE".

+3
source

The 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} 

A single argument Locale(String language) means Locale(String language)

+2
source

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

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

Source: https://habr.com/ru/post/944019/


All Articles