/**
* 手机号码正则表达式
* "^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\\d{8}$"
*/
public static String mockMaskPhone() {
int first = 1;
int second = RandomUtil.randomInt(3, 10);
int third = 0;
int[] temp = null;
switch (second) {
case 3:
case 8:
third = RandomUtil.randomInt(0, 10);
break;
case 4:
temp = new int[] { 5, 7, 9 };
third = temp[RandomUtil.randomInt(0, 3)];
break;
case 5:
temp = new int[] { 0, 1, 2, 3, 5, 6, 7, 8, 9 };
third = temp[RandomUtil.randomInt(0, 9)];
break;
case 6:
third = 6;
break;
case 7:
temp = new int[] { 0, 1, 3, 5, 6, 7, 8 };
third = temp[RandomUtil.randomInt(0, 7)];
break;
case 9:
third = RandomUtil.randomInt(8, 10);
break;
default:
break;
}
Integer lastFour = 0;
do {
lastFour = RandomUtil.randomInt(1000, 9999);
String laString = lastFour.toString();
for (int i = 0; i < laString.length(); i++) {
char j = laString.charAt(i);
String afterReplace = laString.replace(new String(new char[] { j }), "");
if (afterReplace.trim().length() <= 1) {
lastFour = 0;
break;
}
}
} while (0 == lastFour);
String mockPhone = new StringBuilder().append(first).append(second).append(third).append("****").append(lastFour).toString();
return mockPhone;
}
public static int randomInt(int from, int to) {
if (from < 0 || to <= 0 || (to - from) <= 0) {
return 0;
}
Random random = new Random();
random.nextInt();
return random.nextInt(to - from) + from;
}
网友评论