Class LocaleUtils


  • public final class LocaleUtils
    extends Object

    Operations to assist when working with a Locale.

    This class tries to handle null input gracefully. An exception will not be thrown for a null input. Each method documents its behavior in more detail.

    Author:
    https://github.com/apache/commons-lang/blob/master/src/main/java/org/apache/commons/lang3/LocaleUtils.java
    • Method Detail

      • toLocale

        public static Locale toLocale​(String str)

        Converts a String to a Locale.

        This method takes the string format of a locale and creates the locale object from it.

           LocaleUtils.toLocale("")           = new Locale("", "")
           LocaleUtils.toLocale("en")         = new Locale("en", "")
           LocaleUtils.toLocale("en_GB")      = new Locale("en", "GB")
           LocaleUtils.toLocale("en_001")     = new Locale("en", "001")
           LocaleUtils.toLocale("en_GB_xxx")  = new Locale("en", "GB", "xxx")   (#)
         

        This method validates the input strictly. The language code must be lowercase. The country code must be uppercase. The separator must be an underscore. The length must be correct.

        Parameters:
        str - the locale String to convert, null returns null
        Returns:
        a Locale, null if null input
        Throws:
        IllegalArgumentException - if the string is an invalid format
        See Also:
        Locale.forLanguageTag(String)