一、手机号验证
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class IsMobileNO {
//验证手机号是否合法
public static boolean isMobileNO(String mobile){
if (mobile.length() != 11)
{
return false;
}else{
/**
* 移动号段正则表达式
*/
String pat1 = "^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$";
/**
* 联通号段正则表达式
*/
String pat2 = "^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$";
/**
* 电信号段正则表达式
*/
String pat3 = "^((133)|(153)|(177)|(18[0,1,9])|(149))\\d{8}$";
/**
* 虚拟运营商正则表达式
*/
String pat4 = "^((17)|(19)|(16))\\d{9}$";
Pattern pattern1 = Pattern.compile(pat1);
Matcher match1 = pattern1.matcher(mobile);
boolean isMatch1 = match1.matches();
if(isMatch1){
return true;
}
Pattern pattern2 = Pattern.compile(pat2);
Matcher match2 = pattern2.matcher(mobile);
boolean isMatch2 = match2.matches();
if(isMatch2){
return true;
}
Pattern pattern3 = Pattern.compile(pat3);
Matcher match3 = pattern3.matcher(mobile);
boolean isMatch3 = match3.matches();
if(isMatch3){
return true;
}
Pattern pattern4 = Pattern.compile(pat4);
Matcher match4 = pattern4.matcher(mobile);
boolean isMatch4 = match4.matches();
if(isMatch4){
return true;
}
return false;
}
}
}
二、身份证号验证
import cn.hutool.core.util.IdcardUtil;
import lombok.extern.slf4j.Slf4j;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author :
* 身份证检验工具类
* 计算方法
* 1、将前面的身份证号码17位数分别乘以不同的系数。从第一位到第十七位的系数分别为:7-9-10-5-8-4-2-1-6-3-7-9-10-5-8-4-2。
* 2、将这17位数字和系数相乘的结果相加。
* 3、用加出来和除以11,看余数是多少?
* 4、余数只可能有0-1-2-3-4-5-6-7-8-9-10这11个数字。其分别对应的最后一位身份证的号码为1-0-X -9-8-7-6-5-4-3-2。
* (即余数0对应1,余数1对应0,余数2对应X...)
* 5、通过上面得知如果余数是3,就会在身份证的第18位数字上出现的是9。如果对应的数字是2,身份证的最后一位号码就是罗马数字X。
* 例如:某男性的身份证号码为【53010219200508011X】, 我们看看这个身份证是不是符合计算规则的身份证。
* 首先我们得出前17位的乘积和
* 【(5*7)+(3*9)+(0*10)+(1*5)+(0*8)+(2*4)+(1*2)+(9*1)+(2*6)+(0*3)+(0*7)+(5*9)+(0*10)+(8*5)+(0*8)+(1*4)+(1*2)】是189,
* 然后用189除以11得出的结果是189÷11=17余下2,187÷11=17,还剩下2不能被除尽,也就是说其余数是2。
* 最后通过对应规则就可以知道余数2对应的检验码是X。所以,可以判定这是一个正确的身份证号码。
* @date :2022/4/21 16:58
*/
@Slf4j
public class IdCardUtil {
public static final String regEx = "[\n`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。, ·、? ]";
public static final String aa = "";//这里是将特殊字符换为aa字符串,""代表直接去掉
public static Boolean judgeIdCard(String idCard) {
idCard = idCard.replace(" ","");
idCard = idCard.replaceAll(ExcelPatternMsg.REGEX,"");
if (idCard.length() == 18) {
try {
char[] idCardArry = idCard.toCharArray();
//前17位加权系数
int[] idCardWi = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
// 前17位相乘相加结果
int sum = 0;
for (int i = 0; i < idCardWi.length; i++) {
int current = Integer.parseInt(String.valueOf(idCardArry[i]));
int count = current * idCardWi[i];
sum += count;
}
char idCardLast = idCardArry[17];
int idCardMod = sum % 11;
//最后一位求余对应系数
String[] idCardY = {"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2" };
return idCardY[idCardMod].equalsIgnoreCase(String.valueOf(idCardLast));
} catch (Exception e) {
e.printStackTrace();
}
}
return false;
}
/**
* 通过身份证取年龄
* @param idCard
* @return
*/
public static int getage(String idCard){
int result=0;
try{
result=IdcardUtil.getAgeByIdCard(idCard);
}catch (Exception e){
log.error("转换年龄错误,idCard={}",idCard);
}
return result;
}
/**
* 通过身份证号取性别
* @param idCard
* @return
*/
public static int getsex(String idCard){
int result=0;
try{
result=IdcardUtil.getGenderByIdCard(idCard);
}catch (Exception e){
log.error("转换性别错误,idCard={}",idCard);
}
return result;
}
public static String deleteSpecial(String idCard) {
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(idCard);//这里把想要替换的字符串传进来
return m.replaceAll(aa).trim();
}
/**
* 通过身份证号码获取出生日期(birthday)、年龄(age)、性别(sex)
* @param idCardNo 身份证号码
* @return 返回的出生日期格式:1993-05-07 性别格式:1:男,0:女
*/
public static Map<String, String> getBirthdayAgeSex(String idCardNo) {
String birthday = "";
String age = "";
String sexCode = "";
int year = Calendar.getInstance().get(Calendar.YEAR);
char[] number = idCardNo.toCharArray();
boolean flag = true;
if (number.length == 15) {
for (int x = 0; x < number.length; x++) {
if (!flag){
return new HashMap<String, String>();
}
flag = Character.isDigit(number[x]);
}
} else if (number.length == 18) {
for (int x = 0; x < number.length - 1; x++) {
if (!flag){
return new HashMap<String, String>();
}
flag = Character.isDigit(number[x]);
}
}
if (flag && idCardNo.length() == 15) {
birthday = "19" + idCardNo.substring(6, 8) + "-"
+ idCardNo.substring(8, 10) + "-"
+ idCardNo.substring(10, 12);
sexCode = Integer.parseInt(idCardNo.substring(idCardNo.length() - 3, idCardNo.length())) % 2 == 0 ? "0" : "1";
age = (year - Integer.parseInt("19" + idCardNo.substring(6, 8))) + "";
} else if (flag && idCardNo.length() == 18) {
birthday = idCardNo.substring(6, 10) + "-"
+ idCardNo.substring(10, 12) + "-"
+ idCardNo.substring(12, 14);
sexCode = Integer.parseInt(idCardNo.substring(idCardNo.length() - 4, idCardNo.length() - 1)) % 2 == 0 ? "0" : "1";
age = (year - Integer.parseInt(idCardNo.substring(6, 10))) + "";
}
Map<String, String> map = new HashMap<String, String>();
map.put("birthday", birthday);
map.put("age", age);
map.put("sex", sexCode);
return map;
}
public static void main(String[] args) {
String idCard = "'371427199611033715";
System.out.println(judgeIdCard(idCard));
System.out.println(IdcardUtil.isValidCard18(idCard));
}
}
三、http请求
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import java.util.Map;
/**
* @description: http请求工具类
* @remark: Copyright
*/
@Slf4j
public class HttpsUtil {
/**
* 调用接口 —— GET
* @param title
* @param url
* @param map
* @return
*/
public static String get(String title, String url, Map<String, Object> map) {
return get(title, url, map, 10000);
}
/**
* 调用接口 —— GET
*
* @param title 目的描述
* @param url 接口地址
* @param map 参数
* @return json数组
* @异常
*/
public static String get(String title, String url, Map<String, Object> map, int timeout) {
String mapJson = JSONObject.toJSONString(map);
log.info("\n" +
"++++++++++++++++++++++++" + "\n" +
"* 目的: {}" + "\n" +
"* 参数: {}" + "\n" +
"* URL: {} GET" + "\n" +
"-------------------------\n", title, mapJson, url);
Long requestStart = System.currentTimeMillis();
//GET调用
String json = HttpUtil.get(url, map, timeout);
Long requestEnd = System.currentTimeMillis();
log.info("\n" +
"++++++++++++++++++++++++" + "\n" +
"* 目的: {}" + "\n" +
"* 结果: {}" + "\n" +
"* 参数: {}" + "\n" +
"* URL: {} GET" + "\n" +
"* 耗时: {}ms" + "\n" +
"-------------------------\n", title, json, mapJson, url, requestEnd - requestStart);
return json;
}
/**
* post请求
* @param title
* @param url
* @param param
* @return
*/
public static String post(String title, String url, Map<String, Object> param) {
String paramStr = JSONObject.toJSONString(param);
return post(title, url, paramStr, 10000);
}
/**
* 调用接口 —— POST
*
* @param title 目的描述
* @param url 接口地址
* @param paramStr 参数
* @return json对象
* @异常
*/
public static String post(String title, String url, String paramStr, int timeout) {
log.info("\n" +
"++++++++++++++++++++++++" + "\n" +
"* 目的: {}" + "\n" +
"* 参数: {}" + "\n" +
"* URL: {} POST" + "\n" +
"-------------------------\n", title, paramStr, url);
Long requestStart = System.currentTimeMillis();
//POST调用
String json = HttpUtil.post(url, paramStr, timeout);
Long requestEnd = System.currentTimeMillis();
log.info("\n" +
"++++++++++++++++++++++++" + "\n" +
"* 目的: {}" + "\n" +
"* 结果: {}" + "\n" +
"* 参数: {}" + "\n" +
"* URL: {} POST" + "\n" +
"* 耗时: {}ms" + "\n" +
"-------------------------\n", title, json, paramStr, url, requestEnd - requestStart);
return json;
}
}
四、设置对象属性
import java.lang.reflect.Field;
import java.lang.reflect.Type;
/**
* @classDesc: 功能描述: 工具类
*/
public class ObjectTools {
/**
* 把对象中的 String 类型的null字段,转换为空字符串
* int为null的转为0
* boolean为null的转为false
* @param <T> 待转化对象类型
* @param cls 待转化对象
* @return 转化好的对象
*/
public static <T> T noNullStringAttr(T cls) {
Field[] fields = cls.getClass().getDeclaredFields();
if (fields == null || fields.length == 0) {
return cls;
}
for (Field field : fields) {
if ("String".equals(field.getType().getSimpleName())) {
field.setAccessible(true);
try {
Object value = field.get(cls);
if (value == null) {
field.set(cls, "");
}
} catch (IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace();
}
}else if("Boolean".equals(field.getType().getSimpleName())) {
field.setAccessible(true);
try {
Object value = field.get(cls);
if (value == null) {
field.set(cls, false);
}
} catch (IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace();
}
}
else if("int".equals(field.getType().getSimpleName())) {
field.setAccessible(true);
try {
Object value = field.get(cls);
if (value == null) {
field.set(cls, 0);
}
} catch (IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace();
}
}
else if("Integer".equals(field.getType().getSimpleName())) {
field.setAccessible(true);
try {
Object value = field.get(cls);
if (value == null) {
field.set(cls, 0);
}
} catch (IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace();
}
}
}
return cls;
}
public static boolean objCheckIsNull(Object object) {
Class clazz = (Class) object.getClass(); // 得到类对象
Field fields[] = clazz.getDeclaredFields(); // 得到所有属性
boolean flag = true; // 定义返回结果,默认为true
for (Field field : fields) {
field.setAccessible(true);
Object fieldValue = null;
try {
fieldValue = field.get(object); // 得到属性值
Type fieldType = field.getGenericType();// 得到属性类型
String fieldName = field.getName(); // 得到属性名
System.out.println("属性类型:" + fieldType + ",属性名:" + fieldName
+ ",属性值:" + fieldValue);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
if (fieldValue != null) { // 只要有一个属性值不为null 就返回false 表示对象不为null
flag = false;
break;
}
}
return flag;
}
}