手机号归属地 libphonenumber

libphonenumber是一个Google开发的Common Java类库用于解析,格式化,存储和校验国际电话号码。适合用于智能手机上,同时还提供一个JavaScript移植。 

举一个例子

package com.lei.util;

import java.util.Locale;

import com.google.i18n.phonenumbers.PhoneNumberToCarrierMapper;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber;
import com.google.i18n.phonenumbers.geocoding.PhoneNumberOfflineGeocoder;
import com.okcoin.util.StringUtil;
/** 代码示例 StringUtil 为String工具类,并未提供,可执行更换。 可以使用String 原生函数替换 */
public class PhoneUtil {


    private static PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();

    private static PhoneNumberToCarrierMapper carrierMapper = PhoneNumberToCarrierMapper.getInstance();

    private static PhoneNumberOfflineGeocoder geocoder = PhoneNumberOfflineGeocoder.getInstance();

    /** * 根据国家代码和手机号 判断手机号是否有效 * @param phoneNumber * @param countryCode * @return */
    public static boolean checkPhoneNumber(String phoneNumber, String countryCode){

        int ccode = StringUtil.toInteger(countryCode);
        long phone = StringUtil.toLong(phoneNumber, 0L);

        PhoneNumber pn = new PhoneNumber();
        pn.setCountryCode(ccode);
        pn.setNationalNumber(phone);

        return phoneNumberUtil.isValidNumber(pn);

    }

    /** * 根据国家代码和手机号 判断手机运营商 * @param phoneNumber * @param countryCode * @return */
    public static String getCarrier(String phoneNumber, String countryCode){

        int ccode = StringUtil.toInteger(countryCode);
        long phone = StringUtil.toLong(phoneNumber, 0L);

        PhoneNumber pn = new PhoneNumber();
        pn.setCountryCode(ccode);
        pn.setNationalNumber(phone);
        //返回结果只有英文,自己转成成中文
        String carrierEn = carrierMapper.getNameForNumber(pn, Locale.ENGLISH);
        String carrierZh = "";
        carrierZh += geocoder.getDescriptionForNumber(pn, Locale.CHINESE);
        switch (carrierEn) {
        case "China Mobile":
            carrierZh += "移动";
            break;
        case "China Unicom":
            carrierZh += "联通";
            break;
        case "China Telecom":
            carrierZh += "电信";
            break;
        default:
            break;
        }
        return carrierZh;
    }


    /** * * @Description: 根据国家代码和手机号 手机归属地 * @date 2015-7-13 上午11:33:18 * @param @param phoneNumber * @param @param countryCode * @param @return 参数 * @throws */
    public static String getGeo(String phoneNumber, String countryCode){

        int ccode = StringUtil.toInteger(countryCode);
        long phone = StringUtil.toLong(phoneNumber, 0L);

        PhoneNumber pn = new PhoneNumber();
        pn.setCountryCode(ccode);
        pn.setNationalNumber(phone);

        return geocoder.getDescriptionForNumber(pn, Locale.CHINESE);

    }

    public static void main(String[] args) {
            System.out.println(PhoneUtil.getCarrier("159655555","86"));

    }

}

相关资料
GitHub 地址:https://github.com/googlei18n/libphonenumber

MAVEN 地址
    <dependency>
        <groupId>com.googlecode.libphonenumber</groupId>
        <artifactId>libphonenumber</artifactId>
        <version>7.0</version>
    </dependency>
    <dependency>
      <groupId>com.googlecode.libphonenumber</groupId>
      <artifactId>carrier</artifactId>
      <version>1.11</version>
    </dependency>
    <dependency>
        <groupId>com.googlecode.libphonenumber</groupId>
        <artifactId>geocoder</artifactId>
        <version>2.12</version>
    </dependency>

你可能感兴趣的:(手机归属地)