I'm trying to format dates according to the device's locale. If you have "standard" locales like en_US
, de_DE
and so on, this is no problem. But if the user has set his phone language to e.g. englisch (en) and the region to e.g. Germany (DE), the locale will be the following en_DE
(not standard locale).
If I try now to format a date with this locale, it will always use US
formatting (I think because auf the language part en
):
String date = DateFormat.yMd('en_DE').format(DateTime.now());
print(date); // 7/13/2022
If I use only the region part DE
, this will work for DE
:
String date = DateFormat.yMd('DE').format(DateTime.now());
print(date); // 13.07.2022
But for the locale en_US
this will not work:
String date = DateFormat.yMd('US').format(DateTime.now()); // Invalid argument(s): Invalid locale "US"
How can I format the date according to the region part of the locale?