Libphonenumber is a Google-developed common Java class Library for parsing, formatting, storing, and validating international phone numbers. Suitable for smartphones, while providing a JavaScript transplant.
Give an example
Package com.lei.util;
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;
Import Com.okcoin.util.StringUtil;
/** code example Stringutil is a string tool class, not provided, and can be replaced. You can use the string native function to replace the/public class Phoneutil {private static phonenumberutil Phonenumberutil = Phonenumberutil.getin
Stance ();
private static Phonenumbertocarriermapper Carriermapper = Phonenumbertocarriermapper.getinstance ();
private static Phonenumberofflinegeocoder Geocoder = Phonenumberofflinegeocoder.getinstance (); /** * According to the country code and cell phone number to determine whether the cell phone number is valid * @param phonenumber * @param countrycode * @return * * Public STA Tic Boolean Checkphonenumber (String PhoneNumber, string countrycode) {int ccode = Stringutil.tointeger (countrycod
e); Long phone= Stringutil.tolong (PhoneNumber, 0L);
PhoneNumber pn = new PhoneNumber ();
Pn.setcountrycode (Ccode);
Pn.setnationalnumber (phone);
return Phonenumberutil.isvalidnumber (PN); /** * According to the country code and cell phone number to determine the mobile phone operator * @param phonenumber * @param * * * @return * * * * * * * * publi C Static String Getcarrier (String PhoneNumber, string countrycode) {int ccode = Stringutil.tointeger (CountryCode)
;
Long phone = Stringutil.tolong (PhoneNumber, 0L);
PhoneNumber pn = new PhoneNumber ();
Pn.setcountrycode (Ccode);
Pn.setnationalnumber (phone);
Return result only in English, turn into Chinese String carrieren = carriermapper.getnamefornumber (pn, Locale.english);
String Carrierzh = "";
Carrierzh + = Geocoder.getdescriptionfornumber (pn, Locale.chinese);
Switch (carrieren) {case "Carrierzh": + + = "mobile";
Break Case "Unicom": Carrierzh+ = "Unicom";
Break
Case "Telecom": Carrierzh + + "telecom";
Break
Default:break;
return Carrierzh;
/** * * @Description: According to the country code and mobile phone number of the home * @date 2015-7-13 11:33:18 * @param @param phonenumber * @param @param countrycode * @param @return parameter * @throws/public static string Getgeo (String p
Honenumber, String countrycode) {int ccode = Stringutil.tointeger (CountryCode);
Long phone = Stringutil.tolong (PhoneNumber, 0L);
PhoneNumber pn = new PhoneNumber ();
Pn.setcountrycode (Ccode);
Pn.setnationalnumber (phone);
Return Geocoder.getdescriptionfornumber (PN, Locale.chinese);
public static void Main (string[] args) {System.out.println (Phoneutil.getcarrier ("159655555", "86")); }
}
Related Information
GitHub Address: Https://github.com/googlei18n/libphonenumber
MAVEN address
<dependency>
<groupId>com.googlecode.libphonenumber</groupId>
< artifactid>libphonenumber</artifactid>
<version>7.0</version>
</dependency>
<dependency>
<groupId>com.googlecode.libphonenumber</groupId>
<artifactid >carrier</artifactId>
<version>1.11</version>
</dependency>
< dependency>
<groupId>com.googlecode.libphonenumber</groupId>
<artifactId> geocoder</artifactid>
<version>2.12</version>
</dependency>