Created
April 13, 2011 11:36
-
-
Save trygvis/917385 to your computer and use it in GitHub Desktop.
String to java.util.Locale parser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LocaleUtil { | |
private static Pattern pattern = Pattern.compile("^([a-z][a-z])(_([A-Z][A-Z]))?$"); | |
/** | |
* Returns null if the string couldn't be parsed | |
*/ | |
public static Locale parseStringToLocale(String s) { | |
Matcher matcher = pattern.matcher(s); | |
if(!matcher.matches()) { | |
return null; | |
} | |
String language = matcher.group(1); | |
String country = matcher.group(3); | |
if(country == null) { | |
return new Locale(language); | |
} | |
return new Locale(language, country); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
instead of returning null:
return new Locale("", "");