模拟手机号工具类

package com.ai.jf.acct.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * created by hezy6 on 2019/10/12
 * 

* 此工具类用于模拟手机号 */ public class AnalogPhoneUtil { private static final Logger logger = LoggerFactory.getLogger(AnalogPhoneUtil.class); private AnalogPhoneUtil() { } public static String login(String value) { //将输入的值取hashcode int hashCode = value.hashCode(); //取绝对值 int abs = Math.abs(hashCode); logger.info("hashcode值为:{}", hashCode); logger.info("abs的值为:{}",abs); //将int转换成字符串 StringBuilder sb = new StringBuilder(String.valueOf(abs)); //获取字符串长度 int length = sb.length(); //如果字符串长度<=10,将1+缺位个数补0+字符串长度就是模拟的手机号 if (length <= 10) { for (int i = 0; i < (10 - length); i++) { sb.insert(0, "0"); } sb.insert(0, "1"); //如果字符串长度>10,就截取字符串的10位,在前面插入1就是模拟的手机号 } else { sb.insert(0, "1").substring(0, 11); } String phone = sb.toString(); logger.info("模拟的手机号为:{}", phone); return phone; } public static void main(String[] args){ String requestId = "122311111111"; String phone = AnalogPhoneUtil.login(requestId); logger.info("AnalogPhoneUtil----{}", phone); } }

你可能感兴趣的:(工具类,java)