這篇文章給大家介紹Java中怎么獲取手機(jī)號(hào)碼歸屬地,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
創(chuàng)新互聯(lián)公司是一家網(wǎng)站設(shè)計(jì)公司,集創(chuàng)意、互聯(lián)網(wǎng)應(yīng)用、軟件技術(shù)為一體的創(chuàng)意網(wǎng)站建設(shè)服務(wù)商,主營(yíng)產(chǎn)品:成都響應(yīng)式網(wǎng)站建設(shè)、品牌網(wǎng)站制作、網(wǎng)絡(luò)營(yíng)銷推廣。我們專注企業(yè)品牌在網(wǎng)站中的整體樹立,網(wǎng)絡(luò)互動(dòng)的體驗(yàn),以及在手機(jī)等移動(dòng)端的優(yōu)質(zhì)呈現(xiàn)。成都網(wǎng)站制作、做網(wǎng)站、移動(dòng)互聯(lián)產(chǎn)品、網(wǎng)絡(luò)運(yùn)營(yíng)、VI設(shè)計(jì)、云產(chǎn)品.運(yùn)維為核心業(yè)務(wù)。為用戶提供一站式解決方案,我們深知市場(chǎng)的競(jìng)爭(zhēng)激烈,認(rèn)真對(duì)待每位客戶,為客戶提供賞析悅目的作品,網(wǎng)站的價(jià)值服務(wù)。
引入依賴包
工具類方法
package cn.net.jarvis.common.core;import com.alibaba.fastjson.JSONObject;import com.google.i18n.phonenumbers.NumberParseException;import com.google.i18n.phonenumbers.PhoneNumberUtil;import com.google.i18n.phonenumbers.Phonenumber;import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber;import com.google.i18n.phonenumbers.geocoding.PhoneNumberOfflineGeocoder;import java.util.Locale;import java.util.regex.Matcher;import java.util.regex.Pattern;/** * @author Mao, hongtu1993@sina.cn * @version Enter version here..., 5:47 PM 2019/12/16 */public class PhoneUtil { private static PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance(); private static PhoneNumberOfflineGeocoder geocoder = PhoneNumberOfflineGeocoder.getInstance(); /** * 根據(jù)國(guó)家代碼和手機(jī)號(hào) 判斷手機(jī)號(hào)是否有效 * @param phoneNumber 手機(jī)號(hào)碼 * @param countryCode 國(guó)號(hào)(區(qū)號(hào)) * @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 = Pattern.compile("\\+(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 { // 正則校驗(yàn) 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 Exception(e.getMessage()); } String regionCodeForNumber = phoneNumberUtil.getRegionCodeForNumber(referencePhonenumber); if (regionCodeForNumber == null) { throw new Exception("Missing region code by phone number " + phoneNumber); } boolean checkSuccess = PhoneUtil.checkPhoneNumber(referencePhonenumber.getNationalNumber(), referencePhonenumber.getCountryCode()); if (!checkSuccess) { throw new Exception("Not an active number:" + phoneNumber); } String description = geocoder.getDescriptionForNumber(referencePhonenumber, Locale.CHINA); int countryCode = referencePhonenumber.getCountryCode(); long nationalNumber = referencePhonenumber.getNationalNumber(); JSONObject resultObject = new JSONObject(); // 區(qū)域編碼 Locale : HK, US, CN ... resultObject.put("regionCode", regionCodeForNumber); // 國(guó)號(hào): 86, 1, 852 ... @link: https://blog.csdn.net/wzygis/article/details/45073327 resultObject.put("countryCode", countryCode); // 去掉+號(hào) 和 國(guó)號(hào)/區(qū)號(hào) 后的實(shí)際號(hào)碼 resultObject.put("nationalNumber", nationalNumber); // 所在地區(qū)描述信息 resultObject.put("description", description); // 去掉+號(hào)后的號(hào)碼 (用于阿里云發(fā)送短信) resultObject.put("number", String.valueOf(countryCode) + nationalNumber); resultObject.put("fullNumber", phoneNumber); return resultObject; } public static void main(String[] args) throws Exception { // {"number":"85268476749","regionCode":"HK","nationalNumber":68476749,"countryCode":852,"description":"香港","fullNumber":"+85268476749"} System.out.println(PhoneUtil.getPhoneNumberInfo("+85268476749").toJSONString()); // {"number":"16467879865","regionCode":"US","nationalNumber":6467879865,"countryCode":1,"description":"美國(guó)","fullNumber":"+16467879865"} System.out.println(PhoneUtil.getPhoneNumberInfo("+16467879865").toJSONString()); // The phone number maybe like:+[National Number][Phone number], but got +86155xxxx331 System.out.println(PhoneUtil.getPhoneNumberInfo("+86155xxxx331").toJSONString()); }}
關(guān)于Java中怎么獲取手機(jī)號(hào)碼歸屬地就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。