1 package cn.fraudmetrix.octopus.horai.biz.utils; 2 3 import org.springframework.util.StringUtils; 4 5 import java.util.regex.Matcher; 6 import java.u ...
1 package cn.fraudmetrix.octopus.horai.biz.utils; 2 3 import org.springframework.util.StringUtils; 4 5 import java.util.regex.Matcher; 6 import java.util.regex.Pattern; 7 8 /** 9 * Author: hunt 10 * Date: 2016-11-17 00:15 11 * Version: 1.0.0 12 */ 13 public class PhoneUtils { 14 /** 15 * 是否是手機 16 * 17 * @param number 18 * @return 19 */ 20 public static boolean isPhoneNumber(String number) { 21 String rgx = "^(0|86|\\+86|086)?1[34578]\\d{9}$"; 22 Pattern p = Pattern.compile(rgx); 23 Matcher m = p.matcher(number); 24 return m.matches(); 25 } 26 27 /** 28 * 是否是固話 29 * 30 * @param number 31 * @return 32 */ 33 public static boolean isFixNumber(String number) { 34 String rgx = "^(010|02\\d|0[3-9]\\d{2})?\\d{6,8}$"; 35 Pattern p = Pattern.compile(rgx); 36 Matcher m = p.matcher(number); 37 return m.matches(); 38 } 39 40 /** 41 * 格式話電話號碼 42 * 43 * @param originNumber 44 * @return 45 */ 46 public static String getFormatPhoneNumer(String originNumber) { 47 String formatNumber = originNumber; 48 if (PhoneUtils.isPhoneNumber(originNumber)) { 49 formatNumber = originNumber.substring(originNumber.length() - 11, originNumber.length()); 50 } else if (originNumber.startsWith("+86")) { 51 formatNumber = originNumber.substring(3); 52 } 53 return formatNumber; 54 } 55 56 /** 57 * 根據號碼獲得運營商名稱 58 * 59 * @param phone 60 * @return 61 */ 62 public static String getCarrier(String phone) { 63 String ydRgx = "^1(3[4-9]|47|5[0-27-9]|78|8[2-47-8])\\d{8}$"; 64 String ltRgx = "^1(3[0-2]|45|5[56]|7[156]|8[56])\\d{8}$"; 65 String dxRgx = "^1(33|49|53|7[37]|8[019])\\d{8}$"; 66 String carrier = ""; 67 if (phone.matches(ydRgx)) { 68 carrier = "移動"; 69 } else if (phone.matches(ltRgx)) { 70 carrier = "聯通"; 71 } else if (phone.matches(dxRgx)) { 72 carrier = "電信"; 73 } 74 return carrier; 75 } 76 77 /** 78 * 獲取手機類別:手機號碼、固定電話、親情號碼、其他號碼 79 * 80 * @param phone 81 * @return 82 */ 83 public static String getPhoneType(String phone) { 84 String phoneType = "其它號碼"; 85 if (!StringUtils.isEmpty(phone)) { 86 if (isFixNumber(phone)) { 87 phoneType = "固定電話"; 88 } else if (isPhoneNumber(phone)) { 89 phoneType = "手機號碼"; 90 } 91 } 92 return phoneType; 93 } 94 95 public static void main(String[] args) { 96 String number = "8618435697926"; 97 System.out.println(PhoneUtils.isPhoneNumber(number)); 98 number = "+8618668233542"; 99 String formatStr = PhoneUtils.getFormatPhoneNumer(number); 100 System.out.println(formatStr); 101 System.out.println(isFixNumber("0688904000")); 102 } 103 }View Code