记得上下班打卡 | git大法好,push需谨慎

Commit 7fe9b9d3 authored by 胡佳晨's avatar 胡佳晨

生成取货码

parent 357923cb
...@@ -69,11 +69,27 @@ public class IDGenerator { ...@@ -69,11 +69,27 @@ public class IDGenerator {
} }
public static String storeRefundCode(String orderMasterCode) { public static String storeRefundCode(String orderMasterCode) {
return orderMasterCode.concat("R").concat(RandomUtil.getRandomInt(0, 50) + ""); return orderMasterCode.concat("R").concat(RandomUtil.getRandomInt(0, 99) + "");
} }
public static String getWriteOffCode() { public static String getWriteOffCode() {
return ""; LocalDateTime now = LocalDateTime.now();
String year = (now.getYear()+"").substring(2);
String day = now.getDayOfYear()+"";
String hour = now.getHour()+"";
String sec = now.getSecond()+"";
String random = RandomUtil.getRandomInt(0,100)+"";
if(day.length()==1){
day="0"+day;
}
if(hour.length()==1){
hour="0"+hour;
}
if(sec.length()==1){
sec="0"+sec;
}
String code = year+day+hour+sec+random;
return StringUtil.switchPosition(StringUtil.switchPosition(code,1,4),3,6);
} }
/** /**
...@@ -123,12 +139,6 @@ public class IDGenerator { ...@@ -123,12 +139,6 @@ public class IDGenerator {
} }
public static void main(String[] args) { public static void main(String[] args) {
System.out.println("" + IDGenerator.payCode()); System.out.println(getWriteOffCode());
System.out.println("" + IDGenerator.refundCode());
System.out.println("nextTimeId===" + IDGenerator.nextTimeId());
System.out.println("nextMilliId===" + IDGenerator.nextMilliId());
System.out.println("nextMilliId2===" + IDGenerator.nextMilliId2());
System.out.println("nextSnowId===" + IDGenerator.nextSnowId());
System.out.println("get32UUID===" + IDGenerator.get32UUID());
} }
} }
...@@ -14,446 +14,472 @@ import java.util.Random; ...@@ -14,446 +14,472 @@ import java.util.Random;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.IntStream; import java.util.stream.IntStream;
/** /**
* 字符串工具类 * 字符串工具类
*
* @author <a href="kowlone2006@163.com">kowlone</a> * @author <a href="kowlone2006@163.com">kowlone</a>
* @version 1.0 2012-4-20 上午2:13:53 * @version 1.0 2012-4-20 上午2:13:53
*/ */
public abstract class StringUtil { public abstract class StringUtil {
/** 随机数对象 */ /**
private static final Random random = new Random(); * 随机数对象
/** 数字与字母字典 */ */
private static final char[] LETTER_AND_DIGIT = ("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray(); private static final Random random = new Random();
/** 数字与字母字典长度 */ /**
private static final int LETTER_AND_DIGIT_LENGTH = LETTER_AND_DIGIT.length; * 数字与字母字典
/** 使用Log4j2的消息格式化工厂 */ */
private static final char[] LETTER_AND_DIGIT = ("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
/**
/** * 数字与字母字典长度
* 检测字符串是否为空字符串 */
* 字符串为空的标准:null或全部由空字符组成的字符串 private static final int LETTER_AND_DIGIT_LENGTH = LETTER_AND_DIGIT.length;
* @param str 待检测字符串 /** 使用Log4j2的消息格式化工厂 */
* @return
* <li>true:字符串是空字符串</li>
* <li>false:字符串不是空字符串</li> /**
*/ * 检测字符串是否为空字符串
public static boolean isEmpty(String str) { * 字符串为空的标准:null或全部由空字符组成的字符串
return (str == null || str.trim().length() == 0); *
} * @param str 待检测字符串
* @return <li>true:字符串是空字符串</li>
* <li>false:字符串不是空字符串</li>
/** */
* 检测字符串是否为空字符串 public static boolean isEmpty(String str) {
* 字符串为空的标准:null或全部由空字符组成的字符串 return (str == null || str.trim().length() == 0);
* @param obj 待检测字符串 }
* @return
* <li>true:字符串是空字符串</li>
* <li>false:字符串不是空字符串</li> /**
*/ * 检测字符串是否为空字符串
public static boolean isEmpty(Object obj) { * 字符串为空的标准:null或全部由空字符组成的字符串
*
return (obj == null || obj.toString().trim().length() == 0); * @param obj 待检测字符串
* @return <li>true:字符串是空字符串</li>
} * <li>false:字符串不是空字符串</li>
*/
/** public static boolean isEmpty(Object obj) {
* 检测字符串是否不为空字符串
* 字符串为空的标准:null或全部由空字符组成的字符串 return (obj == null || obj.toString().trim().length() == 0);
* @param str 待检测字符串
* @return }
* <li>true:字符串不是空字符串</li>
* <li>false:字符串是空字符串</li> /**
* @see #isEmpty(String) * 检测字符串是否不为空字符串
*/ * 字符串为空的标准:null或全部由空字符组成的字符串
public static boolean isNotEmpty(String str) { *
return !isEmpty(str); * @param str 待检测字符串
} * @return <li>true:字符串不是空字符串</li>
* <li>false:字符串是空字符串</li>
/** * @see #isEmpty(String)
* 将对象转换为字符串 */
* @param input 待转换对象 public static boolean isNotEmpty(String str) {
* @return 转换后的字符串 return !isEmpty(str);
* @see #getString(Object, String) }
* @see #getString(String)
* @see #getString(String, String) /**
* @see CommonConst#DFT_STRING_VAL * 将对象转换为字符串
*/ *
public static String getString(Object input) { * @param input 待转换对象
return getString(input, CommonConst.DFT_STRING_VAL); * @return 转换后的字符串
} * @see #getString(Object, String)
* @see #getString(String)
/** * @see #getString(String, String)
* 将对象转换为字符串 * @see CommonConst#DFT_STRING_VAL
* @param input 待转换对象 */
* @param defVal 对象转换为空字符串时的默认返回值 public static String getString(Object input) {
* @return 转换后的字符串 return getString(input, CommonConst.DFT_STRING_VAL);
* @see #getString(String) }
* @see #getString(String, String)
*/ /**
public static String getString(Object input, String defVal) { * 将对象转换为字符串
return (input == null) ? defVal : getString(input.toString(), defVal); *
} * @param input 待转换对象
* @param defVal 对象转换为空字符串时的默认返回值
/** * @return 转换后的字符串
* 转换字符串 * @see #getString(String)
* @param input 待转换字符串 * @see #getString(String, String)
* @return 转换后的字符串 */
* @see #getString(String, String) public static String getString(Object input, String defVal) {
*/ return (input == null) ? defVal : getString(input.toString(), defVal);
public static String getString(String input) { }
return getString(input, CommonConst.DFT_STRING_VAL);
} /**
* 转换字符串
/** *
* 转换字符串 * @param input 待转换字符串
* @param input 待转换字符串 * @return 转换后的字符串
* @param defVal 默认转换值 * @see #getString(String, String)
* @return 转换后的字符串 */
* <li>字符串为null或全部由空白字符组成的字符串时,返回defVal参数指定的值</li> public static String getString(String input) {
* <li>其他情况,返回去掉字符串两端空白字符后的字符串</li> return getString(input, CommonConst.DFT_STRING_VAL);
*/ }
public static String getString(String input, String defVal) {
return (isEmpty(input)) ? defVal : input.trim(); /**
} * 转换字符串
*
/** * @param input 待转换字符串
* 生成固定长度的随机字符串 * @param defVal 默认转换值
* @param len 随机字符串长度 * @return 转换后的字符串
* @return 生成的随机字符串 * <li>字符串为null或全部由空白字符组成的字符串时,返回defVal参数指定的值</li>
*/ * <li>其他情况,返回去掉字符串两端空白字符后的字符串</li>
public static String getRandomString(final int len) { */
if (len < 1) { public static String getString(String input, String defVal) {
return (isEmpty(input)) ? defVal : input.trim();
}
/**
* 生成固定长度的随机字符串
*
* @param len 随机字符串长度
* @return 生成的随机字符串
*/
public static String getRandomString(final int len) {
if (len < 1) {
return ""; return "";
} }
StringBuilder sb = new StringBuilder(len); StringBuilder sb = new StringBuilder(len);
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
sb.append(LETTER_AND_DIGIT[random.nextInt(LETTER_AND_DIGIT_LENGTH)]); sb.append(LETTER_AND_DIGIT[random.nextInt(LETTER_AND_DIGIT_LENGTH)]);
} }
return sb.toString(); return sb.toString();
} }
/** /**
* 生成固定长度的随机字符串 * 生成固定长度的随机字符串
* @param len 随机字符串长度 *
* @param dictionary 字符串字典 * @param len 随机字符串长度
* @return 生成的随机字符串 * @param dictionary 字符串字典
*/ * @return 生成的随机字符串
public static String getRandomString(final int len, char[] dictionary) { */
if (len < 1) { public static String getRandomString(final int len, char[] dictionary) {
if (len < 1) {
return ""; return "";
} }
StringBuilder sb = new StringBuilder(len); StringBuilder sb = new StringBuilder(len);
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
sb.append(dictionary[random.nextInt(dictionary.length)]); sb.append(dictionary[random.nextInt(dictionary.length)]);
} }
return sb.toString(); return sb.toString();
} }
/** /**
* 创建一个新的字符串 * 创建一个新的字符串
* @param bytes 字符串内容字节数组(UTF-8编码) *
* @return 新创建的字符串 * @param bytes 字符串内容字节数组(UTF-8编码)
* @see #newString(byte[], String) * @return 新创建的字符串
*/ * @see #newString(byte[], String)
public static String newString(byte[] bytes) { */
return newString(bytes, CommonConst.DFT_CHARSET); public static String newString(byte[] bytes) {
} return newString(bytes, CommonConst.DFT_CHARSET);
}
/**
* 创建一个新的字符串 /**
* @param bytes 字符串内容字节数组 * 创建一个新的字符串
* @param charset 字符串字节编码 *
* @return 新创建的字符串 * @param bytes 字符串内容字节数组
*/ * @param charset 字符串字节编码
public static String newString(byte[] bytes, String charset) { * @return 新创建的字符串
try { */
return new String(bytes, charset); public static String newString(byte[] bytes, String charset) {
} catch (UnsupportedEncodingException e) { try {
throw new RuntimeException("不支持的字符集:" + charset, e); return new String(bytes, charset);
} } catch (UnsupportedEncodingException e) {
} throw new RuntimeException("不支持的字符集:" + charset, e);
}
/** }
* 取得字符串字节数组
* @param str 字符串 /**
* @return 字符串内容字节数组 * 取得字符串字节数组
* @see #getBytes(String, String) *
*/ * @param str 字符串
public static byte[] getBytes(String str) { * @return 字符串内容字节数组
return getBytes(str, CommonConst.DFT_CHARSET); * @see #getBytes(String, String)
} */
public static byte[] getBytes(String str) {
/** return getBytes(str, CommonConst.DFT_CHARSET);
* 取得字符串字节数组 }
* @param str 字符串
* @param charset 字符串字节编码 /**
* @return 字符串内容字节数组 * 取得字符串字节数组
*/ *
public static byte[] getBytes(String str, String charset) { * @param str 字符串
if (str == null) { * @param charset 字符串字节编码
* @return 字符串内容字节数组
*/
public static byte[] getBytes(String str, String charset) {
if (str == null) {
return null; return null;
} }
try { try {
return str.getBytes(charset); return str.getBytes(charset);
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
throw new RuntimeException("不支持的字符集:" + charset, e); throw new RuntimeException("不支持的字符集:" + charset, e);
} }
} }
/** /**
* 从右侧开始截取固定长度的字符串 * 从右侧开始截取固定长度的字符串
* @param str 待截取字符串 *
* @param length 截取的长度 * @param str 待截取字符串
* @return * @param length 截取的长度
* <li>null:字符串为空或字符串长度小于截取的长度</li> * @return <li>null:字符串为空或字符串长度小于截取的长度</li>
* <li>非null:截取字符串后的结果</li> * <li>非null:截取字符串后的结果</li>
*/ */
public static String right(String str, int length) { public static String right(String str, int length) {
return (str == null || str.length() < length) ? null : str.substring(str.length() - length); return (str == null || str.length() < length) ? null : str.substring(str.length() - length);
} }
/**
* 从左侧开始截取固定长度的字符串 /**
* @param str 待截取字符串 * 从左侧开始截取固定长度的字符串
* @param length 截取的长度 *
* @return * @param str 待截取字符串
* <li>null:字符串为空或字符串长度小于截取的长度</li> * @param length 截取的长度
* <li>非null:截取字符串后的结果</li> * @return <li>null:字符串为空或字符串长度小于截取的长度</li>
*/ * <li>非null:截取字符串后的结果</li>
public static String left(String str, int length) { */
return (str == null || str.length() < length) ? null : str.substring(0, length); public static String left(String str, int length) {
} return (str == null || str.length() < length) ? null : str.substring(0, length);
}
/**
* 截取定长字符串(中文、字符、字母、数字……) /**
* @param str * 截取定长字符串(中文、字符、字母、数字……)
* @param fixedWidth *
* @return * @param str
*/ * @param fixedWidth
public static String subFixedWidthString(String str, int fixedWidth) { * @return
if(str.length() <= fixedWidth) { */
return str; public static String subFixedWidthString(String str, int fixedWidth) {
}else{ if (str.length() <= fixedWidth) {
StringBuilder ret = new StringBuilder(); return str;
fixedWidth = fixedWidth << 1; } else {
int currentWidth = 0; StringBuilder ret = new StringBuilder();
for (int i = 0; i < str.length(); i++) { fixedWidth = fixedWidth << 1;
char ch = str.charAt(i); int currentWidth = 0;
currentWidth += (ch < 128) ? 1 : 2; for (int i = 0; i < str.length(); i++) {
if (currentWidth > fixedWidth) { char ch = str.charAt(i);
currentWidth += (ch < 128) ? 1 : 2;
if (currentWidth > fixedWidth) {
break; break;
} }
ret.append(ch); ret.append(ch);
} }
ret.append("…"); ret.append("…");
return ret.toString(); return ret.toString();
} }
} }
/** /**
* 将数组中字符串按照分隔符连接成一个字符串 * 将数组中字符串按照分隔符连接成一个字符串
* @param seperator 分隔符 *
* @param params 待连接字符串数组 * @param seperator 分隔符
* @return 连接后的字符串 * @param params 待连接字符串数组
*/ * @return 连接后的字符串
public static String join(String seperator, Object... params) { */
return joinArray(seperator, params); public static String join(String seperator, Object... params) {
} return joinArray(seperator, params);
/** }
* 将数组中字符串按照分隔符连接成一个字符串
* @param seperator 分隔符 /**
* @param params 待连接字符串数组 * 将数组中字符串按照分隔符连接成一个字符串
* @return 连接后的字符串 *
*/ * @param seperator 分隔符
public static String joinArray(String seperator, Object[] params) { * @param params 待连接字符串数组
if (params == null || params.length == 0) { * @return 连接后的字符串
*/
public static String joinArray(String seperator, Object[] params) {
if (params == null || params.length == 0) {
return CommonConst.DFT_STRING_VAL; return CommonConst.DFT_STRING_VAL;
} }
StringBuilder ret = new StringBuilder(); StringBuilder ret = new StringBuilder();
for (Object param : params) { for (Object param : params) {
if(param == null) { if (param == null) {
continue; continue;
} }
if (ret.length() > 0) { if (ret.length() > 0) {
ret.append(seperator); ret.append(seperator);
} }
if (param.getClass().isArray()) { if (param.getClass().isArray()) {
ret.append(joinArray(seperator, (Object[])param)); ret.append(joinArray(seperator, (Object[]) param));
} else { } else {
ret.append(param); ret.append(param);
} }
} }
return ret.toString(); return ret.toString();
} }
/** /**
* 比较两个字符串大小 * 比较两个字符串大小
* @param str1 字符串1 *
* @param str2 字符串2 * @param str1 字符串1
* @return * @param str2 字符串2
* <li>-1:str1小</li> * @return <li>-1:str1小</li>
* <li>0:两字符串相等</li> * <li>0:两字符串相等</li>
* <li>1:str2小</li> * <li>1:str2小</li>
*/ */
public static int compare(String str1, String str2) { public static int compare(String str1, String str2) {
if (str1 == null && str2 == null) { if (str1 == null && str2 == null) {
return 0; return 0;
} }
if (str1 == null) { if (str1 == null) {
return -1; return -1;
} }
if (str2 == null) { if (str2 == null) {
return 1; return 1;
} }
int len1 = str1.length(); int len1 = str1.length();
int len2 = str2.length(); int len2 = str2.length();
int len = Math.min(len1, len2); int len = Math.min(len1, len2);
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
char ch1 = str1.charAt(i); char ch1 = str1.charAt(i);
char ch2 = str2.charAt(i); char ch2 = str2.charAt(i);
if (ch1 == ch2) { if (ch1 == ch2) {
continue; continue;
} }
return (ch1 < ch2) ? -1 : 1; return (ch1 < ch2) ? -1 : 1;
} }
if (len1 == len2) { if (len1 == len2) {
return 0; return 0;
} }
return (len1 < len2) ? -1 : 1; return (len1 < len2) ? -1 : 1;
} }
/** /**
* 根据参数填充占位符,构建字符串消息,例如: * 根据参数填充占位符,构建字符串消息,例如:
* msg=我是{} params:小明 返回值为:我是小明 * msg=我是{} params:小明 返回值为:我是小明
* @param message *
* @param params * @param message
* @return * @param params
*/ * @return
public static String buildMessage(String message, Object... params) { */
public static String buildMessage(String message, Object... params) {
// return messageFactory.newMessage(msg,params).getFormattedMessage(); // return messageFactory.newMessage(msg,params).getFormattedMessage();
if (params != null) { if (params != null) {
message = MessageFormatter.arrayFormat(message, params).getMessage(); message = MessageFormatter.arrayFormat(message, params).getMessage();
} }
return message; return message;
} }
/** /**
* 过滤空格 * 过滤空格
* @param str 待过滤字符串 *
* @return 过滤结果 * @param str 待过滤字符串
*/ * @return 过滤结果
public static String trim (String str) { */
return str == null ? null : str.trim(); public static String trim(String str) {
} return str == null ? null : str.trim();
}
/**
* 将字符串指定索引位的字符转换为大写 /**
* @param source * 将字符串指定索引位的字符转换为大写
* @param index *
* * @param source
* @return * @param index
*/ * @return
public static String toUpperCase(String source, int index) { */
char[] chars = source.toCharArray(); public static String toUpperCase(String source, int index) {
chars[index] = Character.toUpperCase(chars[index]); char[] chars = source.toCharArray();
return new String(chars); chars[index] = Character.toUpperCase(chars[index]);
} return new String(chars);
}
// GENERAL_PUNCTUATION 判断中文的“号
// CJK_SYMBOLS_AND_PUNCTUATION 判断中文的。号 // GENERAL_PUNCTUATION 判断中文的“号
// HALFWIDTH_AND_FULLWIDTH_FORMS 判断中文的,号 // CJK_SYMBOLS_AND_PUNCTUATION 判断中文的。号
public static final boolean isChinese(char c) { // HALFWIDTH_AND_FULLWIDTH_FORMS 判断中文的,号
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c); public static final boolean isChinese(char c) {
if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) { || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
return true; || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
} return true;
return false; }
} return false;
}
public static final boolean isChinese(String strName) {
char[] ch = strName.toCharArray(); public static final boolean isChinese(String strName) {
for (int i = 0; i < ch.length; i++) { char[] ch = strName.toCharArray();
char c = ch[i]; for (int i = 0; i < ch.length; i++) {
if (isChinese(c)) { char c = ch[i];
return true; if (isChinese(c)) {
} return true;
} }
return false; }
} return false;
}
public static final boolean isMobile(String str) {
Pattern pattern = Pattern.compile("^[1][3,4,5,7,8][0-9]{9}$"); // 验证手机号 public static final boolean isMobile(String str) {
Matcher matcher = pattern.matcher(str); Pattern pattern = Pattern.compile("^[1][3,4,5,7,8][0-9]{9}$"); // 验证手机号
boolean b = matcher.matches(); Matcher matcher = pattern.matcher(str);
return b; boolean b = matcher.matches();
} return b;
}
public static final boolean isEmail(String str) {
String paString ="^([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)*@([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)+[\\.][A-Za-z]{2,3}([\\.][A-Za-z]{2})?$"; public static final boolean isEmail(String str) {
Pattern pattern = Pattern.compile(paString); // 验证邮箱 String paString = "^([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)*@([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)+[\\.][A-Za-z]{2,3}([\\.][A-Za-z]{2})?$";
Matcher matcher = pattern.matcher(str); Pattern pattern = Pattern.compile(paString); // 验证邮箱
boolean b = matcher.matches(); Matcher matcher = pattern.matcher(str);
return b; boolean b = matcher.matches();
} return b;
}
public static String hiddenName(String name) {
if (isEmpty(name)) { public static String hiddenName(String name) {
if (isEmpty(name)) {
return ""; return "";
} }
if (name.length() <= 2) { if (name.length() <= 2) {
return name.charAt(0) + "*"; return name.charAt(0) + "*";
} else { } else {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append(name.charAt(0)); sb.append(name.charAt(0));
IntStream.range(0, name.length() - 2).forEach(s -> sb.append("*")); IntStream.range(0, name.length() - 2).forEach(s -> sb.append("*"));
sb.append(name.charAt(name.length() - 1)); sb.append(name.charAt(name.length() - 1));
return sb.toString(); return sb.toString();
} }
} }
public static String hiddenMobile(String mobile) { public static String hiddenMobile(String mobile) {
return left(mobile, 3) + "****" + right(mobile, 4); return left(mobile, 3) + "****" + right(mobile, 4);
} }
public static String hiddenIDCardNumber(String idcardNumber) { public static String hiddenIDCardNumber(String idcardNumber) {
if (isEmpty(idcardNumber)) { if (isEmpty(idcardNumber)) {
return ""; return "";
} }
if (idcardNumber.length() <= 15) { if (idcardNumber.length() <= 15) {
return left(idcardNumber, 4) + "******" + right(idcardNumber, 5); return left(idcardNumber, 4) + "******" + right(idcardNumber, 5);
} else { } else {
return left(idcardNumber, 2) + "****" + idcardNumber.substring(6, 8) + "******" + right(idcardNumber, 4); return left(idcardNumber, 2) + "****" + idcardNumber.substring(6, 8) + "******" + right(idcardNumber, 4);
} }
} }
public static String hiddenBankId(String bankId){
if(isEmpty(bankId)){ public static String hiddenBankId(String bankId) {
return ""; if (isEmpty(bankId)) {
} return "";
StringBuilder sb = new StringBuilder(); }
sb.append(left(bankId, 4)); StringBuilder sb = new StringBuilder();
IntStream.range(0, bankId.length() - 8).forEach(s -> sb.append("*")); sb.append(left(bankId, 4));
sb.append(right(bankId, 4)); IntStream.range(0, bankId.length() - 8).forEach(s -> sb.append("*"));
return sb.toString(); sb.append(right(bankId, 4));
} return sb.toString();
}
public static boolean isBlank(CharSequence cs) { public static boolean isBlank(CharSequence cs) {
int strLen; int strLen;
if(cs != null && (strLen = cs.length()) != 0) { if (cs != null && (strLen = cs.length()) != 0) {
for(int i = 0; i < strLen; ++i) { for (int i = 0; i < strLen; ++i) {
if(!Character.isWhitespace(cs.charAt(i))) { if (!Character.isWhitespace(cs.charAt(i))) {
return false; return false;
} }
} }
...@@ -468,12 +494,12 @@ public abstract class StringUtil { ...@@ -468,12 +494,12 @@ public abstract class StringUtil {
return !isBlank(cs); return !isBlank(cs);
} }
public static String andStartTime(String start){ public static String andStartTime(String start) {
return start+" 00:00:00"; return start + " 00:00:00";
} }
public static String andEndTime(String end){ public static String andEndTime(String end) {
return end+" 23:59:59"; return end + " 23:59:59";
} }
...@@ -485,140 +511,146 @@ public abstract class StringUtil { ...@@ -485,140 +511,146 @@ public abstract class StringUtil {
return org.apache.commons.lang3.StringUtils.rightPad(name, org.apache.commons.lang3.StringUtils.length(fullName), "*"); return org.apache.commons.lang3.StringUtils.rightPad(name, org.apache.commons.lang3.StringUtils.length(fullName), "*");
} }
/** /**
* * 判断一个对象是否为空 * * 判断一个对象是否为空
* *
* @param object Object * @param object Object
* @return true:为空 false:非空 * @return true:为空 false:非空
*/ */
public static boolean isNull(Object object) public static boolean isNull(Object object) {
{ return object == null;
return object == null; }
}
/**
/** * * 判断一个对象是否非空
* * 判断一个对象是否非空 *
* * @param object Object
* @param object Object * @return true:非空 false:空
* @return true:非空 false:空 */
*/ public static boolean isNotNull(Object object) {
public static boolean isNotNull(Object object) return !isNull(object);
{ }
return !isNull(object);
} /**
* * 判断一个对象数组是否为空
/** *
* * 判断一个对象数组是否为空 * @param objects 要判断的对象数组
* * * @return true:为空 false:非空
* @param objects 要判断的对象数组 */
** @return true:为空 false:非空 public static boolean isEmpty(Object[] objects) {
*/ return isNull(objects) || (objects.length == 0);
public static boolean isEmpty(Object[] objects) }
{
return isNull(objects) || (objects.length == 0); /**
} * * 判断一个对象数组是否非空
*
/** * @param objects 要判断的对象数组
* * 判断一个对象数组是否非空 * @return true:非空 false:空
* */
* @param objects 要判断的对象数组 public static boolean isNotEmpty(Object[] objects) {
* @return true:非空 false:空 return !isEmpty(objects);
*/ }
public static boolean isNotEmpty(Object[] objects)
{ /**
return !isEmpty(objects); * * 判断一个Collection是否为空, 包含List,Set,Queue
} *
* @param coll 要判断的Collection
/** * @return true:为空 false:非空
* * 判断一个Collection是否为空, 包含List,Set,Queue */
* public static boolean isEmpty(Collection<?> coll) {
* @param coll 要判断的Collection return isNull(coll) || coll.isEmpty();
* @return true:为空 false:非空 }
*/
public static boolean isEmpty(Collection<?> coll) /**
{ * * 判断一个Collection是否非空,包含List,Set,Queue
return isNull(coll) || coll.isEmpty(); *
} * @param coll 要判断的Collection
* @return true:非空 false:空
/** */
* * 判断一个Collection是否非空,包含List,Set,Queue public static boolean isNotEmpty(Collection<?> coll) {
* return !isEmpty(coll);
* @param coll 要判断的Collection }
* @return true:非空 false:空
*/ /**
public static boolean isNotEmpty(Collection<?> coll) * * 判断一个Map是否为空
{ *
return !isEmpty(coll); * @param map 要判断的Map
} * @return true:为空 false:非空
*/
/** public static boolean isEmpty(Map<?, ?> map) {
* * 判断一个Map是否为空 return isNull(map) || map.isEmpty();
* }
* @param map 要判断的Map
* @return true:为空 false:非空 /**
*/ * * 判断一个Map是否为空
public static boolean isEmpty(Map<?, ?> map) *
{ * @param map 要判断的Map
return isNull(map) || map.isEmpty(); * @return true:非空 false:空
} */
public static boolean isNotEmpty(Map<?, ?> map) {
/** return !isEmpty(map);
* * 判断一个Map是否为空 }
*
* @param map 要判断的Map /**
* @return true:非空 false:空 * 是否包含字符串
*/ *
public static boolean isNotEmpty(Map<?, ?> map) * @param str 验证字符串
{ * @param strs 字符串组
return !isEmpty(map); * @return 包含返回true
} */
public static boolean inStringIgnoreCase(String str, String... strs) {
/** if (str != null && strs != null) {
* 是否包含字符串 for (String s : strs) {
* if (str.equalsIgnoreCase(trim(s))) {
* @param str 验证字符串 return true;
* @param strs 字符串组 }
* @return 包含返回true }
*/ }
public static boolean inStringIgnoreCase(String str, String... strs) return false;
{ }
if (str != null && strs != null)
{ /**
for (String s : strs) * 字符串位置交换
{ *
if (str.equalsIgnoreCase(trim(s))) * @param p1 位置1
{ * @param p2 位置2
return true; * @return 交换位置大于字符长度返回原字符。小于返回交换后结果
} */
} public static String switchPosition(String str, int p1, int p2) {
} if (str.length() < p1 + 1 || str.length() < p2 + 1) {
return false; return str;
} }
char[] charArray = str.toCharArray();
public static void main(String[] args) { char temp = charArray[p1];
System.out.println(isEmail("123@qq.com#$%^&*()")); charArray[p1] = charArray[p2];
charArray[p2] = temp;
String regEx="[\n`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。, 、?]"; return String.valueOf(charArray);
}
//可以在中括号内加上任何想要替换的字符,实际上是一个正则表达式
public static void main(String[] args) {
String aa = "";//这里是将特殊字符换为aa字符串," "代表直接去掉 System.out.println(isEmail("123@qq.com#$%^&*()"));
Pattern p = Pattern.compile(regEx); String regEx = "[\n`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。, 、?]";
Matcher m = p.matcher("原字符串");//这里把想要替换的字符串传进来 //可以在中括号内加上任何想要替换的字符,实际上是一个正则表达式
String newString = m.replaceAll(aa).trim(); String aa = "";//这里是将特殊字符换为aa字符串," "代表直接去掉
System.out.println("newString====="+newString);
Pattern p = Pattern.compile(regEx);
//将替换后的字符串存在变量newString中
Matcher m = p.matcher("原字符串");//这里把想要替换的字符串传进来
//方法二 如果第一种太麻烦可以直接用下面的
String newString = m.replaceAll(aa).trim();
String str = "#$%^&*()我的正确#$%^&*()原字符串#$%^&*()"; System.out.println("newString=====" + newString);
String newString2 = str.replaceAll(regEx,aa).trim();//不想保留原来的字符串可以直接写成 “str = str.replaceAll(regEX,aa);” //将替换后的字符串存在变量newString中
System.out.println("newString2====="+newString2);
//方法二 如果第一种太麻烦可以直接用下面的
}
String str = "#$%^&*()我的正确#$%^&*()原字符串#$%^&*()";
String newString2 = str.replaceAll(regEx, aa).trim();//不想保留原来的字符串可以直接写成 “str = str.replaceAll(regEX,aa);”
System.out.println("newString2=====" + newString2);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment