123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- package com.yihu.utils;
- /**
- * @Title: TelephoneUtil.java
- * @Description: TODO(用一句话描述该文件做什么)
- * @author yangshl
- * @date 2015年5月22日 下午5:08:12
- */
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class TelephoneUtil {
- //用于匹配手机号码
- private final static String REGEX_MOBILEPHONE = "^0?1[34578]\\d{9}$";
- //用于匹配固定电话号码
- private final static String REGEX_FIXEDPHONE = "^(010|02\\d|0[3-9]\\d{2})?\\d{6,8}$";
- //用于获取固定电话中的区号
- private final static String REGEX_ZIPCODE = "^(010|02\\d|0[3-9]\\d{2})\\d{6,8}$";
- private static Pattern PATTERN_MOBILEPHONE;
- private static Pattern PATTERN_FIXEDPHONE;
- private static Pattern PATTERN_ZIPCODE;
- static {
- PATTERN_FIXEDPHONE = Pattern.compile(REGEX_FIXEDPHONE);
- PATTERN_MOBILEPHONE = Pattern.compile(REGEX_MOBILEPHONE);
- PATTERN_ZIPCODE = Pattern.compile(REGEX_ZIPCODE);
- }
- public static enum PhoneType {
- /**
- * 手机
- */
- CELLPHONE,
- /**
- * 固定电话
- */
- FIXEDPHONE,
- /**
- * 非法格式号码
- */
- INVALIDPHONE
- }
- public static class Number {
- private PhoneType type;
- /**
- * 如果是手机号码,则该字段存储的是手机号码 前七位;如果是固定电话,则该字段存储的是区号
- */
- private String code;
- private String number;
- public Number(PhoneType _type, String _code, String _number) {
- this.type = _type;
- this.code = _code;
- this.number = _number;
- }
- public PhoneType getType() {
- return type;
- }
- public String getCode() {
- return code;
- }
- public String getNumber() {
- return number;
- }
- public String toString(){
- return String.format("[number:%s, type:%s, code:%s]", number, type.name(), code);
- }
- }
- /**
- * 判断是否为手机号码
- *
- * @param number
- * 手机号码
- * @return
- */
- public static boolean isCellPhone(String number) {
- Matcher match = PATTERN_MOBILEPHONE.matcher(number);
- return match.matches();
- }
- /**
- * 判断是否为固定电话号码
- *
- * @param number
- * 固定电话号码
- * @return
- */
- public static boolean isFixedPhone(String number) {
- Matcher match = PATTERN_FIXEDPHONE.matcher(number);
- return match.matches();
- }
- /**
- * 获取固定号码号码中的区号
- *
- * @param strNumber
- * @return
- */
- public static String getZipFromHomephone(String strNumber) {
- Matcher matcher = PATTERN_ZIPCODE.matcher(strNumber);
-
- if (matcher.find()) {
- return matcher.group(1);
- }
-
-
- return null;
- }
- /**
- * 检查号码类型,并获取号码前缀,手机获取前7位,固话获取区号
- * @param number
- * @return
- */
- public static Number checkNumber(String _number){
- String number = _number;
- Number rtNum = null;
- if(number!=null && number.length()>0){
- if(isCellPhone(number)){
- //如果手机号码以0开始,则去掉0
- if(number.charAt(0) == '0'){
- number = number.substring(1);
- }
- rtNum = new Number(PhoneType.CELLPHONE, number.substring(0, 7), _number);
- }else if(isFixedPhone(number)){
- //获取区号
- String zipCode = getZipFromHomephone(number);
- rtNum = new Number(PhoneType.FIXEDPHONE, zipCode, _number);
- }else{
- rtNum = new Number(PhoneType.INVALIDPHONE, null, _number);
- }
- }
- return rtNum;
- }
-
-
- public static void main(String[] args){
- // Number num = checkNumber("05995216043");
- // System.out.println(num);
- System.out.println("05995216043".substring(4));
- //
- // num = checkNumber("13951699549");
- // System.out.println(num);
- //
- // num = checkNumber("051687189099");
- // System.out.println(num);
- //
- // num = checkNumber("0215216043");
- // System.out.println(num);
- //
- // num = checkNumber("05995216043");
- // System.out.println(num);
- //
- // System.out.println(TelephoneUtil.getMobileNoTrack("18950249199"));
-
- System.out.println(TelephoneUtil.isCellPhone("17822222222"));
- }
- }
|