code: private static Locale parseLocale(final String str) {
          if (isISO639LanguageCode(str)) {
              return new Locale(str);
          }
  
          final String[] segments = str.split("_", -1);
          final String language = segments[0];
          if (segments.length == 2) {
              final String country = segments[1];
              if (isISO639LanguageCode(language) && isISO3166CountryCode(country) ||
                      isNumericAreaCode(country)) {
                  return new Locale(language, country);
              }
          } else if (segments.length == 3) {
              final String country = segments[1];
              final String variant = segments[2];
              if (isISO639LanguageCode(language) &&
                      (country.length() == 0 || isISO3166CountryCode(country) || isNumericAreaCode(country)) &&
                      variant.length() > 0) {
                  return new Locale(language, country, variant);
              }
          }
          throw new IllegalArgumentException("Invalid locale format: " + str);
      }
docs: Tries to parse a locale from the given String.
  
  @param str the String to parse a locale from.
  @return a Locale instance parsed from the given String.
  @throws IllegalArgumentException if the given String can not be parsed.