In Java the Locale
defines things that are related how people want to see things (like currency formats, the name of the months and when a week starts).
When parsing the name of a Month (with a DateTimeFormatter
) it starts to become tricky.
If you use Locale.US
or Locale.ENGLISH
then September has the short form Sep
.
If you use Locale.UK
then September also has the short form Sep
in Java 11 ... but when you try Java 17 then it has Sept
(because of changes at the Unicode CLDR end for which I asked if this was correct).
The effect is that my tests started failing when trying to build with Java 17.
The reason my current code uses Locale.UK
instead of Locale.ENGLISH
is because in Java Locale.ENGLISH
is actually not just English but also the non-ISO American way of defining a week (they use Sunday as the first day of the week). I want to have it the ISO way.
Simply:
WeekFields.ISO
=WeekFields.of(Locale.UK)
=WeekFields[MONDAY,4]
WeekFields.of(Locale.ENGLISH)
=WeekFields.of(Locale.US)
=WeekFields[SUNDAY,1]
So starting with Java 17 I have not yet been able to find a built in Locale that works correctly.
In my mind I have to take either the Locale.ENGLISH
and change the WeekFields
or take the Locale.UK
and change the shortname of the month September to what I need.
My question is how do I do this (in Java 17)?
Or is there a better way to fix this?
Update 1:
- I already got feedback from the people at Unicode indicating that the change for en_GB to use Sept instead of Sep is a bugfix because that is the way it should be abbreviated in the UK.
So it seems I will need not just a parser that accepts "Sep" but one that will accept a mix of "Sept" and "Sep" for English.
Update 2:
- I have tweaked my code that in case of a parse exception it will try to change what is assumed to be the input ("Sep") into what the currently selected locate likes to have. This does not cover all cases, it covers enough cases for my specific situation. For those interested: my commit.
-Djava.locale.providers=COMPAT
, which will keep your data consistent with Java 9, regardless of future changes. By the way, this explains why the short form "Sep" has changed, though not how to override it. – SelfregulatingWeekFields
afterwards, if you use it at all. So why do you make your life unnecessarily hard by usingLocale.UK
when you actually meanLocale.ENGLISH
orLocale.ROOT
? – Sesquipedalian