How to get Locale from its String representation in Java?

Joonas Pulakka picture Joonas Pulakka · Mar 26, 2010 · Viewed 131.6k times · Source

Is there a neat way of getting a Locale instance from its "programmatic name" as returned by Locale's toString() method? An obvious and ugly solution would be parsing the String and then constructing a new Locale instance according to that, but maybe there's a better way / ready solution for that?

The need is that I want to store some locale specific settings in a SQL database, including Locales themselves, but it would be ugly to put serialized Locale objects there. I would rather store their String representations, which seem to be quite adequate in detail.

Answer

yurilo picture yurilo · Jan 25, 2011

Method that returns locale from string exists in commons-lang library: LocaleUtils.toLocale(localeAsString)