Specify your own abbreviations for the days of the week
According to CLDR German day of week abbreviations are written with a dot. To have Java parse a string where the abbreviations lacks the dot there are two obvious solutions:
- Don’t use CLDR. Java’s own abbreviations from Java 8 and before did not have the dots and are still available in newer Java versions.
- Specify your own abbreviations.
Since you had similar problems with French, where Java’s own abbreviations have dots too, I suggest that solution 1. would be insufficient for you. So let’s delve into solution 2. My code below takes CLDR’s abbreviations, e.g., So.
, and removes the trailing dots from them, so you get for example So
as in your string.
Locale loc = Locale.GERMANY;
Map<Long,String> dowsWithoutDots = Arrays.stream(DayOfWeek.values())
.collect(Collectors.toMap(dow -> Long.valueOf(dow.getValue()),
dow -> dow.getDisplayName(TextStyle.SHORT, loc).replaceFirst("\\.$", "")));
Map<Long,String> monthsWithoutDots = Arrays.stream(Month.values())
.collect(Collectors.toMap(m -> Long.valueOf(m.getValue()),
m -> m.getDisplayName(TextStyle.SHORT, loc).substring(0, 3)));
DateTimeFormatter germanWithoutDots = new DateTimeFormatterBuilder()
.appendText(ChronoField.DAY_OF_WEEK, dowsWithoutDots)
.appendPattern(", dd ")
.appendText(ChronoField.MONTH_OF_YEAR, monthsWithoutDots)
.appendPattern(" yyyy HH:mm:ss Z")
.toFormatter(loc);
System.out.println(germanWithoutDots.parse("So, 18 Jul 2021 15:24:00 +0200", Instant::from));
Output from the snippet is:
2021-07-18T13:24:00Z
For the month abbreviations removing the final dot did not work since, as you have observed, CLDR’s abbreviation is Juli
where you have got Jul
. So instead of removing the dot I abbreviate to three characters. You should test that it works for all months (including Mai).
I have not tried the same for French and Dutch, but it should work.
In case you want to try your luck with solution 1., circumventing CLDR completely, see JDK dateformatter parsing DayOfWeek in German locale, java8 vs java9.