LanguageIdentifierConverter { public static Locale toLocale(final String identifier) { if ((identifier == null) || identifier.isEmpty()) { return null; } final String[] parts = identifier.split("-"); final String language = parts[0]; if (language.length() != 2) { throw new IllegalArgumentException( "Language must be a 2-letter code!"); } if (parts.length == 1) { return new Locale(language.toLowerCase()); } final String country = parts[1]; if (country.length() != 2) { throw new IllegalArgumentException( "Country must be a 2-letter code!"); } return new Locale(language.toLowerCase(), country.toUpperCase()); } private  LanguageIdentifierConverter(); static String toLanguageIdentifier(final Locale locale); static Locale toLocale(final String identifier);  }
@Test public void testToLocale() { final Locale locale1 = LanguageIdentifierConverter.toLocale("de"); Assert.assertEquals(Locale.GERMAN, locale1); final Locale locale2 = LanguageIdentifierConverter.toLocale("en-US"); Assert.assertEquals(Locale.US, locale2); final Locale locale3 = LanguageIdentifierConverter.toLocale(null); Assert.assertNull(locale3); }