Search code examples
javaandroidphone-numberlibphonenumber

Convert international phone number into local one


I'm trying to use libphonenumber google library to convert a number from international to local.

The international number represent is: "+97239658320"

The local number (here in Israel is): "039658320"

http://code.google.com/p/libphonenumber/

    PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();

    PhoneNumber phone = null;

    try
    {
        phone = PhoneNumberUtil.getInstance().parse("+97239658320", null);
    }

    catch (NumberParseException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

however, in "phone" the results are:

Country Code: 972 National Number: 39658320 (without the leading zero)

how do I convert it correctly?


Solution

  • Looking at the quick example of the project page says that you after parsing should have:

    {
    country_code: 972
    national_number: 39658320
    }
    

    To get it in your desired format you should format it with format NATIONAL:

    phoneUtil.format(phone, PhoneNumberFormat.NATIONAL)
    

    To strip out any phone number separators take a look at stripSeparators() from android.telephony.PhoneNumberUtils