1. 依赖
!-- 手机号校验工具包 -->
<dependency>
<groupId>com.googlecode.libphonenumber</groupId>
<artifactId>geocoder</artifactId>
<version>2.185</version>
</dependency>
<dependency>
<groupId>com.googlecode.libphonenumber</groupId>
<artifactId>libphonenumber</artifactId>
<version>8.12.49</version>
</dependency>
<dependency>
<groupId>com.googlecode.libphonenumber</groupId>
<artifactId>prefixmapper</artifactId>
<version>2.185</version>
</dependency>
<dependency>
<groupId>com.googlecode.libphonenumber</groupId>
<artifactId>carrier</artifactId>
<version>1.175</version>
</dependency>
2. 工具类
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ReUtil;
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 lombok.Data;
import lombok.experimental.Accessors;
import java.util.Locale;
public class PhoneUtil {
private static PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
private static PhoneNumberToCarrierMapper carrierMapper = PhoneNumberToCarrierMapper.getInstance();
private static PhoneNumberOfflineGeocoder geocoder = PhoneNumberOfflineGeocoder.getInstance();
public static final int CHINA_COUNTRY_CODE = 86;
public static final String PROVINCE_REG = "[\\u4E00-\\u9FFF]+?(?:省|自治区|特别行政区)|(?:天津|重庆|上海|北京)市";
public static final String CITY_REG = "(?<=省|自治区|特别行政区)[\u4E00-\u9FFF]+?(?:市|自治州|区|县|盟)";
/**
* 根据国家代码和手机号 判断手机号是否有效
* phoneNumber
* countryCode
* 手机号是否有效
*/
public static boolean checkPhoneNumber(String phoneNumber, String countryCode) {
int ccode = NumberUtil.parseInt(countryCode);
long phone = NumberUtil.parseLong(phoneNumber);
PhoneNumber pn = new PhoneNumber();
pn.setCountryCode(ccode);
pn.setNationalNumber(phone);
return phoneNumberUtil.isValidNumber(pn);
}
/**
* 获取运营商
*/
public static String getOperator(String phoneNumber, Integer countryCode) {
long phone = NumberUtil.parseLong(phoneNumber);
PhoneNumber pn = new PhoneNumber();
pn.setCountryCode(countryCode);
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;
}
/**
* 获取运营商,中国
*/
public static String getOperator(String phoneNumber) {
return getOperator(phoneNumber, CHINA_COUNTRY_CODE);
}
/**
* 获取归属地
*/
public static String getGeo(String phoneNumber, Integer countryCode) {
long phone = NumberUtil.parseLong(phoneNumber);
PhoneNumber pn = new PhoneNumber();
pn.setCountryCode(countryCode);
pn.setNationalNumber(phone);
return geocoder.getDescriptionForNumber(pn, Locale.CHINESE);
}
//获取归属地,中国
public static String getGeo(String phoneNumber) {
return getGeo(phoneNumber, CHINA_COUNTRY_CODE);
}
public static Area getArea(String phoneNumber) {
String address = getGeo(phoneNumber);
if (address != null) {
String province = ReUtil.getGroup0(PROVINCE_REG, address);
String city = ReUtil.getGroup0(CITY_REG, address);
return new Area().setProvince(province).setCity(city);
}
return new Area().setProvince("未知").setCity("未知");
}
@Data
@Accessors(chain = true)
public static class Area {
private String province;
private String city;
}
3. 使用
public static void main(String[] args) {
System.out.println(getOperator("手机号"));
}