4

I might be overlooking something incredible obvious, but why does this:

final Calendar calendar = Calendar.getInstance(Locale.GERMAN);
System.out.println(calendar.getFirstDayOfWeek());

Result in

1  (Sunday)

instead of

2 (Monday)

?

And before someone claims "the first day of the week is Sunday for all German speaking people" (again), it's not: "[D] is the weekday number, from 1 through 7, beginning with Monday and ending with Sunday."

In fact, Locale.GERMANY results in the correct "Monday".

Why would the first day of the week be Sunday for a German locale?

Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
Stefan S.
  • 3,950
  • 5
  • 25
  • 77
  • 5
    I recommend you don’t use `Calendar`. That class is poorly designed and long outdated. Instead use `WeekFields` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Feb 14 '19 at 14:07
  • 1
    The result of your code varies with Java version (I can readily reproduce on Java 10 and 11, not on 8 and 9). You can further control it through setting the system property `java.locale.providers` on the command line, for example `-Djava.locale.providers=HOST,COMPAT,CLDR`. – Ole V.V. Feb 14 '19 at 22:09

2 Answers2

3

You kind of answered this yourself, in your declaration you used the locale for the german language and not the country, change it to

 final Calendar calendar = Calendar.getInstance(Locale.GERMANY);

(Note, since you didn't changed the country it used the default one)

Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
  • Well the question isn't "how do I fix this" but "why"? Because I fear that our German speaking customers might get wrong dates, too. – Stefan S. Feb 14 '19 at 13:54
  • Are you saying the default locale is `Locale.GERMANY`? – Joakim Danielson Feb 14 '19 at 13:56
  • Not exactly. Their locale might be "de_DE", or "de_CH", or "de_AT" or "de_LI" or it might just be "de"? I'm worried that if "de" returns the wrong value, even though there is no German speaking country with Sunday as the first day of the week, other German locales might return the wrong value, too. – Stefan S. Feb 14 '19 at 14:03
  • @SteffiS. I hear you. I looked around but to me it isn't clear what `Calendar` does when only given a language and not a country. It is probably best to be as exact as possible giving both language and country if that can be done. Or move away from the old `Calendar` class in favor for the more modern java.time framework – Joakim Danielson Feb 14 '19 at 14:09
  • 3
    I doubt that just `de` would be used as default locale anywhere. I understand your worry, though. – Ole V.V. Feb 14 '19 at 14:10
3

All default locales for a specific language e.g. Locale.GERMAN, Locale.FRENCH, Locale.ENGLISH default the not specified part of the locale to USA. Effectively these constant represent a German, French or English speaking person living in the USA.

There is probably no good answer to this question because knowing just the language is not enough to figure out the first day of a week. Maybe because Java was developed by a USA company for US market the default locale values are using USA as a country.

If you need a German locale use Locale.GERMANY as pointed out by the other answer.

Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111