This is my code:
try {
Locale loc = new Locale("", countryCode);
DateFormat formatter = DateFormat.getDateInstance(DateFormat.SHORT, loc);
String pattern = ((SimpleDateFormat) formatter).toPattern();
format = pattern;
if (!format.contains("yyyy"))
format = format.replace("y", "yyyy");
} catch (Exception e) {
Log.e("date", "Error trying to get date from Locale:" + e.getMessage());
}
Now I do set the locale based on the country code. It does get me back for "us" the displayCountryName as "United Stated" So I know the locale is correct. But the locale returns "dd/MM/y" Why?
Edit
I need to have my minimum API set to 22. so I cannot use Java8 function:
DateTimeFormatter.ofLocalizedDate(dateStyle);
Because it asks for minimum 26
Also, I need the localised date format based on the country code I sent. I have a list of countries, the user selects one and I need to show it's time and date format
To put more context, I load a list with this:
private void createCountriesHashmap() {
for (String iso : Locale.getISOCountries()) {
Locale l = new Locale("", iso);
map.put(l.getDisplayCountry(), iso);
if (l.getDisplayCountry().trim().length() > 0 && !countries.contains(l.getDisplayCountry())) {
countries.add(l.getDisplayCountry());
}
}
Collections.sort(countries);
}
When I select a object from there, I get its country code. I use the country code to create a locale: Locale loc = new Locale("", countryCode);
I need the date format of that country. Not my country