如何对电话号码进行有效判断,并对其添加掩码,加密等相关措施,本实例是我们在实际工作中遇到的情况进行了归类总结,具有很强的适用性,分享出来给大家!!
package main;
/**
* 电话号码处理工具
*
* @author Ligang.Wang[wlgchun@163.com]
* @date 2017年8月22日 下午3:10:50
*/
public class TelephoneUtil {
public static boolean FormatTransfer = true;// 是否将电话号码统一格式化为xxx-xxxx-xxx
public static boolean MosaicSwtich = true;// 是否将电话号码统一格式化为xxx-xxxx-xxx
public static final String MosaicMask = "*****************";// 声明常量掩码,最大支持18位
public static final String[] TelephoneSeparator = new String[] { "-", "\\(", "\\)", "(", ")", "转", "·" };// 几个常见的座机号码常见分隔符
/**
* 添加掩码
*
* @author Ligang.Wang[wlgchun@163.com]
* @date 2017年8月22日下午6:39:03
* @param telePhoneNum
* @return String
*/
public static String mosaic(String telePhoneNum) {
return isAlpha(telePhoneNum, TelephoneType.Mobile) ? mosaic(telePhoneNum, TelephoneType.Mobile) : mosaic(telePhoneNum, TelephoneType.Telephone);
}
public static String mosaic(String telePhoneNum, TelephoneType telephoneType) {
return telephoneType.mosaic(telePhoneNum);
}
/**
* 格式化电话号码
*
* @author Ligang.Wang[wlgchun@163.com]
* @date 2017年8月22日下午6:39:14
* @param telePhoneNum
* @return String
*/
public static String format(String telePhoneNum) {
return isAlpha(telePhoneNum, TelephoneType.Mobile) ? TelephoneType.Mobile.format(telePhoneNum) : TelephoneType.Telephone.format(telePhoneNum);
}
public static String format(String telePhoneNum, TelephoneType telephoneType) {
return telephoneType.format(telePhoneNum);
}
/**
* 判断是否电话号码
*
* @author Ligang.Wang[wlgchun@163.com]
* @date 2017年8月22日下午6:39:32
* @param telePhoneNum
* @return boolean
*/
public static boolean isAlpha(String telePhoneNum) {
return !isAlpha(telePhoneNum, TelephoneType.Mobile) && !isAlpha(telePhoneNum, TelephoneType.Telephone);
}
public static boolean isAlpha(String telePhoneNum, TelephoneType telephoneType) {
return null != telephoneType.format(telePhoneNum);
}
/**
* 加密手机号码
*
* @author Ligang.Wang[wlgchun@163.com]
* @date 2017年8月22日下午6:39:49
* @param telphoneNum
* @return String
*/
public static String encrypt(String telphoneNum) {
if (!isAlpha(telphoneNum)) {
System.out.println("不是通常的电话号码");
return null;
}
String resource = Thread.currentThread().getStackTrace()[2].getClassName() + "." + Thread.currentThread().getStackTrace()[2].getMethodName();
System.out.println("调用加密得资源是:" + resource);
return "自定义加密算法";
}
/**
* 解密手机号码
*
* @author Ligang.Wang[wlgchun@163.com]
* @date 2017年8月22日下午6:39:58