package cn.fraudmetrix.octopus.horai.biz.utils; import org.springframework.util.StringUtils; import java.util.regex.Matcher;
import java.util.regex.Pattern; /**
* Author: hunt
* Date: 2016-11-17 00:15
* Version: 1.0.0
*/
public class PhoneUtils {
/**
* 是否是手机
*
* @param number
* @return
*/
public static boolean isPhoneNumber(String number) {
String rgx = "^(0|86|\\+86|086)?1[34578]\\d{9}$";
Pattern p = Pattern.compile(rgx);
Matcher m = p.matcher(number);
return m.matches();
} /**
* 是否是固话
*
* @param number
* @return
*/
public static boolean isFixNumber(String number) {
String rgx = "^(010|02\\d|0[3-9]\\d{2})?\\d{6,8}$";
Pattern p = Pattern.compile(rgx);
Matcher m = p.matcher(number);
return m.matches();
} /**
* 格式话电话号码
*
* @param originNumber
* @return
*/
public static String getFormatPhoneNumer(String originNumber) {
String formatNumber = originNumber;
if (PhoneUtils.isPhoneNumber(originNumber)) {
formatNumber = originNumber.substring(originNumber.length() - 11, originNumber.length());
} else if (originNumber.startsWith("+86")) {
formatNumber = originNumber.substring(3);
}
return formatNumber;
} /**
* 根据号码获得运营商名称
*
* @param phone
* @return
*/
public static String getCarrier(String phone) {
String ydRgx = "^1(3[4-9]|47|5[0-27-9]|78|8[2-47-8])\\d{8}$";
String ltRgx = "^1(3[0-2]|45|5[56]|7[156]|8[56])\\d{8}$";
String dxRgx = "^1(33|49|53|7[37]|8[019])\\d{8}$";
String carrier = "";
if (phone.matches(ydRgx)) {
carrier = "移动";
} else if (phone.matches(ltRgx)) {
carrier = "联通";
} else if (phone.matches(dxRgx)) {
carrier = "电信";
}
return carrier;
} /**
* 获取手机类别:手机号码、固定电话、亲情号码、其他号码
*
* @param phone
* @return
*/
public static String getPhoneType(String phone) {
String phoneType = "其它号码";
if (!StringUtils.isEmpty(phone)) {
if (isFixNumber(phone)) {
phoneType = "固定电话";
} else if (isPhoneNumber(phone)) {
phoneType = "手机号码";
}
}
return phoneType;
} public static void main(String[] args) {
String number = "8618435697926";
System.out.println(PhoneUtils.isPhoneNumber(number));
number = "+8618668233542";
String formatStr = PhoneUtils.getFormatPhoneNumer(number);
System.out.println(formatStr);
System.out.println(isFixNumber("0688904000"));
}
}