Skip to content

Instantly share code, notes, and snippets.

@ktoraskartwilio
Created September 25, 2014 18:20
Show Gist options
  • Save ktoraskartwilio/ae137abcdf17cb5e930b to your computer and use it in GitHub Desktop.
Save ktoraskartwilio/ae137abcdf17cb5e930b to your computer and use it in GitHub Desktop.
import com.google.i18n.phonenumbers.CountryCodeToRegionCodeMap;
import com.google.i18n.phonenumbers.NumberParseException;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.Phonenumber;
import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber;
import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber.CountryCodeSource;
public static String lookupCountryLibPhoneNumber(String phonenumber) {
String countryKey = null;
String countryName = null;
System.out.println("PhoneNumber: "+phonenumber);
PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
try {
PhoneNumber phoneNumber = phoneNumberUtil.parse(phonenumber, null);
int countryKeyInt = phoneNumber.getCountryCode();
// lookup the country now
countryKey = phoneNumberUtil.getRegionCodeForCountryCode(countryKeyInt);
countryName = getCountryName(countryKey);
} catch (NumberParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return countryName;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment