1 Star 3 Fork 0

清风/CommonUtils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
PhoneNumberUtils.java 4.62 KB
一键复制 编辑 原始数据 按行查看 历史
清风 提交于 2019-03-25 15:35 . Create PhoneNumberUtils.java
package com.te.datalake.util;
import java.util.concurrent.ThreadLocalRandom;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 号码验证工具类 段炼
*/
public class PhoneNumberUtils {
/**
* 中国移动拥有号码段为:139,138,137,136,135,134,159,158,157(3G),151,150,188(3G),187(3G
* );13个号段 中国联通拥有号码段为:130,131,132,156(3G),186(3G),185(3G);6个号段
* 中国电信拥有号码段为:133,153,189(3G),180(3G);4个号码段
*/
private static String mRegMobileStr = "^1(([3][456789])|([5][01789])|([8][78]))[0-9]{8}$";
private static String mRegMobile3GStr = "^((157)|(18[78]))[0-9]{8}$";
private static String mRegUnicomStr = "^1(([3][012])|([5][6])|([8][56]))[0-9]{8}$";
private static String mRegUnicom3GStr = "^((156)|(18[56]))[0-9]{8}$";
private static String mRegTelecomStr = "^1(([3][3])|([5][3])|([8][09]))[0-9]{8}$";
private static String mRegTelocom3GStr = "^(18[09])[0-9]{8}$";
private static String mRegPhoneString = "^(?:13\\d|15\\d)\\d{5}(\\d{3}|\\*{3})$";
private String mobile = "";
private int facilitatorType = 0;
private boolean isLawful = false;
private boolean is3G = false;
public PhoneNumberUtils(String mobile) {
this.setMobile(mobile);
}
public void setMobile(String mobile) {
if (mobile == null) {
return;
}
/** */
/** 第一步判断中国移动 */
if (mobile.matches(PhoneNumberUtils.mRegMobileStr)) {
this.mobile = mobile;
this.setFacilitatorType(0);
this.setLawful(true);
if (mobile.matches(PhoneNumberUtils.mRegMobile3GStr)) {
this.setIs3G(true);
}
}
/** */
/** 第二步判断中国联通 */
else if (mobile.matches(PhoneNumberUtils.mRegUnicomStr)) {
this.mobile = mobile;
this.setFacilitatorType(1);
this.setLawful(true);
if (mobile.matches(PhoneNumberUtils.mRegUnicom3GStr)) {
this.setIs3G(true);
}
}
/** */
/** 第三步判断中国电信 */
else if (mobile.matches(PhoneNumberUtils.mRegTelecomStr)) {
this.mobile = mobile;
this.setFacilitatorType(2);
this.setLawful(true);
if (mobile.matches(PhoneNumberUtils.mRegTelocom3GStr)) {
this.setIs3G(true);
}
}
/** */
/** 第四步判断座机 */
if (mobile.matches(PhoneNumberUtils.mRegPhoneString)) {
this.mobile = mobile;
this.setFacilitatorType(0);
this.setLawful(true);
if (mobile.matches(PhoneNumberUtils.mRegMobile3GStr)) {
this.setIs3G(true);
}
}
}
public String getMobile() {
return mobile;
}
public int getFacilitatorType() {
return facilitatorType;
}
/**
* 手机号是否合法
*/
public boolean isLawful() {
return isLawful;
}
/**
* 是否是3G
*/
public boolean isIs3G() {
return is3G;
}
/**
* 判断email格式是否正确
*
* @param email
* @return 是否正确
*/
public static boolean isEmail(String email) {
String str = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
Pattern p = Pattern.compile(str);
Matcher m = p.matcher(email);
return m.matches();
}
private void setFacilitatorType(int facilitatorType) {
this.facilitatorType = facilitatorType;
}
private void setLawful(boolean isLawful) {
this.isLawful = isLawful;
}
private void setIs3G(boolean is3G) {
this.is3G = is3G;
}
public static String generatePhoneNumber(){
ThreadLocalRandom current = ThreadLocalRandom.current();
int head3Num = current.nextInt(130, 189);
int tail8Num = current.nextInt(99999999);
String tail8Nums=reform(tail8Num);
String cadidate = head3Num + tail8Nums;
PhoneNumberUtils phoneNumberUtils = new PhoneNumberUtils(cadidate);
if (phoneNumberUtils.isLawful)
return cadidate;
else
return generatePhoneNumber();
}
private static String reform(int tail8Num) {
String s = tail8Num + "";
while (s.length()<8){
s="0"+s;
}
return s;
}
public static void main(String[] args) {
for (int i=0;i<100;++i){
System.out.println(PhoneNumberUtils.generatePhoneNumber());
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/agp5050/CommonUtils.git
[email protected]:agp5050/CommonUtils.git
agp5050
CommonUtils
CommonUtils
master

搜索帮助