'Java Locale to String

I need to convert Locale to String. How can I do that?

I have Locale loc and String loc1 and need to convert the value from locale to String.



Solution 1:[1]

That really depends on the Java version you use. When you say convert to String, I assume you might want to:

  1. Convert it to Locale identifier, so that you can easily recreate Locale in the future.
  2. Pass it as a String representation to some part of the system.
  3. Get (possibly native) name of language, country or the Locale object.

Others pointed you to a documentation, but I think it is better to walk through several possibilities.

Ad 1. If you need to convert to a Locale identifier, the best method as of Java 7 (that doesn't really exist in previous releases) is toLanguageTag().

That's because you can easily store Locale identifier in a string (including all the options, like script, Unicode extensions, etc.) and recreate Locale object using forLanguageTag(String) without losing information. Simple toString() does not hold this property.
Following code snippet illustrates my point:

Locale locale = Locale.getDefault(Locale.Category.DISPLAY);
String localeId = locale.toLanguageTag();
Locale recreatedLocale = Locale.forLanguageTag(localeId);

Prior to Java 7, you really needed to use toString() and manually handle countries and variants (for Locale consisted of up to three identifiers separated by and underscore, i.e. no_NO_NY.)

Ad 2. If you need to pass Locale to other part of the system (i.e. web service), toString() may and may be not what you are looking for. That's because of what I wrote above - if you need dash as a separator and don't care so much for variants, I would go with getLanguage() and getCountry() methods rather than plain toString():

Locale uiLocale = Locale.getDefault(Locale.Category.DISPLAY);
String localeId = MessageFormat.format("{0}-{1}", 
                                       uiLocale.getLanguage(),
                                       uiLocale.getCountry());

This method should work well with Java versions since version 1.4.

Ad 3. If you want to present language, country or Locale display name, so that user can choose the valid Locale (i.e. in his/her profile), what you probably looking for are getDisplayCountry(Locale), getDisplayLanguage(Locale) and getDisplayName(Locale). You can use them to display names in a target user language (provided that JVM has appropriate translations, which is true for many languages):

Locale uiLocale = Locale.getDefault(Locale.Category.DISPLAY);
String languageName = uiLocale.getDisplayLanguage(uiLocale);
String countryName = uiLocale.getDisplayLanguage(uiLocale);
// usually something like langage (country), i.e. polski (Polska)
String localeName = uiLocale.getDisplayName(uiLocale);

Solution 2:[2]

It’s very simple. Just use the following code. Consider you are using locale to convert from an ISO current calendar date to the Hijri calendar.

Chronology iso = ISOChronology.getInstanceUTC();
Chronology hijri = IslamicChronology.getInstanceUTC();
LocalDate todayIso = new LocalDate(2016, 1, 20, iso);
LocalDate todayHijri = new LocalDate(todayIso.toDateTimeAtStartOfDay(), hijri);
String currenthijridate = "" + todayHijri;

/* 'currenthijridate' will appear as 1437-04-08 which
   is a string. Now it's simply String = "" + locale
 */

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Paweł Dyda
Solution 2 Peter Mortensen