引⼊依赖包
<!-- 电话号码⼯具类 -->
<dependency>
<groupId&lecode.libphonenumber</groupId>
<artifactId>libphonenumber</artifactId>
<version>7.0</version>
</dependency>
<dependency>
<groupId&lecode.libphonenumber</groupId>
<artifactId>carrier</artifactId>
<version>1.11</version>
</dependency>
<dependency>
<groupId&lecode.libphonenumber</groupId>
<artifactId>geocoder</artifactId>
<version>2.12</version>
</dependency>
<!-- 电话号码⼯具类 END -->
⼯具类⽅法
package ;
import com.alibaba.fastjson.JSONObject;
le.i18n.phonenumbers.NumberParseException;
le.i18n.phonenumbers.PhoneNumberUtil;
le.i18n.phonenumbers.Phonenumber;
le.i18n.phonenumbers.Phonenumber.PhoneNumber;
le.ding.PhoneNumberOfflineGeocoder;
import java.util.Locale;
import Matcher;
import Pattern;
/**
* @author Mao, hongtu1993@sina
* @version Enter , 5:47 PM 2019/12/16
代理做帐*/
public class PhoneUtil {
private static PhoneNumberUtil phoneNumberUtil = Instance();
private static PhoneNumberOfflineGeocoder geocoder = Instance();
/**
* 根据国家代码和⼿机号判断⼿机号是否有效
* @param phoneNumber ⼿机号码
* @param countryCode 国号(区号)
* @return true / false
*/
public static boolean checkPhoneNumber(long phoneNumber, int countryCode) {
PhoneNumber pn = new PhoneNumber();
黄文豪pn.setCountryCode(countryCode);
pn.setNationalNumber(phoneNumber);
return phoneNumberUtil.isValidNumber(pn);
}
private static Pattern phoneReg = Patternpile("\\+(9[976]\\d|8[987530]\\d|6[987]\\d|5[90]\\d|42\\d|3[875]\\d|2[98654321]\\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|4[987654310]|3[9643210]|2[70]|7|1)\\d{1,14}$"); public static JSONObject getPhoneNumberInfo(String phoneNumber) throws Exception {
// 正则校验
Matcher matcher = phoneReg.matcher(phoneNumber);
if (!matcher.find()) {
throw new Exception("The phone number maybe like:" + "+[National Number][Phone number], but got " + phoneNumber);
}
Phonenumber.PhoneNumber referencePhonenumber;
try {
String language = "CN";
referencePhonenumber = phoneNumberUtil.parse(phoneNumber, language);
} catch (NumberParseException e) {
throw new Message());
}
String regionCodeForNumber = RegionCodeForNumber(referencePhonenumber);
if (regionCodeForNumber == null) {
throw new Exception("Missing region code by phone number " + phoneNumber);
}
boolean checkSuccess = PhoneUtil.NationalNumber(), CountryCode());
if (!checkSuccess) {
throw new Exception("Not an active number:" + phoneNumber);
}
String description = DescriptionForNumber(referencePhonenumber, Locale.CHINA);
int countryCode = CountryCode();
long nationalNumber = NationalNumber();鹿晗为什么退出exo
初二下册数学期末试卷JSONObject resultObject = new JSONObject();
// 区域编码 Locale : HK, US, CN ...
resultObject.put("regionCode", regionCodeForNumber);
// 国号: 86, 1, 852 ... @link: blog.csdn/wzygis/article/details/45073327
resultObject.put("countryCode", countryCode);
// 去掉+号和国号/区号后的实际号码
resultObject.put("nationalNumber", nationalNumber);
// 所在地区描述信息
美国电话号码resultObject.put("description", description);
// 去掉+号后的号码 (⽤于阿⾥云)
resultObject.put("number", String.valueOf(countryCode) + nationalNumber);
resultObject.put("fullNumber", phoneNumber);
return resultObject;
}
public static void main(String[] args) throws Exception {
// {"number":"852********","regionCode":"HK","nationalNumber":68476749,"countryCode":852,"description":"⾹港","fullNumber":"+852********"} System.out.PhoneNumberInfo("+852********").toJSONString());
// {"number":"16467879865","regionCode":"US","nationalNumber":6467879865,"countryCode":1,"description":"美国","fullNumber":"+16467879865"} System.out.PhoneNumberInfo("+16467879865").toJSONString());
四大官窑是哪四个// The phone number maybe like:+[National Number][Phone number], but got +86155xxxx331
System.out.PhoneNumberInfo("+86155xxxx331").toJSONString());
}
}
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
发布评论