packageutil;importorg.springframework.util.StringUtils;importjava.math.BigDecimal;importjava.text.ParseException;importjava.text.SimpleDateFormat;importjava.util.Date;importjava.util.List;importjava.util.regex.Pattern;public classConvertUtil {/*** 检查字符串是否为null、""或" "
*@paramstr
* 字符串
*@returnnull、""或" "返回true,否则返回false*/
public static booleanisNullOrEmpty(String str){return !StringUtils.hasText(str);
}/*** 将Object转换为String,若为空,则转换为null
*@paramobj
* Object
*@returnnull或字符串*/
public staticString toString(Object obj){if(StringUtils.isEmpty(obj))return null;else
returnobj.toString();
}/*** 将字符串"on"转换为"Y",否则为“N”
*@paramstr
*@return"on" -> "Y",否则为"N"*/
public staticString toActiveString(String str){if(isNullOrEmpty(str))return "N";else if(str.equals("on"))return "Y";else
return "N";
}/*** 将以","作为分隔符的字符串转为List
*@paramstr
*@returnList*/
public static ListtoList(String str) {if(isNullOrEmpty(str))return null;else{
String[] arr= str.split(",");returnjava.util.Arrays.asList(arr);
}
}/*** 将空字符串转换为null,将带有空格的字符串trim
*@paramstr
* 待处理字符串
*@returnnull或字符串*/
public staticString StringToNull(String str){if(isNullOrEmpty(str))return null;else
returnstr.trim();
}/*** 字符串转换为日期类型(Date,不是DateTime)
*@paramstr 格式:yyyy-MM-dd
*@return*@throwsParseException*/
public static Date toDate(String str) throwsParseException {if(isNullOrEmpty(str))return null;else{
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");returnsdf.parse(str);
}
}/*** 字符串转换为日期类型(DateTime,不是Date)
*@paramstr 格式:yyyy-MM-dd HH:mm
*@return*@throwsParseException*/
public static Date toDateTime(String str) throwsParseException {if(isNullOrEmpty(str))return null;else{
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm");returnsdf.parse(str);
}
}public static Date toDate(String str, String formater) throwsParseException {if(isNullOrEmpty(str))return null;else{
SimpleDateFormat sdf= newSimpleDateFormat(formater);returnsdf.parse(str);
}
}/*** 将日期转换为指定格式的字符串
*@paramdate
*@paramformater
*@return
*/
public staticString toDateString(Date date, String formater){return newSimpleDateFormat(formater).format(date);
}public staticDouble toDouble(String str){if (isNullOrEmpty(str)) return null;else returnDouble.parseDouble(str);
}public staticBigDecimal toDecimal(String str){if (isNullOrEmpty(str)) return null;else return newBigDecimal(str);
}public staticInteger toInteger(String str){if (isNullOrEmpty(str)) return null;else returnInteger.parseInt(str);
}/*** 判断是否为整数
*@paramstr 传入的字符串
*@return是整数返回true,否则返回false*/
public static booleanisInteger(String str){
Pattern pattern= Pattern.compile("^[-\\+]?[\\d]*$");returnpattern.matcher(str).matches();
}/*** 将字节数组转换为十六进制字符串
*
*@paramb
* 字节数组
*@return十六进制字符串*/
public static String bytesToHexString(byte[] b){
StringBuilder stringBuilder= new StringBuilder("");for (int i = 0; i < b.length; i++) {int v = b[i] & 0xFF;
String hv=Integer.toHexString(v);if (hv.length() < 2) {
stringBuilder.append(0);
}
stringBuilder.append(hv);
}returnstringBuilder.toString();
}/*** 将十六进制字符串转换字节数组
*
*@paramhexString
* 十六进制字符串
*@return字节数组*/
public static byte[] hexStringToBytes(String hexString) {
hexString=hexString.toUpperCase();int length = hexString.length() / 2;char[] hexChars =hexString.toCharArray();byte[] d = new byte[length];for (int i = 0; i < length; i++) {int pos = i * 2;
d[i]= (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
}returnd;
}/*** 将char转换为byte
*@paramc char
*@returnbyte*/
private static byte charToByte(charc) {return (byte)"0123456789ABCDEF".indexOf(c);
}
}