It is often used in projects to parse strings into locale, but there is no more useful class.
Java itself provides 3 constructors, but the actual use of the process, you need to parse, more cumbersome.
Locale (Langugae)
Locale (Language,country)
Locale (String language, String country, string variant)
Apache has a localeutils.tolocale (String language) but does not support the last 2 characters as lowercase, such as: ZH_CN support, ZH_CN is not supported.
Refer to other methods, write the following classes, make a note, and hope to help people who solve similar problems.
Copy Code code as follows:
Import Java.util.Locale;
public class Localeutil
{
Private final static Locale Default_locale = Locale.english;
Public final static String Ietf_separator = "-";
Public final static String SEPARATOR = "_";
Public final static String empty_string = "";
public static Locale Tolocale (String language)
{
if (! Stringutil.isnullorempty (language))
{
Return Langtolocale (language, SEPARATOR);
}
return default_locale;
}
public static Locale Langtolocale (String lang, string separator)
{
if (stringutil.isnullorempty (lang))
{
return default_locale;
}
String language = empty_string;
String country = empty_string;
String variant = empty_string;
int i1 = Lang.indexof (separator);
if (I1 < 0)
{
language = lang;
} else
{
Language = lang.substring (0, I1);
++I1;
int i2 = Lang.indexof (separator, i1);
if (I2 < 0)
{
Country = lang.substring (I1);
} else
{
Country = lang.substring (I1, I2);
Variant = lang.substring (i2+1);
}
}
if (language.length () = = 2)
{
Language = Language.tolowercase ();
}else
{
Language = empty_string;
}
if (country.length () = = 2)
{
Country = Country.touppercase ();
}else
{
Country = empty_string;
}
if ((Variant.length () > 0) &&
((language.length () = 2) | | (Country.length () = 2)) )
{
Variant = Variant.touppercase ();
}else
{
variant = empty_string;
}
return new Locale (language, country, variant);
}
}
Note:
Copy Code code as follows:
public class Stringutil
{
public static Boo Lean IsNullOrEmpty (String target) {
return target = = NULL | | "". Equals (target);
}
}