package com.sinosoft.common; import com.sinosoft.dto.UsersinfoDTO; import com.sinosoft.dto.common.RequestInfo; import org.apache.commons.lang3.StringUtils; import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; //import javax.servlet.http.HttpServletRequest; /** * Created by xushuyi on 2017/3/13. */ public class CommonUtil { public static final int MIN = 1930; //最低年限 public static final int power[] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};//每位加权因子 // 手机号码判断 public static boolean isMobileNO(String mobiles) { Pattern p = Pattern.compile("^1[0-9]{10}$"); Matcher m = p.matcher(mobiles); return m.matches(); } // 邮箱格式判断 public static boolean isEmail(String email) { String str = "^[0-9a-zA-Z_][-_\\.0-9a-zA-Z-]{0,63}@([0-9a-zA-Z][0-9a-zA-Z-]*\\.)+[a-zA-Z]{2,4}$"; System.out.println(str); Pattern p = Pattern.compile(str); Matcher m = p.matcher(email); return m.matches(); } //用户账号格式判断 public static boolean isUserAccount(String userAccount) { String str = "^[a-zA-Z_]{1}\\w{5,15}$";//tiansankun 描述:注册用户名格式校验修改 Pattern p = Pattern.compile(str); Matcher m = p.matcher(userAccount); return m.matches(); } //用户密码判断 public static boolean isUserPwd(String userpwd) { String str = "^[\\w-]{6,16}$"; Pattern p = Pattern.compile(str); Matcher m = p.matcher(userpwd); return m.matches(); } //真实姓名格式判断 public static boolean isUserName(String userName) { userName = userName.replaceAll(" ", ""); String str = "^[\u4e00-\u9fa5]{1}[\u4e00-\u9fa5\\·]{0,48}[\u4e00-\u9fa5]{1}$"; Pattern p = Pattern.compile(str); Matcher m = p.matcher(userName); return m.matches(); } //护照格式判断 public static boolean isPassport(String passport) { passport = passport.replaceAll(" ", ""); String str = "^([a-zA-Z]{5,17})|([a-zA-Z0-9]{5,17})$"; Pattern p = Pattern.compile(str); Matcher m = p.matcher(passport); return m.matches(); } //邮编格式判断 public static boolean isZipCode(String zipCode) { String str = "^\\d{6}$"; Pattern p = Pattern.compile(str); Matcher m = p.matcher(zipCode); return m.matches(); } //组织机构代码 public static boolean isOrganizeCode(String organizeCode) { String str = "^([0-9A-Z]){8}[-]?[0-9|X]$"; Pattern p = Pattern.compile(str); Matcher m = p.matcher(organizeCode); return m.matches(); } //座机电话 public static boolean isOfficePhone(String officePhone) { String str = "(^(\\d{11})$|^((\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1})|(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1}))$)"; Pattern p = Pattern.compile(str); Matcher m = p.matcher(officePhone); return m.matches(); } //金额 public static boolean isRegisteredCapital(String registeredCapital) { String str = "^(([1-9]\\d{0,11})|0)(\\.\\d{1,2})?$"; Pattern p = Pattern.compile(str); Matcher m = p.matcher(registeredCapital); return m.matches(); } // 性别 public static boolean isSex(String sex) { if (sex.equals("1") || sex.equals("2")) { return true; } return false; } // 验证字符串是否是BigDecimal public static boolean isBigDecimal(String str) { Matcher match = null; if (isNumeric(str) == true) { Pattern pattern = Pattern.compile("[0-9]*"); match = pattern.matcher(str.trim()); } else { if (str.trim().indexOf(".") == -1) { Pattern pattern = Pattern.compile("^[+]?[0-9]*"); match = pattern.matcher(str.trim()); } else { Pattern pattern = Pattern.compile("^[+]?[0-9]+(\\.\\d{1,100}){1}"); match = pattern.matcher(str.trim()); } } return match.matches(); } // 银行账号校验 public static boolean isBankAccNo(String str) { if (!isNumeric(str)) { return false; } if (str.length() != 15 && str.length() != 16 && str.length() != 19 && str.length() != 23) { return false; } return true; } /* * 功能:身份证的有效验证 * @param IDStr 身份证号 * @return 有效:返回"" 无效:返回String信息 * @throws ParseException */ @SuppressWarnings("unchecked") public static String IDCardValidate(String IDStr, Date birthday, String sex) throws ParseException { String errorInfo = "";// 记录错误信息 String[] ValCodeArr = {"1", "0", "x", "9", "8", "7", "6", "5", "4", "3", "2"}; String[] Wi = {"7", "9", "10", "5", "8", "4", "2", "1", "6", "3", "7", "9", "10", "5", "8", "4", "2"}; String Ai = ""; // ================ 号码的长度 15位或18位 ================ if (IDStr.length() != 15 && IDStr.length() != 18) { errorInfo = "身份证号码长度应该为15位或18位。"; return errorInfo; } // =======================(end)======================== // ================ 数字 除最后以为都为数字 ================ //性别位 int gender = 0; if (IDStr.length() == 18) { Ai = IDStr.substring(0, 17); gender = Integer.parseInt(IDStr.substring(16, 17)) % 2 == 1 ? 1 : 2; } else if (IDStr.length() == 15) { gender = Integer.parseInt(IDStr.substring(14, 15)) % 2 == 1 ? 1 : 2; Ai = IDStr.substring(0, 6) + "19" + IDStr.substring(6, 15); } if (isNumeric(Ai) == false) { errorInfo = "身份证15位号码都应为数字 ; 18位号码除最后一位外,都应为数字。"; return errorInfo; } // =======================(end)======================== //性别判断 if (!StringUtils.isEmpty(sex) && !String.valueOf(gender).equals(sex)) { errorInfo = "身份证号码与性别不匹配!"; return errorInfo; } // ================ 出生年月是否有效 ================ String strYear = Ai.substring(6, 10);// 年份 String strMonth = Ai.substring(10, 12);// 月份 String strDay = Ai.substring(12, 14);// 月份 if (isDate(strYear + "-" + strMonth + "-" + strDay) == false) { errorInfo = "身份证生日无效。"; return errorInfo; } GregorianCalendar gc = new GregorianCalendar(); SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd"); if (birthday != null && !(strYear + "-" + strMonth + "-" + strDay).equals(s.format(birthday))) { errorInfo = "身份证号码与出生日期不匹配!"; return errorInfo; } if ((gc.get(Calendar.YEAR) - Integer.parseInt(strYear)) > 150 || (gc.getTime().getTime() - s.parse( strYear + "-" + strMonth + "-" + strDay).getTime()) < 0) { errorInfo = "身份证生日不在有效范围。"; return errorInfo; } if (Integer.parseInt(strMonth) > 12 || Integer.parseInt(strMonth) == 0) { errorInfo = "身份证月份无效"; return errorInfo; } if (Integer.parseInt(strDay) > 31 || Integer.parseInt(strDay) == 0) { errorInfo = "身份证日期无效"; return errorInfo; } // =====================(end)===================== // ================ 地区码时候有效 ================ Hashtable h = GetAreaCode(); if (h.get(Ai.substring(0, 2)) == null) { errorInfo = "身份证地区编码错误。"; return errorInfo; } // ============================================== // ================ 判断最后一位的值 ================ int TotalmulAiWi = 0; for (int i = 0; i < 17; i++) { TotalmulAiWi = TotalmulAiWi + Integer.parseInt(String.valueOf(Ai.charAt(i))) * Integer.parseInt(Wi[i]); } int modValue = TotalmulAiWi % 11; String strVerifyCode = ValCodeArr[modValue]; Ai = Ai + strVerifyCode; if (IDStr.length() == 18) { if (Ai.equals(IDStr.toLowerCase()) == false) { errorInfo = "身份证无效,不是合法的身份证号码"; return errorInfo; } } else { return ""; } // =====================(end)===================== return ""; } /** * 功能:设置地区编码 * * @return Hashtable 对象 */ @SuppressWarnings("unchecked") public static Hashtable GetAreaCode() { Hashtable hashtable = new Hashtable(); hashtable.put("11", "北京"); hashtable.put("12", "天津"); hashtable.put("13", "河北"); hashtable.put("14", "山西"); hashtable.put("15", "内蒙古"); hashtable.put("21", "辽宁"); hashtable.put("22", "吉林"); hashtable.put("23", "黑龙江"); hashtable.put("31", "上海"); hashtable.put("32", "江苏"); hashtable.put("33", "浙江"); hashtable.put("34", "安徽"); hashtable.put("35", "福建"); hashtable.put("36", "江西"); hashtable.put("37", "山东"); hashtable.put("41", "河南"); hashtable.put("42", "湖北"); hashtable.put("43", "湖南"); hashtable.put("44", "广东"); hashtable.put("45", "广西"); hashtable.put("46", "海南"); hashtable.put("50", "重庆"); hashtable.put("51", "四川"); hashtable.put("52", "贵州"); hashtable.put("53", "云南"); hashtable.put("54", "西藏"); hashtable.put("61", "陕西"); hashtable.put("62", "甘肃"); hashtable.put("63", "青海"); hashtable.put("64", "宁夏"); hashtable.put("65", "新疆"); hashtable.put("71", "台湾"); hashtable.put("81", "香港"); hashtable.put("82", "澳门"); hashtable.put("91", "国外"); return hashtable; } /** * 功能:判断字符串是否为数字 * * @param str * @return */ public static boolean isNumeric(String str) { Pattern pattern = Pattern.compile("[0-9]*"); Matcher isNum = pattern.matcher(str); if (isNum.matches()) { return true; } else { return false; } } /** * 功能:判断字符串是否为日期格式 * * @param strDate * @return */ public static boolean isDate(String strDate) { Pattern pattern = Pattern .compile("^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1-2][0-3]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$"); Matcher m = pattern.matcher(strDate); if (m.matches()) { return true; } else { return false; } } /** * 返回浏览器的IP地址<br/> * 请求有可能经过Apache代理模式,request.getRemoteAddr()返回的不一定是浏览器的IP地址 * * @param request HttpServletRequest实例 * @return 浏览器的IP地址 */ // public final static String getClientIPAddress(HttpServletRequest request) { // String address = request.getHeader("X-Forwarded-For"); // if (address != null) { // int iFirstDot = address.indexOf(','); // if (iFirstDot > 0) { // address = address.substring(0, iFirstDot); // } // if (isIPv4Address(address)) { // return address; // } // } // return request.getRemoteAddr(); // } /** * 是否是ipv4地址 * * @param str 字符串 * @return */ public final static boolean isIPv4Address(String str) { if (str != null) { int dot1 = str.indexOf('.'); if (dot1 <= 0) { return false; } int temp; try { temp = Integer.parseInt(str.substring(0, dot1++)); if (temp < 0 || temp > 255) { return false; } } catch (Exception ex) { return false; } int dot2 = str.indexOf('.', dot1); if (dot2 <= 0) { return false; } try { temp = Integer.parseInt(str.substring(dot1, dot2++)); if (temp < 0 || temp > 255) { return false; } } catch (Exception ex) { return false; } int dot3 = str.indexOf('.', dot2); if (dot3 <= 0) { return false; } try { temp = Integer.parseInt(str.substring(dot2, dot3++)); if (temp < 0 || temp > 255) { return false; } } catch (Exception ex) { return false; } try { temp = Integer.parseInt(str.substring(dot3)); if (temp < 0 || temp > 255) { return false; } } catch (Exception ex) { return false; } return true; } return false; } /** * 两个时间相差的秒数 * * @param begin * @param end * @return */ public static long compareDifferSecond(Date begin, Date end) { if (begin == null || end == null) { return 0; } long second = (end.getTime() - begin.getTime()) / 1000; //转换成秒 return second; } /** * 两个时间相差的小时数 * * @param begin * @param end * @return */ public static long compareDifferHours(Date begin, Date end) { if (null == begin || null == end) { return -1; } long intervalMilli = end.getTime() - begin.getTime(); return intervalMilli / (60 * 60 * 1000); } /** * 将geUserPersonal里的性别1,2转换为字母代号,M、F */ public static String convertSexToWord(String sex) { if ("1".equals(sex)) { return "M"; } else if ("2".equals(sex)) { return "F"; } else { return ""; } } /** * 判断一个对象是否为空 * * @param obj * @return */ public static boolean isEmpty(Object obj) { if (("").equals(obj) || null == obj || "null".equals(obj) || obj.equals(null)) { return true; } return false; } /** * 校验请求分页数据 * * @param requestInfo * @return */ public static boolean verifyReqPageInfo(RequestInfo requestInfo) { Integer currentPage = requestInfo.getCurrentPage(); if (currentPage < 0) { setVerifyPageError("当前页码不能小于零,原因:当前页码为" + currentPage); return false; } Integer pageSize = requestInfo.getPageSize(); if (pageSize <= 0) { setVerifyPageError("设置当前页行数不能小于或等于零,原因:当前页行数为" + pageSize); return false; } return true; } /** * 错误信息 */ private static String errorMsg; public static String getVerifyPageError() { return errorMsg; } public static void setVerifyPageError(String msg) { errorMsg = msg; } /** * 输出详细日志 * * @param anexcepObj * @return */ public static String getExceptionStackTrace(Throwable anexcepObj) { StringWriter sw = null; PrintWriter printWriter = null; try { if (anexcepObj != null) { sw = new StringWriter(); printWriter = new PrintWriter(sw); anexcepObj.printStackTrace(printWriter); printWriter.flush(); sw.flush(); return sw.toString(); } else { return null; } } finally { try { if (sw != null) { sw.close(); } if (printWriter != null) { printWriter.close(); } } catch (IOException e) { e.printStackTrace(); } } } public static final String[] constellationArr = {"水瓶座", "双鱼座", "白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "魔羯座"}; public static final int[] constellationEdgeDay = {20, 19, 21, 21, 21, 22, 23, 23, 23, 23, 22, 22}; /** * 根据出生日期获取星座 * * @return */ public static String getConstellation(Date date) { if (date == null) { return ""; } Calendar cal = Calendar.getInstance(); cal.setTime(date); int month = cal.get(Calendar.MONTH); int day = cal.get(Calendar.DAY_OF_MONTH); if (day < constellationEdgeDay[month]) { month = month - 1; } if (month >= 0) { return constellationArr[month]; } // default to return 魔羯 return constellationArr[11]; } /** * 验证输入的用户名格式是否符合(用户名由6-16位的英文字母、数字或下划线组成,不能以数字开头) * * @param UserName * @return 是否合法 */ public static boolean UserNameFormat(String UserName) { boolean tag = true; final String pattern1 = "^[a-zA-Z_]{1}\\w{5,29}$"; final Pattern pattern = Pattern.compile(pattern1); final Matcher mat = pattern.matcher(UserName); if (!mat.find()) { tag = false; } return tag; } /** * 验证输入的邮箱格式是否符合 * * @param email * @return 是否合法 */ public static boolean emailFormat(String email) { boolean tag = true; final String pattern1 = "^([a-z0-9A-Z]+[-|\\_|\\.]?)+[a-z0-9A-Z]?@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$"; // final String pattern1 = // "^\\w+[-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$"; final Pattern pattern = Pattern.compile(pattern1); final Matcher mat = pattern.matcher(email); if (!mat.find()) { tag = false; } return tag; } /** * 验证身份证 * * @param IDNumber */ public static boolean isIDNumber(String IDNumber) { if (IDNumber.length() == 18) { return validateIdCard18(IDNumber); } else if (IDNumber.length() == 15) { return validateIdCard15(IDNumber); } else { return false; } } /** * 验证18位身份编码是否合法 * * @param idCard 身份编码 * @return 是否合法 */ public static boolean validateIdCard18(String idCard) { boolean bTrue = false; if (idCard.length() == 18) { // 前17位 String code17 = idCard.substring(0, 17); // 第18位 String code18 = idCard.substring(17, 18); if (isNum(code17)) { char[] cArr = code17.toCharArray(); if (cArr != null) { int[] iCard = converCharToInt(cArr); int iSum17 = getPowerSum(iCard); // 获取校验位 String val = getCheckCode18(iSum17); if (val.length() > 0) { if (val.equalsIgnoreCase(code18)) { bTrue = true; } } } } } return bTrue; } /** * 验证15位身份编码是否合法 * * @param idCard 身份编码 * @return 是否合法 */ public static boolean validateIdCard15(String idCard) { if (idCard.length() != 15) { return false; } if (isNum(idCard)) { String birthCode = idCard.substring(6, 12); Date birthDate = null; try { birthDate = new SimpleDateFormat("yy").parse(birthCode.substring(0, 2)); } catch (Exception e) { e.printStackTrace(); } Calendar cal = Calendar.getInstance(); if (birthDate != null) { cal.setTime(birthDate); } if (!valiDate(cal.get(Calendar.YEAR), Integer.valueOf(birthCode.substring(2, 4)), Integer.valueOf(birthCode.substring(4, 6)))) { return false; } } else { return false; } return true; } /** * 数字验证 * * @param val * @return 提取的数字。 */ public static boolean isNum(String val) { return val == null || "".equals(val) ? false : val.matches("^[0-9]*$"); } /** * 验证小于当前日期 是否有效 * * @param iYear 待验证日期(年) * @param iMonth 待验证日期(月 1-12) * @param iDate 待验证日期(日) * @return 是否有效 */ public static boolean valiDate(int iYear, int iMonth, int iDate) { Calendar cal = Calendar.getInstance(); int year = cal.get(Calendar.YEAR); int datePerMonth; if (iYear < MIN || iYear >= year) { return false; } if (iMonth < 1 || iMonth > 12) { return false; } switch (iMonth) { case 4: case 6: case 9: case 11: datePerMonth = 30; break; case 2: boolean dm = ((iYear % 4 == 0 && iYear % 100 != 0) || (iYear % 400 == 0)) && (iYear > MIN && iYear < year); datePerMonth = dm ? 29 : 28; break; default: datePerMonth = 31; } return (iDate >= 1) && (iDate <= datePerMonth); } /** * 将字符数组转换成数字数组 * * @param ca 字符数组 * @return 数字数组 */ public static int[] converCharToInt(char[] ca) { int len = ca.length; int[] iArr = new int[len]; try { for (int i = 0; i < len; i++) { iArr[i] = Integer.parseInt(String.valueOf(ca[i])); } } catch (NumberFormatException e) { e.printStackTrace(); } return iArr; } /** * 将身份证的每位和对应位的加权因子相乘之后,再得到和值 * * @param iArr * @return 身份证编码。 */ public static int getPowerSum(int[] iArr) { int iSum = 0; if (power.length == iArr.length) { for (int i = 0; i < iArr.length; i++) { for (int j = 0; j < power.length; j++) { if (i == j) { iSum = iSum + iArr[i] * power[j]; } } } } return iSum; } /** * 将power和值与11取模获得余数进行校验码判断 * * @param iSum * @return 校验位 */ public static String getCheckCode18(int iSum) { String sCode = ""; switch (iSum % 11) { case 10: sCode = "2"; break; case 9: sCode = "3"; break; case 8: sCode = "4"; break; case 7: sCode = "5"; break; case 6: sCode = "6"; break; case 5: sCode = "7"; break; case 4: sCode = "8"; break; case 3: sCode = "9"; break; case 2: sCode = "x"; break; case 1: sCode = "0"; break; case 0: sCode = "1"; break; } return sCode; } /** * 根据出生日期计算年龄 * * @param birthday * @return */ public static int getAgeByBirth(Date birthday) { int age = 0; try { Calendar now = Calendar.getInstance(); now.setTime(new Date());// 当前时间 Calendar birth = Calendar.getInstance(); birth.setTime(birthday); if (birth.after(now)) {//如果传入的时间,在当前时间的后面,返回0岁 age = 0; } else { age = now.get(Calendar.YEAR) - birth.get(Calendar.YEAR); if (now.get(Calendar.DAY_OF_YEAR) > birth.get(Calendar.DAY_OF_YEAR)) { age += 1; } } return age; } catch (Exception e) {//兼容性更强,异常后返回数据 return 0; } } }