Deep parsing of Locale_java in Java

Source: Internet
Author: User
Tags locale
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);
   }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.