常用工具类
来源:互联网 发布:淘宝会员抓取软件 编辑:程序博客网 时间:2024/04/26 06:01
* 方法描述:通过身份证获得年龄
*
* @param IDNo
* @return
*/
public static String getAgeByIDNo(String IDNo) {
int age = 0;
if (StringUtils.isNotBlank(IDNo)) {
int IDNolength = IDNo.length();
if (IDNolength == 18 || IDNolength == 15) {
int IDYear = 0;
int IDMonth = 0;
int IDDay = 0;
Calendar ca = Calendar.getInstance();
int nowYear = ca.get(Calendar.YEAR);
int nowMonth = ca.get(Calendar.MONTH) + 1;
int nowDay = ca.get(Calendar.DAY_OF_MONTH);
if (IDNo.length() == 18) {
IDYear = Integer.valueOf(IDNo.substring(6, 10));
IDMonth = Integer.valueOf(IDNo.substring(10, 12));
IDDay = Integer.valueOf(IDNo.substring(12, 14));
} else {
IDYear = Integer.valueOf("19" + IDNo.substring(6, 8));
IDMonth = Integer.valueOf(IDNo.substring(8, 10));
IDDay = Integer.valueOf(IDNo.substring(10, 12));
}
if (nowMonth > IDMonth) {
age = nowYear - IDYear;
} else if (nowMonth < IDMonth) {
age = nowYear - IDYear - 1;
} else if (nowMonth == IDMonth) {
if (nowDay >= IDDay) {
age = nowYear - IDYear;
} else {
age = nowYear - IDYear - 1;
}
}
} else {
logger.info("证件号码长度不正确");
}
} else {
logger.info("证件号码为空");
}
return String.valueOf(age);
}
/**
*
* getRandomNumberString 获取数字短信验证码
*
* @param size
* @return
*/
public static String getRandomNumberString(int size) {
char[] c = { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' };
Random random = new Random();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < size; i++) {
sb.append(c[Math.abs(random.nextInt()) % c.length]);
}
return sb.toString();
}
/**
*
* getMemberName随机生成单位长度的字符串
*
* @return
*/
public static String getMemberName(int size) {
String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Random random = new Random();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < size; i++) {
int number = random.nextInt(base.length());
sb.append(base.charAt(number));
}
return sb.toString();
}
*
* md5 32位MD5加密
*
* @param text
* 需要加密的字符串
* @return
* @throws Exception
*/
public static String encrypt(String text) throws Exception {
if (StringUtils.isEmpty(text))
throw new Exception("需要加密的字符串不能为空!");
StringBuffer hexString = new StringBuffer();
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(text.getBytes());
byte[] digest = md.digest();
for (int i = 0; i < digest.length; i++) {
text = Integer.toHexString(0xFF & digest[i]);
if (text.length() < 2) {
text = "0" + text;
}
hexString.append(text);
}
return hexString.toString().toLowerCase();
}
public class ValidateUtil {
/**
* 手机端手机号码校验
*
* @param tel
* 手机号码
* @return
*/
public static boolean validateTel(String tel) {
String regex = "^1[34578]\\d{9}$";
return match(regex, tel);
}
/**
* 手机端短信验证码校验
*
* @param code
* 短信验证码
* @return
*/
public static boolean validateCode(String code) {
String regex = "^[0-9]{6}$";
return match(regex, code);
}
/**
* 手机端注册密码校验
*
* @param pwd
* 手机端用户注册密码
*/
public static boolean validatePwd(String pwd) {
String regex = "^[0-9A-Za-z]{6,20}$";
return match(regex, pwd);
}
/**
* 手机端姓名校验(身份证、银行卡绑定)
*
* @param name
* 姓名
*/
public static boolean validateName(String name) {
String regex = "^([\u4E00-\uFA29]|[\uE7C7-\uE7F3])+$";
return match(regex, name);
}
/**
* 手机端身份证号码校验
*
* @param idNo
* 身份证号
*/
public static boolean validateIdNo(String idNo) {
String regex = "(^\\d{15}$)|(^\\d{18}$)|(^\\d{17}(\\d|X|x)$)";
return match(regex, idNo);
}
/**
* 手机端银行卡号校验
*
* @param bankAccount
* 银行卡号
*/
public static boolean validateBankAccount(String bankAccount) {
String regex = "^[0-9]{16,19}$";
return match(regex, bankAccount);
}
/**
* 手机端电子邮箱地址正则校验
*/
public static boolean validateEmail(String email) {
String regex = "^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\\.[a-zA-Z0-9_-]{2,3}){1,2})$";
return match(regex, email);
}
/**
* @param regex
* 正则表达式字符串
* @param str
* 要匹配的字符串
* @return 如果str 符合 regex的正则表达式格式,返回true, 否则返回 false;
*/
public static boolean match(String regex, String str) {
boolean flag = false;
if (StringUtils.isNotBlank(str)) {
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
flag = matcher.matches();
}
return flag;
}
}
- 常用工具类
- 常用工具类
- 常用工具类
- 常用工具类
- 常用工具类
- 常用工具类
- 常用工具类
- 常用工具类
- 常用工具类
- 常用工具类
- 常用工具类
- 常用工具类
- 常用工具类
- 常用工具类
- 常用工具类
- 常用工具类
- 常用工具类
- 常用工具类
- webSphere操作步骤
- 学习linux下安装phpmyadmin
- 笔记
- poj1487--Single-Player Games(高斯消元)+测试数据
- 黑马程序员-反射
- 常用工具类
- hdoj 1241 Oil Deposits【DFS】
- LeetCode(33)(81) Search in Rotated Sorted Array I II
- Customer Class
- CRectTracker源码学习笔记
- AFNetworking 2.5
- UVA11059-Maximum Product
- Trie树兄弟单词实例
- POJ 1442 Black Box(优先队列)