|
@@ -15,31 +15,56 @@ import java.util.Map;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
/**
|
|
|
-* 类型转换器
|
|
|
-* @author W.Y.C
|
|
|
-* @date 2025/6/11 14:02
|
|
|
-* @version 1.0
|
|
|
-*/
|
|
|
+ * 类型转换工具类
|
|
|
+ *
|
|
|
+ * <p>提供各种数据类型之间的转换方法,包括基础类型、集合、日期、枚举等转换</p>
|
|
|
+ *
|
|
|
+ * @author W.Y.C
|
|
|
+ * @date 2025/6/11 14:02
|
|
|
+ * @version 1.1
|
|
|
+ */
|
|
|
public class ConvertUtil {
|
|
|
|
|
|
// =================== 基础类型转换 ===================
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将int值转换为4字节的byte数组(大端序)
|
|
|
+ *
|
|
|
+ * @param value 要转换的int值
|
|
|
+ * @return 4字节的byte数组
|
|
|
+ */
|
|
|
public static byte[] intToBytes(int value) {
|
|
|
return new byte[] {
|
|
|
- (byte)(value >> 24),
|
|
|
- (byte)(value >> 16),
|
|
|
- (byte)(value >> 8),
|
|
|
- (byte)value
|
|
|
+ (byte)(value >> 24),
|
|
|
+ (byte)(value >> 16),
|
|
|
+ (byte)(value >> 8),
|
|
|
+ (byte)value
|
|
|
};
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 将4字节的byte数组转换为int值(大端序)
|
|
|
+ *
|
|
|
+ * @param bytes 4字节的byte数组
|
|
|
+ * @return 转换后的int值
|
|
|
+ * @throws IllegalArgumentException 如果输入数组长度不为4
|
|
|
+ */
|
|
|
public static int bytesToInt(byte[] bytes) {
|
|
|
+ if (bytes == null || bytes.length != 4) {
|
|
|
+ throw new IllegalArgumentException("输入字节数组必须为4字节长度");
|
|
|
+ }
|
|
|
return ((bytes[0] & 0xFF) << 24) |
|
|
|
- ((bytes[1] & 0xFF) << 16) |
|
|
|
- ((bytes[2] & 0xFF) << 8) |
|
|
|
- (bytes[3] & 0xFF);
|
|
|
+ ((bytes[1] & 0xFF) << 16) |
|
|
|
+ ((bytes[2] & 0xFF) << 8) |
|
|
|
+ (bytes[3] & 0xFF);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 将long值转换为8字节的byte数组(大端序)
|
|
|
+ *
|
|
|
+ * @param value 要转换的long值
|
|
|
+ * @return 8字节的byte数组
|
|
|
+ */
|
|
|
public static byte[] longToBytes(long value) {
|
|
|
byte[] result = new byte[8];
|
|
|
for (int i = 7; i >= 0; i--) {
|
|
@@ -49,7 +74,17 @@ public class ConvertUtil {
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 将8字节的byte数组转换为long值(大端序)
|
|
|
+ *
|
|
|
+ * @param bytes 8字节的byte数组
|
|
|
+ * @return 转换后的long值
|
|
|
+ * @throws IllegalArgumentException 如果输入数组长度不为8
|
|
|
+ */
|
|
|
public static long bytesToLong(byte[] bytes) {
|
|
|
+ if (bytes == null || bytes.length != 8) {
|
|
|
+ throw new IllegalArgumentException("输入字节数组必须为8字节长度");
|
|
|
+ }
|
|
|
long value = 0;
|
|
|
for (byte b : bytes) {
|
|
|
value = (value << 8) + (b & 0xFF);
|
|
@@ -57,63 +92,136 @@ public class ConvertUtil {
|
|
|
return value;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 将short值转换为2字节的byte数组(大端序)
|
|
|
+ *
|
|
|
+ * @param value 要转换的short值
|
|
|
+ * @return 2字节的byte数组
|
|
|
+ */
|
|
|
public static byte[] shortToBytes(short value) {
|
|
|
return new byte[] {
|
|
|
- (byte)(value >> 8),
|
|
|
- (byte)value
|
|
|
+ (byte)(value >> 8),
|
|
|
+ (byte)value
|
|
|
};
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 将2字节的byte数组转换为short值(大端序)
|
|
|
+ *
|
|
|
+ * @param bytes 2字节的byte数组
|
|
|
+ * @return 转换后的short值
|
|
|
+ * @throws IllegalArgumentException 如果输入数组长度不为2
|
|
|
+ */
|
|
|
public static short bytesToShort(byte[] bytes) {
|
|
|
+ if (bytes == null || bytes.length != 2) {
|
|
|
+ throw new IllegalArgumentException("输入字节数组必须为2字节长度");
|
|
|
+ }
|
|
|
return (short)(((bytes[0] & 0xFF) << 8) | (bytes[1] & 0xFF));
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 将byte转换为无符号int值
|
|
|
+ *
|
|
|
+ * @param b 要转换的byte
|
|
|
+ * @return 无符号int值 (0-255)
|
|
|
+ */
|
|
|
public static int byteToUnsignedInt(byte b) {
|
|
|
return b & 0xFF;
|
|
|
}
|
|
|
|
|
|
// =================== 十六进制转换 ===================
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将字节数组转换为十六进制字符串
|
|
|
+ *
|
|
|
+ * @param bytes 要转换的字节数组
|
|
|
+ * @return 十六进制字符串 (小写)
|
|
|
+ */
|
|
|
public static String toHex(byte[] bytes) {
|
|
|
- StringBuilder sb = new StringBuilder();
|
|
|
+ if (bytes == null) return "";
|
|
|
+ StringBuilder sb = new StringBuilder(bytes.length * 2);
|
|
|
for (byte b : bytes) {
|
|
|
sb.append(String.format("%02x", b));
|
|
|
}
|
|
|
return sb.toString();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 将十六进制字符串转换为字节数组
|
|
|
+ *
|
|
|
+ * @param hex 十六进制字符串 (不区分大小写)
|
|
|
+ * @return 转换后的字节数组
|
|
|
+ * @throws IllegalArgumentException 如果输入字符串长度不是偶数
|
|
|
+ */
|
|
|
public static byte[] hexToBytes(String hex) {
|
|
|
- int len = hex.length();
|
|
|
- byte[] data = new byte[len / 2];
|
|
|
- for (int i = 0; i < len; i += 2) {
|
|
|
- data[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4)
|
|
|
- + Character.digit(hex.charAt(i+1), 16));
|
|
|
+ if (hex == null || hex.isEmpty()) {
|
|
|
+ return new byte[0];
|
|
|
+ }
|
|
|
+ if (hex.length() % 2 != 0) {
|
|
|
+ throw new IllegalArgumentException("十六进制字符串长度必须为偶数");
|
|
|
+ }
|
|
|
+
|
|
|
+ hex = hex.toLowerCase();
|
|
|
+ byte[] data = new byte[hex.length() / 2];
|
|
|
+ for (int i = 0; i < hex.length(); i += 2) {
|
|
|
+ int high = Character.digit(hex.charAt(i), 16);
|
|
|
+ int low = Character.digit(hex.charAt(i+1), 16);
|
|
|
+ if (high == -1 || low == -1) {
|
|
|
+ throw new IllegalArgumentException("包含非十六进制字符: " + hex);
|
|
|
+ }
|
|
|
+ data[i / 2] = (byte) ((high << 4) + low);
|
|
|
}
|
|
|
return data;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 将十六进制字符串转换为指定字符集的字符串
|
|
|
+ *
|
|
|
+ * @param hex 十六进制字符串
|
|
|
+ * @param charset 目标字符集
|
|
|
+ * @return 转换后的字符串
|
|
|
+ */
|
|
|
public static String hexToString(String hex, Charset charset) {
|
|
|
return new String(hexToBytes(hex), charset);
|
|
|
}
|
|
|
|
|
|
// =================== 对象到基础类型转换 ===================
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将对象转换为Byte类型
|
|
|
+ *
|
|
|
+ * @param value 要转换的对象
|
|
|
+ * @return 转换后的Byte值,转换失败返回null
|
|
|
+ */
|
|
|
public static Byte toByte(Object value) {
|
|
|
- return toByte(value, (Byte) null);
|
|
|
+ return toByte(value, null);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 将对象转换为Byte类型
|
|
|
+ *
|
|
|
+ * @param value 要转换的对象
|
|
|
+ * @param defaultValue 转换失败时的默认值
|
|
|
+ * @return 转换后的Byte值
|
|
|
+ */
|
|
|
public static Byte toByte(Object value, Byte defaultValue) {
|
|
|
- if (value == null) {return defaultValue;};
|
|
|
+ if (value == null) return defaultValue;
|
|
|
if (value instanceof Number) {
|
|
|
return ((Number) value).byteValue();
|
|
|
}
|
|
|
try {
|
|
|
- return Byte.parseByte(value.toString());
|
|
|
+ return Byte.parseByte(value.toString().trim());
|
|
|
} catch (Exception e) {
|
|
|
return defaultValue;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 将对象转换为byte数组
|
|
|
+ *
|
|
|
+ * @param value 要转换的对象
|
|
|
+ * @return 转换后的byte数组,转换失败返回空数组
|
|
|
+ */
|
|
|
public static byte[] toPrimitiveByteArray(Object value) {
|
|
|
if (value instanceof byte[]) {
|
|
|
return (byte[]) value;
|
|
@@ -122,19 +230,32 @@ public class ConvertUtil {
|
|
|
Byte[] bytes = (Byte[]) value;
|
|
|
byte[] result = new byte[bytes.length];
|
|
|
for (int i = 0; i < bytes.length; i++) {
|
|
|
- result[i] = bytes[i];
|
|
|
+ result[i] = bytes[i] != null ? bytes[i] : 0;
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
return new byte[0];
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 将对象转换为Integer类型
|
|
|
+ *
|
|
|
+ * @param value 要转换的对象
|
|
|
+ * @return 转换后的Integer值,转换失败返回null
|
|
|
+ */
|
|
|
public static Integer toInt(Object value) {
|
|
|
return toInt(value, null);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 将对象转换为Integer类型
|
|
|
+ *
|
|
|
+ * @param value 要转换的对象
|
|
|
+ * @param defaultValue 转换失败时的默认值
|
|
|
+ * @return 转换后的Integer值
|
|
|
+ */
|
|
|
public static Integer toInt(Object value, Integer defaultValue) {
|
|
|
- if (value == null) {return defaultValue;};
|
|
|
+ if (value == null) return defaultValue;
|
|
|
if (value instanceof Number) {
|
|
|
return ((Number) value).intValue();
|
|
|
}
|
|
@@ -145,23 +266,229 @@ public class ConvertUtil {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // 其他基本类型转换(Long, Short, Float, Double, Boolean, Character)实现类似
|
|
|
- // 为节省篇幅,这里省略具体实现,实际使用时需补充完整
|
|
|
+ /**
|
|
|
+ * 将对象转换为Long类型
|
|
|
+ *
|
|
|
+ * @param value 要转换的对象
|
|
|
+ * @return 转换后的Long值,转换失败返回null
|
|
|
+ */
|
|
|
+ public static Long toLong(Object value) {
|
|
|
+ return toLong(value, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将对象转换为Long类型
|
|
|
+ *
|
|
|
+ * @param value 要转换的对象
|
|
|
+ * @param defaultValue 转换失败时的默认值
|
|
|
+ * @return 转换后的Long值
|
|
|
+ */
|
|
|
+ public static Long toLong(Object value, Long defaultValue) {
|
|
|
+ if (value == null) return defaultValue;
|
|
|
+ if (value instanceof Number) {
|
|
|
+ return ((Number) value).longValue();
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ return Long.parseLong(value.toString().trim());
|
|
|
+ } catch (Exception e) {
|
|
|
+ return defaultValue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将对象转换为Short类型
|
|
|
+ *
|
|
|
+ * @param value 要转换的对象
|
|
|
+ * @return 转换后的Short值,转换失败返回null
|
|
|
+ */
|
|
|
+ public static Short toShort(Object value) {
|
|
|
+ return toShort(value, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将对象转换为Short类型
|
|
|
+ *
|
|
|
+ * @param value 要转换的对象
|
|
|
+ * @param defaultValue 转换失败时的默认值
|
|
|
+ * @return 转换后的Short值
|
|
|
+ */
|
|
|
+ public static Short toShort(Object value, Short defaultValue) {
|
|
|
+ if (value == null) return defaultValue;
|
|
|
+ if (value instanceof Number) {
|
|
|
+ return ((Number) value).shortValue();
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ return Short.parseShort(value.toString().trim());
|
|
|
+ } catch (Exception e) {
|
|
|
+ return defaultValue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将对象转换为Float类型
|
|
|
+ *
|
|
|
+ * @param value 要转换的对象
|
|
|
+ * @return 转换后的Float值,转换失败返回null
|
|
|
+ */
|
|
|
+ public static Float toFloat(Object value) {
|
|
|
+ return toFloat(value, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将对象转换为Float类型
|
|
|
+ *
|
|
|
+ * @param value 要转换的对象
|
|
|
+ * @param defaultValue 转换失败时的默认值
|
|
|
+ * @return 转换后的Float值
|
|
|
+ */
|
|
|
+ public static Float toFloat(Object value, Float defaultValue) {
|
|
|
+ if (value == null) return defaultValue;
|
|
|
+ if (value instanceof Number) {
|
|
|
+ return ((Number) value).floatValue();
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ return Float.parseFloat(value.toString().trim());
|
|
|
+ } catch (Exception e) {
|
|
|
+ return defaultValue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将对象转换为Double类型
|
|
|
+ *
|
|
|
+ * @param value 要转换的对象
|
|
|
+ * @return 转换后的Double值,转换失败返回null
|
|
|
+ */
|
|
|
+ public static Double toDouble(Object value) {
|
|
|
+ return toDouble(value, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将对象转换为Double类型
|
|
|
+ *
|
|
|
+ * @param value 要转换的对象
|
|
|
+ * @param defaultValue 转换失败时的默认值
|
|
|
+ * @return 转换后的Double值
|
|
|
+ */
|
|
|
+ public static Double toDouble(Object value, Double defaultValue) {
|
|
|
+ if (value == null) return defaultValue;
|
|
|
+ if (value instanceof Number) {
|
|
|
+ return ((Number) value).doubleValue();
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ return Double.parseDouble(value.toString().trim());
|
|
|
+ } catch (Exception e) {
|
|
|
+ return defaultValue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将对象转换为Boolean类型
|
|
|
+ *
|
|
|
+ * <p>支持以下转换:<br>
|
|
|
+ * - 字符串 "true" (不区分大小写) -> true<br>
|
|
|
+ * - 字符串 "false" (不区分大小写) -> false<br>
|
|
|
+ * - 数字 0 -> false, 非0 -> true<br>
|
|
|
+ * - 其他对象通过Boolean.parseBoolean转换</p>
|
|
|
+ *
|
|
|
+ * @param value 要转换的对象
|
|
|
+ * @return 转换后的Boolean值,转换失败返回null
|
|
|
+ */
|
|
|
+ public static Boolean toBoolean(Object value) {
|
|
|
+ return toBoolean(value, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将对象转换为Boolean类型
|
|
|
+ *
|
|
|
+ * @param value 要转换的对象
|
|
|
+ * @param defaultValue 转换失败时的默认值
|
|
|
+ * @return 转换后的Boolean值
|
|
|
+ */
|
|
|
+ public static Boolean toBoolean(Object value, Boolean defaultValue) {
|
|
|
+ if (value == null) return defaultValue;
|
|
|
+ if (value instanceof Boolean) {
|
|
|
+ return (Boolean) value;
|
|
|
+ }
|
|
|
+ if (value instanceof Number) {
|
|
|
+ return ((Number) value).intValue() != 0;
|
|
|
+ }
|
|
|
+ if (value instanceof String) {
|
|
|
+ String strVal = ((String) value).trim().toLowerCase();
|
|
|
+ if ("true".equals(strVal)) return true;
|
|
|
+ if ("false".equals(strVal)) return false;
|
|
|
+ if ("1".equals(strVal)) return true;
|
|
|
+ if ("0".equals(strVal)) return false;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ return Boolean.parseBoolean(value.toString());
|
|
|
+ } catch (Exception e) {
|
|
|
+ return defaultValue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将对象转换为Character类型
|
|
|
+ *
|
|
|
+ * @param value 要转换的对象
|
|
|
+ * @return 转换后的Character值,转换失败返回null
|
|
|
+ */
|
|
|
+ public static Character toCharacter(Object value) {
|
|
|
+ return toCharacter(value, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将对象转换为Character类型
|
|
|
+ *
|
|
|
+ * @param value 要转换的对象
|
|
|
+ * @param defaultValue 转换失败时的默认值
|
|
|
+ * @return 转换后的Character值
|
|
|
+ */
|
|
|
+ public static Character toCharacter(Object value, Character defaultValue) {
|
|
|
+ if (value == null) return defaultValue;
|
|
|
+ if (value instanceof Character) {
|
|
|
+ return (Character) value;
|
|
|
+ }
|
|
|
+ if (value instanceof Number) {
|
|
|
+ return (char) ((Number) value).intValue();
|
|
|
+ }
|
|
|
+ String str = value.toString();
|
|
|
+ if (str.length() == 1) {
|
|
|
+ return str.charAt(0);
|
|
|
+ }
|
|
|
+ return defaultValue;
|
|
|
+ }
|
|
|
|
|
|
// =================== 数字转换 ===================
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将对象转换为BigDecimal类型
|
|
|
+ *
|
|
|
+ * @param value 要转换的对象
|
|
|
+ * @return 转换后的BigDecimal值,转换失败返回null
|
|
|
+ */
|
|
|
public static BigDecimal toBigDecimal(Object value) {
|
|
|
return toBigDecimal(value, null);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 将对象转换为BigDecimal类型
|
|
|
+ *
|
|
|
+ * @param value 要转换的对象
|
|
|
+ * @param defaultValue 转换失败时的默认值
|
|
|
+ * @return 转换后的BigDecimal值
|
|
|
+ */
|
|
|
public static BigDecimal toBigDecimal(Object value, BigDecimal defaultValue) {
|
|
|
- if (value == null) {return defaultValue;};
|
|
|
+ if (value == null) return defaultValue;
|
|
|
if (value instanceof BigDecimal) {
|
|
|
return (BigDecimal) value;
|
|
|
}
|
|
|
if (value instanceof BigInteger) {
|
|
|
return new BigDecimal((BigInteger) value);
|
|
|
}
|
|
|
+ if (value instanceof Number) {
|
|
|
+ return new BigDecimal(value.toString());
|
|
|
+ }
|
|
|
try {
|
|
|
return new BigDecimal(value.toString().trim());
|
|
|
} catch (Exception e) {
|
|
@@ -169,15 +496,34 @@ public class ConvertUtil {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 将对象转换为BigInteger类型
|
|
|
+ *
|
|
|
+ * @param value 要转换的对象
|
|
|
+ * @return 转换后的BigInteger值,转换失败返回null
|
|
|
+ */
|
|
|
public static BigInteger toBigInteger(Object value) {
|
|
|
return toBigInteger(value, null);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 将对象转换为BigInteger类型
|
|
|
+ *
|
|
|
+ * @param value 要转换的对象
|
|
|
+ * @param defaultValue 转换失败时的默认值
|
|
|
+ * @return 转换后的BigInteger值
|
|
|
+ */
|
|
|
public static BigInteger toBigInteger(Object value, BigInteger defaultValue) {
|
|
|
- if (value == null) {return defaultValue;};
|
|
|
+ if (value == null) return defaultValue;
|
|
|
if (value instanceof BigInteger) {
|
|
|
return (BigInteger) value;
|
|
|
}
|
|
|
+ if (value instanceof BigDecimal) {
|
|
|
+ return ((BigDecimal) value).toBigInteger();
|
|
|
+ }
|
|
|
+ if (value instanceof Number) {
|
|
|
+ return BigInteger.valueOf(((Number) value).longValue());
|
|
|
+ }
|
|
|
try {
|
|
|
return new BigInteger(value.toString().trim());
|
|
|
} catch (Exception e) {
|
|
@@ -186,13 +532,26 @@ public class ConvertUtil {
|
|
|
}
|
|
|
|
|
|
// =================== 字符串转换 ===================
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将对象转换为String类型
|
|
|
+ *
|
|
|
+ * @param value 要转换的对象
|
|
|
+ * @return 转换后的String值,转换失败返回null
|
|
|
+ */
|
|
|
public static String toStr(Object value) {
|
|
|
return toStr(value, null);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 将对象转换为String类型
|
|
|
+ *
|
|
|
+ * @param value 要转换的对象
|
|
|
+ * @param defaultValue 转换失败时的默认值
|
|
|
+ * @return 转换后的String值
|
|
|
+ */
|
|
|
public static String toStr(Object value, String defaultValue) {
|
|
|
- if (value == null) {return defaultValue;};
|
|
|
+ if (value == null) return defaultValue;
|
|
|
if (value instanceof String) {
|
|
|
return (String) value;
|
|
|
}
|
|
@@ -201,10 +560,29 @@ public class ConvertUtil {
|
|
|
|
|
|
// =================== 日期时间转换 ===================
|
|
|
|
|
|
+ /**
|
|
|
+ * 将对象转换为Date类型
|
|
|
+ *
|
|
|
+ * <p>支持转换的类型:<br>
|
|
|
+ * - java.util.Date<br>
|
|
|
+ * - java.time.LocalDateTime<br>
|
|
|
+ * - 时间戳 (long/Long)<br>
|
|
|
+ * - 日期字符串 (格式: yyyy-MM-dd HH:mm:ss)</p>
|
|
|
+ *
|
|
|
+ * @param value 要转换的对象
|
|
|
+ * @return 转换后的Date值,转换失败返回null
|
|
|
+ */
|
|
|
public static Date toDate(Object value) {
|
|
|
return toDate(value, null);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 将对象转换为Date类型
|
|
|
+ *
|
|
|
+ * @param value 要转换的对象
|
|
|
+ * @param defaultValue 转换失败时的默认值
|
|
|
+ * @return 转换后的Date值
|
|
|
+ */
|
|
|
public static Date toDate(Object value, Date defaultValue) {
|
|
|
if (value == null) {
|
|
|
return defaultValue;
|
|
@@ -216,14 +594,44 @@ public class ConvertUtil {
|
|
|
LocalDateTime ldtValue = (LocalDateTime) value;
|
|
|
return Date.from(ldtValue.atZone(ZoneId.systemDefault()).toInstant());
|
|
|
}
|
|
|
- // 可扩展:支持字符串解析等其他类型
|
|
|
+ if (value instanceof Number) {
|
|
|
+ return new Date(((Number) value).longValue());
|
|
|
+ }
|
|
|
+ if (value instanceof String) {
|
|
|
+ try {
|
|
|
+ // 简化实现,实际应使用DateTimeFormatter
|
|
|
+ return Date.from(LocalDateTime.parse((String) value)
|
|
|
+ .atZone(ZoneId.systemDefault()).toInstant());
|
|
|
+ } catch (Exception e) {
|
|
|
+ return defaultValue;
|
|
|
+ }
|
|
|
+ }
|
|
|
return defaultValue;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 将对象转换为LocalDateTime类型
|
|
|
+ *
|
|
|
+ * <p>支持转换的类型:<br>
|
|
|
+ * - java.time.LocalDateTime<br>
|
|
|
+ * - java.util.Date<br>
|
|
|
+ * - 时间戳 (long/Long)<br>
|
|
|
+ * - 日期字符串 (格式: yyyy-MM-dd HH:mm:ss)</p>
|
|
|
+ *
|
|
|
+ * @param value 要转换的对象
|
|
|
+ * @return 转换后的LocalDateTime值,转换失败返回null
|
|
|
+ */
|
|
|
public static LocalDateTime toLocalDateTime(Object value) {
|
|
|
return toLocalDateTime(value, null);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 将对象转换为LocalDateTime类型
|
|
|
+ *
|
|
|
+ * @param value 要转换的对象
|
|
|
+ * @param defaultValue 转换失败时的默认值
|
|
|
+ * @return 转换后的LocalDateTime值
|
|
|
+ */
|
|
|
public static LocalDateTime toLocalDateTime(Object value, LocalDateTime defaultValue) {
|
|
|
if (value == null) {
|
|
|
return defaultValue;
|
|
@@ -235,23 +643,58 @@ public class ConvertUtil {
|
|
|
Date dateValue = (Date) value;
|
|
|
return dateValue.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
|
|
|
}
|
|
|
- // 可扩展:支持字符串解析等其他类型
|
|
|
+ if (value instanceof Number) {
|
|
|
+ return LocalDateTime.ofInstant(
|
|
|
+ new Date(((Number) value).longValue()).toInstant(),
|
|
|
+ ZoneId.systemDefault());
|
|
|
+ }
|
|
|
+ if (value instanceof String) {
|
|
|
+ try {
|
|
|
+ // 简化实现,实际应使用DateTimeFormatter
|
|
|
+ return LocalDateTime.parse((String) value);
|
|
|
+ } catch (Exception e) {
|
|
|
+ return defaultValue;
|
|
|
+ }
|
|
|
+ }
|
|
|
return defaultValue;
|
|
|
}
|
|
|
|
|
|
// =================== 集合转换 ===================
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将对象转换为List集合
|
|
|
+ *
|
|
|
+ * <p>支持转换的类型:<br>
|
|
|
+ * - 数组<br>
|
|
|
+ * - Iterable/Collection<br>
|
|
|
+ * - Map (转换为Entry集合)<br>
|
|
|
+ * - 单个对象 (创建单元素集合)</p>
|
|
|
+ *
|
|
|
+ * @param <T> 目标元素类型
|
|
|
+ * @param value 要转换的对象
|
|
|
+ * @return 转换后的List集合
|
|
|
+ */
|
|
|
public static <T> List<T> toList(Object value) {
|
|
|
return toList(value, ArrayList::new);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 将对象转换为List集合
|
|
|
+ *
|
|
|
+ * @param <T> 目标元素类型
|
|
|
+ * @param value 要转换的对象
|
|
|
+ * @param supplier List集合的提供者
|
|
|
+ * @return 转换后的List集合
|
|
|
+ */
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
public static <T> List<T> toList(Object value, java.util.function.Supplier<List<T>> supplier) {
|
|
|
- if (value == null) {return Collections.emptyList();};
|
|
|
+ if (value == null) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
List<T> result = supplier.get();
|
|
|
-
|
|
|
+
|
|
|
if (value instanceof Iterable) {
|
|
|
for (Object item : (Iterable<?>) value) {
|
|
|
- // 实际使用时需要类型转换
|
|
|
result.add((T) item);
|
|
|
}
|
|
|
} else if (value.getClass().isArray()) {
|
|
@@ -269,10 +712,26 @@ public class ConvertUtil {
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 将对象转换为Map集合
|
|
|
+ *
|
|
|
+ * <p>支持转换的类型:<br>
|
|
|
+ * - Map (直接转换)<br>
|
|
|
+ * - Entry集合<br>
|
|
|
+ * - 键值对数组 (偶数长度)</p>
|
|
|
+ *
|
|
|
+ * @param <K> 键类型
|
|
|
+ * @param <V> 值类型
|
|
|
+ * @param value 要转换的对象
|
|
|
+ * @return 转换后的Map集合
|
|
|
+ */
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
public static <K, V> Map<K, V> toMap(Object value) {
|
|
|
- if (value == null) {return Collections.emptyMap();};
|
|
|
+ if (value == null) {
|
|
|
+ return Collections.emptyMap();
|
|
|
+ }
|
|
|
Map<K, V> result = new HashMap<>();
|
|
|
-
|
|
|
+
|
|
|
if (value instanceof Map) {
|
|
|
for (Map.Entry<?, ?> entry : ((Map<?, ?>) value).entrySet()) {
|
|
|
result.put((K) entry.getKey(), (V) entry.getValue());
|
|
@@ -285,34 +744,58 @@ public class ConvertUtil {
|
|
|
}
|
|
|
}
|
|
|
} else if (value.getClass().isArray()) {
|
|
|
- // 处理数组类型的转换
|
|
|
+ Object[] array = (Object[]) value;
|
|
|
+ if (array.length % 2 != 0) {
|
|
|
+ throw new IllegalArgumentException("数组长度必须为偶数");
|
|
|
+ }
|
|
|
+ for (int i = 0; i < array.length; i += 2) {
|
|
|
+ result.put((K) array[i], (V) array[i + 1]);
|
|
|
+ }
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
// =================== 枚举转换 ===================
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将对象转换为枚举类型
|
|
|
+ *
|
|
|
+ * @param <E> 枚举类型
|
|
|
+ * @param enumClass 枚举类
|
|
|
+ * @param value 要转换的对象
|
|
|
+ * @return 转换后的枚举值,转换失败返回null
|
|
|
+ */
|
|
|
public static <E extends Enum<E>> E toEnum(Class<E> enumClass, Object value) {
|
|
|
return toEnum(enumClass, value, null);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 将对象转换为枚举类型
|
|
|
+ *
|
|
|
+ * @param <E> 枚举类型
|
|
|
+ * @param enumClass 枚举类
|
|
|
+ * @param value 要转换的对象
|
|
|
+ * @param defaultValue 转换失败时的默认值
|
|
|
+ * @return 转换后的枚举值
|
|
|
+ */
|
|
|
public static <E extends Enum<E>> E toEnum(Class<E> enumClass, Object value, E defaultValue) {
|
|
|
- if (value == null) {return defaultValue;};
|
|
|
-
|
|
|
+ if (value == null) return defaultValue;
|
|
|
+
|
|
|
// 尝试通过名称匹配
|
|
|
if (value instanceof String) {
|
|
|
+ String strValue = ((String) value).trim();
|
|
|
try {
|
|
|
- return Enum.valueOf(enumClass, (String) value);
|
|
|
+ return Enum.valueOf(enumClass, strValue);
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
// 尝试忽略大小写匹配
|
|
|
for (E constant : enumClass.getEnumConstants()) {
|
|
|
- if (constant.name().equalsIgnoreCase((String) value)) {
|
|
|
+ if (constant.name().equalsIgnoreCase(strValue)) {
|
|
|
return constant;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 尝试通过ordinal匹配
|
|
|
if (value instanceof Number) {
|
|
|
int ordinal = ((Number) value).intValue();
|
|
@@ -321,21 +804,26 @@ public class ConvertUtil {
|
|
|
return constants[ordinal];
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
return defaultValue;
|
|
|
}
|
|
|
|
|
|
// =================== 中文数字转换 ===================
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将中文数字字符串转换为整数
|
|
|
+ *
|
|
|
+ * <p>支持的中文数字:零,一,二,三,四,五,六,七,八,九,十,百,千,万,亿</p>
|
|
|
+ *
|
|
|
+ * @param chineseNumber 中文数字字符串
|
|
|
+ * @return 转换后的整数值
|
|
|
+ */
|
|
|
public static int chineseToNumber(String chineseNumber) {
|
|
|
- // 简化的实现,实际需要更完整的处理
|
|
|
- Map<Character, Integer> unitMap = new HashMap<>();
|
|
|
- unitMap.put('十', 10);
|
|
|
- unitMap.put('百', 100);
|
|
|
- unitMap.put('千', 1000);
|
|
|
- unitMap.put('万', 10000);
|
|
|
- unitMap.put('亿', 100000000);
|
|
|
-
|
|
|
+ if (chineseNumber == null || chineseNumber.isEmpty()) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 数字映射
|
|
|
Map<Character, Integer> numMap = new HashMap<>();
|
|
|
numMap.put('零', 0);
|
|
|
numMap.put('一', 1);
|
|
@@ -347,11 +835,19 @@ public class ConvertUtil {
|
|
|
numMap.put('七', 7);
|
|
|
numMap.put('八', 8);
|
|
|
numMap.put('九', 9);
|
|
|
-
|
|
|
+
|
|
|
+ // 单位映射
|
|
|
+ Map<Character, Integer> unitMap = new HashMap<>();
|
|
|
+ unitMap.put('十', 10);
|
|
|
+ unitMap.put('百', 100);
|
|
|
+ unitMap.put('千', 1000);
|
|
|
+ unitMap.put('万', 10000);
|
|
|
+ unitMap.put('亿', 100000000);
|
|
|
+
|
|
|
int result = 0;
|
|
|
int temp = 0;
|
|
|
int base = 0;
|
|
|
-
|
|
|
+
|
|
|
for (char c : chineseNumber.toCharArray()) {
|
|
|
if (numMap.containsKey(c)) {
|
|
|
temp = numMap.get(c);
|
|
@@ -369,18 +865,25 @@ public class ConvertUtil {
|
|
|
return result + temp;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 将数字转换为中文表示
|
|
|
+ *
|
|
|
+ * @param number 要转换的数字
|
|
|
+ * @param isMoney 是否为货币格式 (添加"元整"后缀)
|
|
|
+ * @return 中文数字字符串
|
|
|
+ */
|
|
|
public static String numberToChinese(double number, boolean isMoney) {
|
|
|
// 简化的实现
|
|
|
String[] units = {"", "十", "百", "千", "万", "十", "百", "千", "亿"};
|
|
|
String[] digits = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
|
|
|
-
|
|
|
+
|
|
|
String strNumber = String.valueOf((long) number);
|
|
|
StringBuilder result = new StringBuilder();
|
|
|
-
|
|
|
+
|
|
|
for (int i = 0; i < strNumber.length(); i++) {
|
|
|
int digit = Character.getNumericValue(strNumber.charAt(i));
|
|
|
int unitIndex = strNumber.length() - i - 1;
|
|
|
-
|
|
|
+
|
|
|
if (digit == 0) {
|
|
|
// 处理零的特殊情况
|
|
|
if (unitIndex % 4 == 0) {
|
|
@@ -390,26 +893,49 @@ public class ConvertUtil {
|
|
|
result.append(digits[digit]).append(units[unitIndex]);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
if (isMoney) {
|
|
|
result.append("元整");
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
return result.toString();
|
|
|
}
|
|
|
|
|
|
// =================== 其他转换 ===================
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 时间单位转换
|
|
|
+ *
|
|
|
+ * @param sourceTime 源时间值
|
|
|
+ * @param sourceUnit 源时间单位
|
|
|
+ * @param targetUnit 目标时间单位
|
|
|
+ * @return 转换后的时间值
|
|
|
+ */
|
|
|
public static long convertTime(long sourceTime, TimeUnit sourceUnit, TimeUnit targetUnit) {
|
|
|
return targetUnit.convert(sourceTime, sourceUnit);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 字符串字符集转换
|
|
|
+ *
|
|
|
+ * @param str 要转换的字符串
|
|
|
+ * @param sourceCharset 源字符集名称
|
|
|
+ * @param targetCharset 目标字符集名称
|
|
|
+ * @return 转换后的字符串
|
|
|
+ */
|
|
|
public static String convertCharset(String str, String sourceCharset, String targetCharset) {
|
|
|
- return new String(str.getBytes(Charset.forName(sourceCharset)),
|
|
|
- Charset.forName(targetCharset));
|
|
|
+ return new String(str.getBytes(Charset.forName(sourceCharset)),
|
|
|
+ Charset.forName(targetCharset));
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 字符串转换为Unicode编码
|
|
|
+ *
|
|
|
+ * @param str 要转换的字符串
|
|
|
+ * @return Unicode编码字符串 (格式: \u4e2d\u6587)
|
|
|
+ */
|
|
|
public static String strToUnicode(String str) {
|
|
|
+ if (str == null) return null;
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
for (char c : str.toCharArray()) {
|
|
|
sb.append("\\u").append(String.format("%04x", (int) c));
|
|
@@ -417,27 +943,98 @@ public class ConvertUtil {
|
|
|
return sb.toString();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Unicode编码转换为字符串
|
|
|
+ *
|
|
|
+ * @param unicode Unicode编码字符串 (格式: \u4e2d\u6587)
|
|
|
+ * @return 解码后的字符串
|
|
|
+ */
|
|
|
public static String unicodeToStr(String unicode) {
|
|
|
+ if (unicode == null) return null;
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
String[] hex = unicode.split("\\\\u");
|
|
|
for (int i = 1; i < hex.length; i++) {
|
|
|
- int data = Integer.parseInt(hex[i], 16);
|
|
|
- sb.append((char) data);
|
|
|
+ if (hex[i].isEmpty()) continue;
|
|
|
+ try {
|
|
|
+ int data = Integer.parseInt(hex[i].substring(0, 4), 16);
|
|
|
+ sb.append((char) data);
|
|
|
+ if (hex[i].length() > 4) {
|
|
|
+ sb.append(hex[i].substring(4));
|
|
|
+ }
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ sb.append("\\u").append(hex[i]);
|
|
|
+ }
|
|
|
}
|
|
|
return sb.toString();
|
|
|
}
|
|
|
|
|
|
// =================== 数组转换 ===================
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将对象转换为int数组
|
|
|
+ *
|
|
|
+ * <p>支持转换的类型:<br>
|
|
|
+ * - int[]<br>
|
|
|
+ * - Integer[]<br>
|
|
|
+ * - 数字集合</p>
|
|
|
+ *
|
|
|
+ * @param value 要转换的对象
|
|
|
+ * @return 转换后的int数组
|
|
|
+ */
|
|
|
public static int[] toIntArray(Object value) {
|
|
|
- // 实现各种类型到int数组的转换
|
|
|
+ if (value == null) return new int[0];
|
|
|
+ if (value instanceof int[]) {
|
|
|
+ return (int[]) value;
|
|
|
+ }
|
|
|
+ if (value instanceof Integer[]) {
|
|
|
+ Integer[] array = (Integer[]) value;
|
|
|
+ int[] result = new int[array.length];
|
|
|
+ for (int i = 0; i < array.length; i++) {
|
|
|
+ result[i] = array[i] != null ? array[i] : 0;
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ if (value instanceof List) {
|
|
|
+ List<?> list = (List<?>) value;
|
|
|
+ int[] result = new int[list.size()];
|
|
|
+ for (int i = 0; i < list.size(); i++) {
|
|
|
+ result[i] = toInt(list.get(i), 0);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
return new int[0];
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 将对象转换为String数组
|
|
|
+ *
|
|
|
+ * <p>支持转换的类型:<br>
|
|
|
+ * - String[]<br>
|
|
|
+ * - 任意对象数组<br>
|
|
|
+ * - 集合</p>
|
|
|
+ *
|
|
|
+ * @param value 要转换的对象
|
|
|
+ * @return 转换后的String数组
|
|
|
+ */
|
|
|
public static String[] toStrArray(Object value) {
|
|
|
- // 实现各种类型到字符串数组的转换
|
|
|
+ if (value == null) return new String[0];
|
|
|
+ if (value instanceof String[]) {
|
|
|
+ return (String[]) value;
|
|
|
+ }
|
|
|
+ if (value instanceof Object[]) {
|
|
|
+ Object[] array = (Object[]) value;
|
|
|
+ String[] result = new String[array.length];
|
|
|
+ for (int i = 0; i < array.length; i++) {
|
|
|
+ result[i] = array[i] != null ? array[i].toString() : null;
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ if (value instanceof List) {
|
|
|
+ List<?> list = (List<?>) value;
|
|
|
+ return list.stream()
|
|
|
+ .map(Object::toString)
|
|
|
+ .toArray(String[]::new);
|
|
|
+ }
|
|
|
return new String[0];
|
|
|
}
|
|
|
-
|
|
|
- // 其他数组类型转换(long, short, byte, char, float, double, boolean)实现类似
|
|
|
}
|