Java-mockMaskPhone

5 篇文章 1 订阅

https://github.com/zuojingang/common-tools-intergrated/blob/develop/src/main/java/pers/zuo/util/PhoneUtil.java

package util;

import java.util.Random;

public class PhoneUtil {

    /**
     * 手机号码正则表达式
     * "^(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;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值