首頁 > 軟體

Java手機號碼工具類範例詳解(判斷運營商、獲取歸屬地)

2022-02-28 13:05:36

所需參照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>

工具類原始碼

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);
    }
     * 根據國家程式碼和手機號  判斷手機運營商
    public static String getCarrier(String phoneNumber, String countryCode){
        //返回結果只有英文,自己轉成成中文
        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 += "聯通";
        case "China Telecom":
            carrierZh += "電信";
        default:
        }
        return carrierZh;
     * 
    * @Description: 根據國家程式碼和手機號  手機歸屬地
    * @param @param phoneNumber
    * @param @param countryCode
    * @param @return    引數
    * @throws
    public static String getGeo(String phoneNumber, String countryCode){
        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 areaName;
    public static void main(String[] args) {
        System.out.println(getPhoneRegionCode("18931234689","86"));
		
}

到此這篇關於Java手機號碼工具類(判斷運營商、獲取歸屬地)的文章就介紹到這了,更多相關Java手機號碼工具類內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com