3111637820592c186232ce22a6c12639ea5d4618.svn-base 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package com.yihu.utils;
  2. /**
  3. * @Title: TelephoneUtil.java
  4. * @Description: TODO(用一句话描述该文件做什么)
  5. * @author yangshl
  6. * @date 2015年5月22日 下午5:08:12
  7. */
  8. import java.util.regex.Matcher;
  9. import java.util.regex.Pattern;
  10. public class TelephoneUtil {
  11. //用于匹配手机号码
  12. private final static String REGEX_MOBILEPHONE = "^0?1[34578]\\d{9}$";
  13. //用于匹配固定电话号码
  14. private final static String REGEX_FIXEDPHONE = "^(010|02\\d|0[3-9]\\d{2})?\\d{6,8}$";
  15. //用于获取固定电话中的区号
  16. private final static String REGEX_ZIPCODE = "^(010|02\\d|0[3-9]\\d{2})\\d{6,8}$";
  17. private static Pattern PATTERN_MOBILEPHONE;
  18. private static Pattern PATTERN_FIXEDPHONE;
  19. private static Pattern PATTERN_ZIPCODE;
  20. static {
  21. PATTERN_FIXEDPHONE = Pattern.compile(REGEX_FIXEDPHONE);
  22. PATTERN_MOBILEPHONE = Pattern.compile(REGEX_MOBILEPHONE);
  23. PATTERN_ZIPCODE = Pattern.compile(REGEX_ZIPCODE);
  24. }
  25. public static enum PhoneType {
  26. /**
  27. * 手机
  28. */
  29. CELLPHONE,
  30. /**
  31. * 固定电话
  32. */
  33. FIXEDPHONE,
  34. /**
  35. * 非法格式号码
  36. */
  37. INVALIDPHONE
  38. }
  39. public static class Number {
  40. private PhoneType type;
  41. /**
  42. * 如果是手机号码,则该字段存储的是手机号码 前七位;如果是固定电话,则该字段存储的是区号
  43. */
  44. private String code;
  45. private String number;
  46. public Number(PhoneType _type, String _code, String _number) {
  47. this.type = _type;
  48. this.code = _code;
  49. this.number = _number;
  50. }
  51. public PhoneType getType() {
  52. return type;
  53. }
  54. public String getCode() {
  55. return code;
  56. }
  57. public String getNumber() {
  58. return number;
  59. }
  60. public String toString(){
  61. return String.format("[number:%s, type:%s, code:%s]", number, type.name(), code);
  62. }
  63. }
  64. /**
  65. * 判断是否为手机号码
  66. *
  67. * @param number
  68. * 手机号码
  69. * @return
  70. */
  71. public static boolean isCellPhone(String number) {
  72. Matcher match = PATTERN_MOBILEPHONE.matcher(number);
  73. return match.matches();
  74. }
  75. /**
  76. * 判断是否为固定电话号码
  77. *
  78. * @param number
  79. * 固定电话号码
  80. * @return
  81. */
  82. public static boolean isFixedPhone(String number) {
  83. Matcher match = PATTERN_FIXEDPHONE.matcher(number);
  84. return match.matches();
  85. }
  86. /**
  87. * 获取固定号码号码中的区号
  88. *
  89. * @param strNumber
  90. * @return
  91. */
  92. public static String getZipFromHomephone(String strNumber) {
  93. Matcher matcher = PATTERN_ZIPCODE.matcher(strNumber);
  94. if (matcher.find()) {
  95. return matcher.group(1);
  96. }
  97. return null;
  98. }
  99. /**
  100. * 检查号码类型,并获取号码前缀,手机获取前7位,固话获取区号
  101. * @param number
  102. * @return
  103. */
  104. public static Number checkNumber(String _number){
  105. String number = _number;
  106. Number rtNum = null;
  107. if(number!=null && number.length()>0){
  108. if(isCellPhone(number)){
  109. //如果手机号码以0开始,则去掉0
  110. if(number.charAt(0) == '0'){
  111. number = number.substring(1);
  112. }
  113. rtNum = new Number(PhoneType.CELLPHONE, number.substring(0, 7), _number);
  114. }else if(isFixedPhone(number)){
  115. //获取区号
  116. String zipCode = getZipFromHomephone(number);
  117. rtNum = new Number(PhoneType.FIXEDPHONE, zipCode, _number);
  118. }else{
  119. rtNum = new Number(PhoneType.INVALIDPHONE, null, _number);
  120. }
  121. }
  122. return rtNum;
  123. }
  124. public static void main(String[] args){
  125. // Number num = checkNumber("05995216043");
  126. // System.out.println(num);
  127. System.out.println("05995216043".substring(4));
  128. //
  129. // num = checkNumber("13951699549");
  130. // System.out.println(num);
  131. //
  132. // num = checkNumber("051687189099");
  133. // System.out.println(num);
  134. //
  135. // num = checkNumber("0215216043");
  136. // System.out.println(num);
  137. //
  138. // num = checkNumber("05995216043");
  139. // System.out.println(num);
  140. //
  141. // System.out.println(TelephoneUtil.getMobileNoTrack("18950249199"));
  142. System.out.println(TelephoneUtil.isCellPhone("17822222222"));
  143. }
  144. }