当前位置:   article > 正文

Java手机号码工具类(判断运营商、获取归属地)_java判断手机号归属地架包

java判断手机号归属地架包

所需引用Jar包

<dependency>
	<groupId>com.googlecode.libphonenumber</groupId>
	<artifactId>geocoder</artifactId>
	<version>2.15</version>
</dependency>
		
<dependency>
	<groupId>com.googlecode.libphonenumber</groupId>
	<artifactId>libphonenumber</artifactId>
	<version>6.3</version>
</dependency>
		
<dependency>
	<groupId>com.googlecode.libphonenumber</groupId>
	<artifactId>prefixmapper</artifactId>
	<version>2.15</version>
</dependency>
<dependency>
	<groupId>com.googlecode.libphonenumber</groupId>
	<artifactId>carrier</artifactId>
	<version>1.5</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

工具类源码

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;
 
 
/**
  * 
  * @ClassName: PhoneUtil
  * @Description:手机号码归属地工具类
 */
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 = StringUtils.obj2Int(countryCode);
        long phone = StringUtils.toLong(phoneNumber);
        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 = StringUtils.obj2Int(countryCode);
        long phone = StringUtils.toLong(phoneNumber);
        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: 根据国家代码和手机号  手机归属地
    * @param @param phoneNumber
    * @param @param countryCode
    * @param @return    参数
    * @throws
     */
    public static String getGeo(String phoneNumber, String countryCode){
        int ccode = StringUtils.obj2Int(countryCode);
        long phone = StringUtils.toLong(phoneNumber);
        PhoneNumber pn = new PhoneNumber();
        pn.setCountryCode(ccode);
        pn.setNationalNumber(phone);
        return geocoder.getDescriptionForNumber(pn, Locale.CHINESE);
    }
    
    
    /**
     * 
      * @Title: getPhoneRegionCode
      * @Description: 得到手机的归宿地编码
      * @return String    返回类型
      * @throws
     */
    public static String getPhoneRegionCode(String phoneNumber, String countryCode){
    	String areaName=getGeo(phoneNumber,countryCode);
    	if(StringUtils.isEmpty(areaName)){
    		return "";
    	}
    	if(areaName.length()<3){
    		return "";
    	}
    	return areaName;
    }
 
    
    public static void main(String[] args) {
        System.out.println(getPhoneRegionCode("18931234689","86"));
    }
		
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/683361
推荐阅读
相关标签
  

闽ICP备14008679号