java正则表达式的匹配包括:邮箱,手机,姓名,昵称,身份证号,银行卡号等;生成6位随机数;对url中字符串进行编码和解码;获取客户端ip地址;获取系统当前时间;生成32位编码不含横线;生成MD5编码;通过身份证获取性别;通过身份证获取生日;手机号中间4位替换成星号;邮箱地址加星号;生成随机密码;import com.alibaba.fastjson.JSONObject;import org.apache.commons.beanutils.BeanUtilsBean;import org.apache.commons.beanutils.PropertyUtilsBean;importorg.springframework.util.DigestUtils;import org.springframework.util.StringUtils;import javax.servlet.http.HttpServletRequest;import java.io.;importjava.net.MalformedURLException;import java.net.URL;import java.net.URLDecoder;import java.security.SecureRandom;import java.sql.Timestamp;importjava.text.ParseException;import java.text.SimpleDateFormat;import java.util.;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;importjava.util.regex.Matcher;import java.util.regex.Pattern;import java.util.stream.Stream;/ 通用工具类./public class CommonUtil {private staticSecureRandom random =new SecureRandom();public static final Pattern MAIL_PATTERN = Pattern.compile("^([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})(\]?)$"</span>);<span style="color: green;">public</span> <span style="color: green;">static</span> <span style="color: green;">final</span> Pattern MOBILE_PATTERN = Pattern.compile(<span style="color: green;">"^1[3|4|5|7|8][0-9]\\d{8}$");public static final Pattern NAME_PATTERN = Pattern.compile("^\u4E00-\u9FBF{0,13}[\u4E00-\u9FBF]$"</span>);<span style="color: green;">public</span> <span style="color: green;">static</span> <span style="color: green;">final</span> Pattern NICKNAME_PATTERN = Pattern.compile(<span style="color: green;">"^((?!\\d{5})[\\u4E00-\\u9FBF(.|·)|0-9A-Za-z_]){2,11}$");public static finalPattern PASSWORD_PATTERN = Pattern.compile("^[\s\S]{6,30}$"</span>);<span style="color: green;">public</span> <span style="color: green;">static</span> <span style="color: green;">final</span> Pattern CODE_PATTERN = Pattern.compile(<span style="color: green;">"^0\\d{2,4}$");public static finalPattern POSTCODE_PATTERN = Pattern.compile("^\d{6}$"</span>);<span style="color: green;">public</span> <span style="color: green;">static</span> <span style="color: green;">final</span> Pattern ID_PATTERN = Pattern.compile(<span style="color: green;">"^\\d{6}(\\d{8}|\\d{11})[0-9a-zA-Z]$");public static finalPattern BANK_CARD_PATTERN = Pattern.compile("^\d{16,30}$"</span>);<span style="color: green;">/*** 生成6位随机数字, 用于手机短信验证码.** <span style="color: green;">@return</span> 6位随机数字*/</span><span style="color: green;"><span style="color: green;">public</span> <span style="color: green;">static</span> <span style="color: green;">int</span> <span style="color: green;">random</span><span style="color: green;">()</span> </span>{<span style="color: green;">int</span>x = Math.abs(random.nextInt(<span style="color: green;">899999</span>));<span style="color: green;">return</span> x + <span style="color: green;">100000</span>;}<span style="color: green;">/*** 对url字符串进行编码.** <span style="color: green;">@param</span> url 要编码的url字符串* <span style="color: green;">@return</span>编码后的字符串*/</span><span style="color: green;"><span style="color: green;">public</span> <span style="color: green;">static</span> String <span style="color: green;">urlEncoder</span><span style="color: green;">(String url)</span> </span>{<span style="color: green;">if</span> (StringUtils.isEmpty(url)) {<span style="color: green;">return</span> <span style="color: green;">null</span>;}<span style="color: green;">try</span> {<span style="color: green;">return</span> java.net.URLEncoder.encode(url, <span style="color: green;">"utf-8"</span>);}<span style="color: green;">catch</span> (UnsupportedEncodingException e) {e.printStackTrace();}<span style="color: green;">return</span> <span style="color: green;">null</span>;}<span style="color: green;">/*** 对url字符串进行解码.** <span style="color: green;">@param</span>url 要解码的url字符串*<span style="color: green;">@return</span> 解码后的字符串*/</span><span style="color: green;"><span style="color: green;">public</span> <span style="color: green;">static</span> String <span style="color: green;">urlDecoder</span><span style="color: green;">(String url)</span> </span>{<span style="color: green;">if</span> (StringUtils.isEmpty(url)) {<span style="color: green;">return</span> <span style="color: green;">null</span>;}<span style="color: green;">try</span> {<span style="color: green;">return</span>URLDecoder.decode(url,<span style="color: green;">"UTF-8"</span>);} <span style="color: green;">catch</span> (UnsupportedEncodingException e) {e.printStackTrace();}<span style="color: green;">return</span> <span style="color: green;">null</span>;}<span style="color: green;">/*** 验证字符串是不是邮箱.**<span style="color: green;">@param</span> email 要验证的邮箱* <span style="color: green;">@return</span> 是否正确邮箱*/</span><span style="color: green;"><span style="color: green;">public</span> <span style="color: green;">static</span> <span style="color: green;">boolean</span> <span style="color: green;">validateEmail</span><span style="color: green;">(String email)</span> </span>{<span style="color: green;">if</span> (StringUtils.isEmpty(email)) {<span style="color: green;">return</span> <span style="color: green;">false</span>;}Matcher m = MAIL_PATTERN.matcher(email);<span style="color: green;">return</span> m.matches();}<span style="color: green;">/*** 验证字符串是不是手机号.** <span style="color: green;">@param</span>mobile 要验证的手机号*<span style="color: green;">@return</span> 是否正确手机号*/</span><span style="color: green;"><span style="color: green;">public</span> <span style="color: green;">static</span> <span style="color: green;">boolean</span> <span style="color: green;">validateMobile</span><span style="color: green;">(String mobile)</span> </span>{<span style="color: green;">if</span> (StringUtils.isEmpty(mobile)) {<span style="color: green;">return</span> <span style="color: green;">false</span>;}Matcher m = MOBILE_PATTERN.matcher(mobile);<span style="color: green;">return</span> m.matches();}<span style="color: green;">/*** 验证身份证是否有效.** <span style="color: green;">@param</span> idCardNumber 要验证的身份证* <span style="color: green;">@return</span>是否正确身份证*/</span><span style="color: green;"><span style="color: green;">public</span> <span style="color: green;">static</span> <span style="color: green;">boolean</span> <span style="color: green;">validateId</span><span style="color: green;">(String idCardNumber)</span> </span>{<span style="color: green;">if</span> (StringUtils.isEmpty(idCardNumber)) {<span style="color: green;">return</span> <span style="color: green;">false</span>;}Matcher m = ID_PATTERN.matcher(idCardNumber);<span style="color: green;">return</span> m.matches() &amp;&amp; IdCardUtils.validateCard(idCardNumber);}<span style="color: green;">/*** 验证姓名是否有效.** <span style="color: green;">@param</span> name 要验证的姓名* <span style="color: green;">@return</span> 是否正确姓名*/</span><span style="color: green;"><span style="color: green;">public</span> <span style="color: green;">static</span> <span style="color: green;">boolean</span> <span style="color: green;">validateName</span><span style="color: green;">(String name)</span> </span>{<span style="color: green;">if</span> (StringUtils.isEmpty(name) || name.replaceAll(<span style="color: green;">"[^.·]"</span>, <span style="color: green;">""</span>).length() &gt; <span style="color: green;">1</span>) {<span style="color: green;">return</span> <span style="color: green;">false</span>;}Matcher m = NAME_PATTERN.matcher(name);<span style="color: green;">return</span> m.matches();}<span style="color: green;">/*** 验证昵称是否有效.** <span style="color: green;">@param</span>nickname 要验证的昵称*<span style="color: green;">@return</span> 是否正确昵称*/</span><span style="color: green;"><span style="color: green;">public</span> <span style="color: green;">static</span> <span style="color: green;">boolean</span> <span style="color: green;">validateNickname</span><span style="color: green;">(String nickname)</span> </span>{<span style="color: green;">//规则 不能包含5个数字 允许中英文和数字 2-11位</span><span style="color: green;">if</span>(StringUtils.isEmpty(nickname) || nickname.replaceAll(<span style="color: green;">"[^0-9]"</span>, <span style="color: green;">""</span>).length() &gt; <span style="color: green;">4</span>) {<span style="color: green;">return</span> <span style="color: green;">false</span>;}Matcher m = NICKNAME_PATTERN.matcher(nickname);<span style="color: green;">return</span> m.matches();}<span style="color: green;">/*** 验证密码格式是否有效.**<span style="color: green;">@param</span> password 要验证的密码* <span style="color: green;">@return</span> 是否正确密码格式*/</span><span style="color: green;"><span style="color: green;">public</span> <span style="color: green;">static</span> <span style="color: green;">boolean</span> <span style="color: green;">validatePassword</span><span style="color: green;">(String password)</span> </span>{<span style="color: green;">if</span>(StringUtils.isEmpty(password)) {<span style="color: green;">return</span> <span style="color: green;">false</span>;}Matcher m = PASSWORD_PATTERN.matcher(password);<span style="color: green;">return</span> m.matches();}<span style="color: green;">/*** 验证区号是否有效.**<span style="color: green;">@param</span> code 要验证的区号* <span style="color: green;">@return</span> 是否正确身份证*/</span><span style="color: green;"><span style="color: green;">public</span> <span style="color: green;">static</span> <span style="color: green;">boolean</span> <span style="color: green;">validateCode</span><span style="color: green;">(String code)</span> </span>{<span style="color: green;">if</span> (StringUtils.isEmpty(code)) {<span style="color: green;">return</span> <span style="color: green;">false</span>;}Matcher m = CODE_PATTERN.matcher(code);<span style="color: green;">return</span> m.matches();}<span style="color: green;">/*** 验证邮政编码是否有效.** <span style="color: green;">@param</span>postcode 要验证的邮政编码*<span style="color: green;">@return</span> 是否正确邮政编码*/</span><span style="color: green;"><span style="color: green;">public</span> <span style="color: green;">static</span> <span style="color: green;">boolean</span> <span style="color: green;">validatePostcode</span><span style="color: green;">(String postcode)</span> </span>{<span style="color: green;">if</span> (StringUtils.isEmpty(postcode)) {<span style="color: green;">return</span> <span style="color: green;">false</span>;}Matcher m = POSTCODE_PATTERN.matcher(postcode);<span style="color: green;">return</span> m.matches();}<span style="color: green;">/*** 验证银行卡是否有效.** <span style="color: green;">@param</span> bankCardNumber 要验证的银行卡号* <span style="color: green;">@return</span> 是否正确银行卡号*/</span><span style="color: green;"><span style="color: green;">public</span> <span style="color: green;">static</span> <span style="color: green;">boolean</span> <span style="color: green;">validateBankCardNumber</span><span style="color: green;">(String bankCardNumber)</span> </span>{<span style="color: green;">if</span> (StringUtils.isEmpty(bankCardNumber)) {<span style="color: green;">return</span> <span style="color: green;">false</span>;}Matcher m = BANK_CARD_PATTERN.matcher(bankCardNumber);<span style="color: green;">return</span> m.matches();}<span style="color: green;">/*** 获取客户端IP地址.** <span style="color: green;">@param</span>request request请求*<span style="color: green;">@return</span> ip地址*/</span><span style="color: green;"><span style="color: green;">public</span> <span style="color: green;">static</span> String <span style="color: green;">getIpAddr</span><span style="color: green;">(HttpServletRequest request)</span> </span>{String ip = request.getHeader(<span style="color: green;">"Cdn-Src-Ip"</span>);<span style="color: green;">if</span> (ip == <span style="color: green;">null</span>|| ip.length() ==<span style="color: green;">0</span> || <span style="color: green;">"unknown"</span>.equalsIgnoreCase(ip)) {ip = request.getHeader(<span style="color: green;">"X-Forwarded-For"</span>);}<span style="color: green;">if</span> (ip == <span style="color: green;">null</span> || ip.length() == <span style="color: green;">0</span> || <span style="color: green;">"unknown"</span>.equalsIgnoreCase(ip)) {ip = request.getRemoteAddr();}<span style="color: green;">if</span> (StringUtils.hasText(ip)) {<span style="color: green;">return</span> StringUtils.tokenizeToStringArray(ip, <span style="color: green;">","</span>)[<span style="color: green;">0</span>];}<span style="color: green;">return</span> <span style="color: green;">""</span>;}<span style="color: green;">/*** 获取当前系统时间,以java.sql.Timestamp类型返回.** <span style="color: green;">@return</span> 当前时间*/</span><span style="color: green;"><span style="color: green;">public</span> <span style="color: green;">static</span> Timestamp <span style="color: green;">getTimestamp</span><span style="color: green;">()</span> </span>{Timestamp d = <span style="color: green;">new</span> Timestamp(System.currentTimeMillis());<span style="color: green;">return</span> d;}<span style="color: green;">/*** 生成32位编码,不含横线** <span style="color: green;">@return</span> uuid串*/</span><span style="color: green;"><span style="color: green;">public</span> <span style="color: green;">static</span> String <span style="color: green;">getUUID</span><span style="color: green;">()</span> </span>{String uuid = UUID.randomUUID().toString().trim().replaceAll(<span style="color: green;">"-"</span>, <span style="color: green;">""</span>);<span style="color: green;">return</span> uuid.toUpperCase();}<span style="color: green;">/*** 生成MD5编码** <span style="color: green;">@param</span> data 要编码的字符串* <span style="color: green;">@return</span> 加密后的字符串*/</span><span style="color: green;"><span style="color: green;">public</span> <span style="color: green;">static</span> String <span style="color: green;">md5</span><span style="color: green;">(String data)</span> </span>{<span style="color: green;">return</span> md5(data, <span style="color: green;">1</span>);}<span style="color: green;">/*** 生成MD5编码** <span style="color: green;">@param</span> data 要编码的字符串* <span style="color: green;">@param</span> time 加密次数* <span style="color: green;">@return</span> 加密后的字符串*/</span><span style="color: green;"><span style="color: green;">public</span> <span style="color: green;">static</span> String <span style="color: green;">md5</span><span style="color: green;">(String data, <span style="color: green;">int</span> time)</span> </span>{<span style="color: green;">byte</span>[] bytes = data == <span style="color: green;">null</span> ? <span style="color: green;">new</span> <span style="color: green;">byte</span>[<span style="color: green;">0</span>] : data.getBytes(Constants.UTF8);<span style="color: green;">while</span> (time-- &gt; <span style="color: green;">1</span>) {bytes = DigestUtils.md5Digest(bytes);}<span style="color: green;">return</span>DigestUtils.md5DigestAsHex(bytes).toUpperCase();}<span style="color: green;">/*** 空字符串转为null** <span style="color: green;">@param</span> object 要规则化的对象* <span style="color: green;">@param</span> &lt;T&gt; 对象类型* <span style="color: green;">@return</span> 规则化后的对象*/</span><span style="color: green;">public</span> <span style="color: green;">static</span> &lt;T&gt; <span style="color: green;">T <span style="color: green;">normalizeBlankStringFields</span><span style="color: green;">(T object)</span> </span>{<span style="color: green;">if</span> (object == <span style="color: green;">null</span>) <span style="color: green;">return</span> <span style="color: green;">null</span>;PropertyUtilsBean utils = BeanUtilsBean.getInstance().getPropertyUtils();Stream.of(utils.getPropertyDescriptors(object)).forEach(pd -&gt; {<span style="color: green;">try</span> {Object value = utils.getNestedProperty(object, pd.getName());<span style="color: green;">if</span> (value == <span style="color: green;">null</span>) <span style="color: green;">return</span>;<span style="color: green;">if</span> (!StringUtils.hasText(value.toString())) {utils.setNestedProperty(object, pd.getName(), <span style="color: green;">null</span>);}} <span style="color: green;">catch</span>(Throwable e) {}});<span style="color: green;">return</span> object;}<span style="color: green;">/*** 通过身份证获取性别** <span style="color: green;">@param</span> idNumber 身份证号* <span style="color: green;">@return</span> 返回性别, 0 保密 , 1 男 2 女*/</span><span style="color: green;"><span style="color: green;">public</span> <span style="color: green;">static</span>Integer<span style="color: green;">getGenderByIdNumber</span><span style="color: green;">(String idNumber)</span> </span>{<span style="color: green;">int</span> gender = <span style="color: green;">0</span>;<span style="color: green;">if</span> (idNumber.length() == <span style="color: green;">15</span>) {gender = Integer.parseInt(String.valueOf(idNumber.charAt(<span style="color: green;">14</span>))) %<span style="color: green;">2</span> == <span style="color: green;">0</span> ? <span style="color: green;">2</span> : <span style="color: green;">1</span>;} <span style="color: green;">else</span> <span style="color: green;">if</span> (idNumber.length() == <span style="color: green;">18</span>) {gender = Integer.parseInt(String.valueOf(idNumber.charAt(<span style="color: green;">16</span>))) % <span style="color: green;">2</span> == <span style="color: green;">0</span> ? <span style="color: green;">2</span> : <span style="color: green;">1</span>;}<span style="color: green;">return</span>gender;}<span style="color: green;">/*** 通过身份证获取生日** <span style="color: green;">@param</span> idNumber 身份证号* <span style="color: green;">@return</span> 返回生日, 格式为 yyyy-MM-dd 的字符串*/</span><span style="color: green;"><span style="color: green;">public</span> <span style="color: green;">static</span> String <span style="color: green;">getBirthdayByIdNumber</span><span style="color: green;">(String idNumber)</span> </span>{String birthday = <span style="color: green;">""</span>;<span style="color: green;">if</span> (idNumber.length() == <span style="color: green;">15</span>) {birthday = <span style="color: green;">"19"</span> + idNumber.substring(<span style="color: green;">6</span>, <span style="color: green;">8</span>) + <span style="color: green;">"-"</span> + idNumber.substring(<span style="color: green;">8</span>, <span style="color: green;">10</span>) + <span style="color: green;">"-"</span> + idNumber.substring(<span style="color: green;">10</span>, <span style="color: green;">12</span>);} <span style="color: green;">else</span> <span style="color: green;">if</span> (idNumber.length() == <span style="color: green;">18</span>) {birthday = idNumber.substring(<span style="color: green;">6</span>, <span style="color: green;">10</span>) + <span style="color: green;">"-"</span> + idNumber.substring(<span style="color: green;">10</span>, <span style="color: green;">12</span>) + <span style="color: green;">"-"</span> + idNumber.substring(<span style="color: green;">12</span>, <span style="color: green;">14</span>);}<span style="color: green;">return</span> birthday;}<span style="color: green;">/*** 通过身份证获取年龄** <span style="color: green;">@param</span> idNumber 身份证号* <span style="color: green;">@return</span>返回年龄*/</span><span style="color: green;"><span style="color: green;">public</span> <span style="color: green;">static</span> Integer <span style="color: green;">getAgeByIdNumber</span><span style="color: green;">(String idNumber)</span> </span>{String birthString = getBirthdayByIdNumber(idNumber);<span style="color: green;">if</span>(StringUtils.isEmpty(birthString)) {<span style="color: green;">return</span> <span style="color: green;">0</span>;}<span style="color: green;">return</span> getAgeByBirthString(birthString);}<span style="color: green;">/*** 通过身份证获取年龄** <span style="color: green;">@param</span> idNumber 身份证号* <span style="color: green;">@param</span>isNominalAge 是否按元旦算年龄,过了1月1日加一岁 true : 是 false : 否*<span style="color: green;">@return</span> 返回年龄*/</span><span style="color: green;"><span style="color: green;">public</span> <span style="color: green;">static</span> Integer <span style="color: green;">getAgeByIdNumber</span><span style="color: green;">(String idNumber, <span style="color: green;">boolean</span> isNominalAge)</span> </span>{String birthString = getBirthdayByIdNumber(idNumber);<span style="color: green;">if</span> (StringUtils.isEmpty(birthString)) {<span style="color: green;">return</span> <span style="color: green;">0</span>;}<span style="color: green;">return</span>getAgeByBirthString(birthString, isNominalAge);}<span style="color: green;">/*** 通过生日日期获取年龄** <span style="color: green;">@param</span> birthDate 生日日期* <span style="color: green;">@return</span> 返回年龄*/</span><span style="color: green;"><span style="color: green;">public</span> <span style="color: green;">static</span> Integer <span style="color: green;">getAgeByBirthDate</span><span style="color: green;">(Date birthDate)</span> </span>{<span style="color: green;">return</span> getAgeByBirthString(<span style="color: green;">new</span> SimpleDateFormat(<span style="color: green;">"yyyy-MM-dd"</span>).format(birthDate));}<span style="color: green;">/*** 通过生日字符串获取年龄** <span style="color: green;">@param</span> birthString 生日字符串* <span style="color: green;">@return</span> 返回年龄*/</span><span style="color: green;"><span style="color: green;">public</span> <span style="color: green;">static</span> Integer <span style="color: green;">getAgeByBirthString</span><span style="color: green;">(String birthString)</span> </span>{<span style="color: green;">return</span> getAgeByBirthString(birthString, <span style="color: green;">"yyyy-MM-dd"</span>);}<span style="color: green;">/*** 通过生日字符串获取年龄** <span style="color: green;">@param</span> birthString 生日字符串* <span style="color: green;">@param</span>isNominalAge 是否按元旦算年龄,过了1月1日加一岁 true : 是 false : 否*<span style="color: green;">@return</span> 返回年龄*/</span><span style="color: green;"><span style="color: green;">public</span> <span style="color: green;">static</span> Integer <span style="color: green;">getAgeByBirthString</span><span style="color: green;">(String birthString, <span style="color: green;">boolean</span> isNominalAge)</span> </span>{<span style="color: green;">return</span>getAgeByBirthString(birthString,<span style="color: green;">"yyyy-MM-dd"</span>, isNominalAge);}<span style="color: green;">/*** 通过生日字符串获取年龄** <span style="color: green;">@param</span> birthString 生日字符串* <span style="color: green;">@param</span>format 日期字符串格式,为空则默认"yyyy-MM-dd"*<span style="color: green;">@return</span> 返回年龄*/</span><span style="color: green;"><span style="color: green;">public</span> <span style="color: green;">static</span> Integer <span style="color: green;">getAgeByBirthString</span><span style="color: green;">(String birthString, String format)</span> </span>{<span style="color: green;">return</span> getAgeByBirthString(birthString, <span style="color: green;">"yyyy-MM-dd"</span>, <span style="color: green;">false</span>);}<span style="color: green;">/*** 通过生日字符串获取年龄** <span style="color: green;">@param</span> birthString 生日字符串* <span style="color: green;">@param</span> format 日期字符串格式,为空则默认"yyyy-MM-dd"* <span style="color: green;">@param</span> isNominalAge 是否按元旦算年龄,过了1月1日加一岁 true : 是 false : 否* <span style="color: green;">@return</span> 返回年龄*/</span><span style="color: green;"><span style="color: green;">public</span> <span style="color: green;">static</span> Integer <span style="color: green;">getAgeByBirthString</span><span style="color: green;">(String birthString, String format,<span style="color: green;">boolean</span> isNominalAge)</span> </span>{<span style="color: green;">int</span> age = <span style="color: green;">0</span>;<span style="color: green;">if</span> (StringUtils.isEmpty(birthString)) {<span style="color: green;">return</span> age;}<span style="color: green;">if</span> (StringUtils.isEmpty(format)) {format = <span style="color: green;">"yyyy-MM-dd"</span>;}<span style="color: green;">try</span> {Calendar birthday = Calendar.getInstance();Calendar today = Calendar.getInstance();SimpleDateFormat sdf = <span style="color: green;">new</span>SimpleDateFormat(format);birthday.setTime(sdf.parse(birthString));age = today.get(Calendar.YEAR) - birthday.get(Calendar.YEAR);<span style="color: green;">if</span> (!isNominalAge) {<span style="color: green;">if</span>(today.get(Calendar.MONTH) &lt; birthday.get(Calendar.MONTH) ||(today.get(Calendar.MONTH) == birthday.get(Calendar.MONTH) &amp;&amp;today.get(Calendar.DAY_OF_MONTH) &lt; birthday.get(Calendar.DAY_OF_MONTH))) {age = age -<span style="color: green;">1</span>;}}} <span style="color: green;">catch</span> (ParseException e) {e.printStackTrace();}<span style="color: green;">return</span>age;}<span style="color: green;">/*** 手机号中间四位替换成星号** <span style="color: green;">@param</span> mobile* <span style="color: green;">@return</span>*/</span><span style="color: green;"><span style="color: green;">public</span> <span style="color: green;">static</span> String <span style="color: green;">maskMobile</span><span style="color: green;">(String mobile)</span> </span>{<span style="color: green;">if</span>(validateMobile(mobile)) {<span style="color: green;">return</span> mobile.replaceAll(<span style="color: green;">"(\\d{3})\\d{4}(\\d{4})"</span>, <span style="color: green;">"$1$2"</span>);}<span style="color: green;">return</span> mobile;}<span style="color: green;">/*** 手机号中间四位自定义替换** <span style="color: green;">@param</span>mobile*<span style="color: green;">@param</span> transCode 中间四位目标值 如GXJF 将136GXJF1111* <span style="color: green;">@return</span>*/</span><span style="color: green;"><span style="color: green;">public</span> <span style="color: green;">static</span> String <span style="color: green;">maskMobile</span><span style="color: green;">(String mobile, String transCode)</span> </span>{<span style="color: green;">if</span>(validateMobile(mobile)) {transCode = StringUtils.isEmpty(transCode) ?<span style="color: green;">"****"</span> : transCode;<span style="color: green;">return</span> mobile.replaceAll(<span style="color: green;">"(\\d{3})\\d{4}(\\d{4})"</span>, String.format(<span style="color: green;">"$1%s$2"</span>, transCode));}<span style="color: green;">return</span> mobile;}<span style="color: green;">/*** 邮箱地址加星号** <span style="color: green;">@param</span> email* <span style="color: green;">@return</span>*/</span><span style="color: green;"><span style="color: green;">public</span> <span style="color: green;">static</span> String <span style="color: green;">maskEmail</span><span style="color: green;">(String email)</span> </span>{<span style="color: green;">if</span> (validateEmail(email)) {String userName = email.substring(<span style="color: green;">0</span>, email.indexOf(<span style="color: green;">"@"</span>));<span style="color: green;">int</span> len = userName.length();<span style="color: green;">if</span> (len &gt;= <span style="color: green;">5</span>) {<span style="color: green;">int</span> total = len - <span style="color: green;">3</span>;<span style="color: green;">int</span> half = total / <span style="color: green;">2</span>;<span style="color: green;">int</span> start = half;<span style="color: green;">int</span> end = len - half;<span style="color: green;">if</span> (total % <span style="color: green;">2</span> != <span style="color: green;">0</span>) {end = end - <span style="color: green;">1</span>;}StringBuilder sb = <span style="color: green;">new</span> StringBuilder(email);<span style="color: green;">for</span> (<span style="color: green;">int</span>i = start; i &lt; end; i++) {sb.setCharAt(i,<span style="color: green;">*</span>);}<span style="color: green;">return</span> sb.toString();}}<span style="color: green;">return</span> email;}<span style="color: green;">/*** 账号中间四位自定义替换** <span style="color: green;">@param</span> account* <span style="color: green;">@return</span>*/</span><span style="color: green;"><span style="color: green;">public</span> <span style="color: green;">static</span> String <span style="color: green;">maskTradeAccount</span><span style="color: green;">(String account)</span> </span>{<span style="color: green;">return</span> account.replaceAll(<span style="color: green;">"(\\d{7})\\d*(\\d{4})"</span>, <span style="color: green;">"$1$2");}/ 读取jsonResult的value中的某个属性值@param jsonResult @param attrName @return/public static String readJSONResultValueAttr(JSONResult jsonResult, String attrName) {String ret = "";try{ret = ((JSONObject) jsonResult.getValue()).get(attrName).toString();}catch (Exception e) {//e.printStackTrace();}return ret;}/ 验证是否为日期 @param date @return/public static boolean validateDate(String date) {boolean convertSuccess = true;SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");try {format.setLenient(false);format.parse(date);}catch (ParseException e) {convertSuccess = false;}return convertSuccess;}/ 获取时间戳,作为递增的ID/private static final Lock lock = new ReentrantLock(); //锁对象public static long getUniqueLong() {long l;lock.lock();try{l = System.currentTimeMillis();}finally {lock.unlock();}return l;}/ 解析出url参数中的键值对 如 "index.jsp?Action=del&id=123",解析出Action:del,id:123存入map中@param URL url地址 @return url请求参数部分/public static String getUrlParams(String URL, String key) {Map<String, String> mapRequest =new HashMap<String, String>();String[] arrSplit = null;String strUrlParam = null;java.net.URL aURL = null;try {aURL = new URL(URL);strUrlParam = aURL.getQuery();} catch (MalformedURLException e) {e.printStackTrace();}if (strUrlParam == null) {return "";}arrSplit = strUrlParam.split("[&]");for (String strSplit : arrSplit) {String[] arrSplitEqual = null;arrSplitEqual = strSplit.split("[=]");if (arrSplitEqual.length > 1) {mapRequest.put(arrSplitEqual[0], arrSplitEqual[1]);} else {if (!StringUtils.isEmpty(arrSplitEqual[0])) {mapRequest.put(arrSplitEqual[0], "");}}}if (mapRequest.containsKey(key)) {try {return URLDecoder.decode(mapRequest.get(key), "UTF-8");} catch (UnsupportedEncodingException e) {e.printStackTrace();} catch (Exception e) {}}return "";}/ 生成随机密码@param pwd_len 生成的密码的总长度 @return 密码的字符串/public static String genRandomNum(int pwd_len) {// 35是因为数组是从0开始的,26个字母+10个数字final intmaxNum =36;int i; // 生成的随机数int count = 0; // 生成的密码的长度char[] str = {a, b, c, d, e, f, g, h, i, j, k,l, m, n, o, p, q, r, s, t, u, v, w,x, y, z, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};StringBuffer pwd =new StringBuffer("");Random r = new Random();while (count < pwd_len) {// 生成随机数,取绝对值,防止生成负数,i = Math.abs(r.nextInt(maxNum)); // 生成的数最大为36-1if (i >= 0&& i < str.length) {pwd.append(str[i]);count++;}}return pwd.toString();}}

分类: 百科知识 标签: 暂无标签

评论

暂无评论数据

暂无评论数据

目录