TelPhoneUtils.java 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package com.zhongzheng.common.utils;
  2. import com.zhongzheng.common.utils.spring.SpringUtils;
  3. import org.springframework.context.MessageSource;
  4. import org.springframework.context.i18n.LocaleContextHolder;
  5. import java.io.IOException;
  6. import java.util.Random;
  7. /**
  8. * 手机号码工具栏
  9. *
  10. * @author hjl
  11. */
  12. public class TelPhoneUtils
  13. {
  14. //中国移动
  15. public static final String[] CHINA_MOBILE = {
  16. "134", "135", "136", "137", "138", "139", "150", "151", "152", "157", "158", "159",
  17. "182", "183", "184", "187", "188", "178", "147", "172", "198"
  18. };
  19. //中国联通
  20. public static final String[] CHINA_UNICOM = {
  21. "130", "131", "132", "145", "155", "156", "166", "171", "175", "176", "185", "186", "166"
  22. };
  23. //中国电信
  24. public static final String[] CHINA_TELECOME = {
  25. "133", "149", "153", "173", "177", "180", "181", "189", "199"
  26. };
  27. /**
  28. * 生成手机号
  29. */
  30. public static String createMobile() {
  31. StringBuilder sb = new StringBuilder();
  32. Random random = new Random();
  33. int op = random.nextInt(3);//随机运营商标志位
  34. String mobileThree;//手机号前三位
  35. int temp;
  36. switch (op) {
  37. case 0:
  38. mobileThree = CHINA_MOBILE[random.nextInt(CHINA_MOBILE.length)];
  39. break;
  40. case 1:
  41. mobileThree = CHINA_UNICOM[random.nextInt(CHINA_UNICOM.length)];
  42. break;
  43. case 2:
  44. mobileThree = CHINA_TELECOME[random.nextInt(CHINA_TELECOME.length)];
  45. break;
  46. default:
  47. mobileThree = "op标志位有误!";
  48. break;
  49. }
  50. if (mobileThree.length() > 3) {
  51. return mobileThree;
  52. }
  53. sb.append(mobileThree);
  54. //生成手机号后8位
  55. for (int i = 0; i < 8; i++) {
  56. temp = random.nextInt(10);
  57. sb.append(temp);
  58. }
  59. return sb.toString();
  60. }
  61. /**
  62. * 隐藏手机号码
  63. * @param tel
  64. * @return
  65. */
  66. public static String hideTelPhone(String tel)
  67. {
  68. String phoneNumber = tel.replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2");
  69. return phoneNumber;
  70. }
  71. }