el vis
el vis

Reputation: 1302

Java default Locale fallback

In Java I create Locale with Language and Country parameters and then use appropriate date formats.

However Java does not support all Language_Country combinations and fallbacks to default locale formats (in my case en_US).

Is there a way to determine that fallback occured?

E.g:

When I create locale with Locale locale = new Locale("xx","xx");

then

((SimpleDateFormat)DateFormat.getDateInstance(DateFormat.SHORT,locale)).toLocalizedPattern();

returns M/d/yy so it fallbacks to en_US.

So i need something like

locale.isDefault

Upvotes: 3

Views: 1518

Answers (2)

assylias
assylias

Reputation: 328608

You can check that the locale is one of the available locales:

Set<Locale> locales = new HashSet<>();
Collections.addAll(locales, Locale.getAvailableLocales());

Locale locale = new Locale("xx", "xx");
System.out.println(locales.contains(locale)); //prints false

locale = new Locale("fr", "FR");
System.out.println(locales.contains(locale)); //prints true

Upvotes: 1

Peter Crotty
Peter Crotty

Reputation: 303

Locale.getDefault().equals(locale)

Upvotes: 0

Related Questions