|
@@ -0,0 +1,443 @@
|
|
|
|
+package nckd.jxcc.base.common.utils;
|
|
|
|
+
|
|
|
|
+import java.lang.reflect.Array;
|
|
|
|
+import java.math.BigDecimal;
|
|
|
|
+import java.math.BigInteger;
|
|
|
|
+import java.nio.charset.Charset;
|
|
|
|
+import java.time.LocalDateTime;
|
|
|
|
+import java.time.ZoneId;
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.Collections;
|
|
|
|
+import java.util.Date;
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+* 类型转换器
|
|
|
|
+* @author W.Y.C
|
|
|
|
+* @date 2025/6/11 14:02
|
|
|
|
+* @version 1.0
|
|
|
|
+*/
|
|
|
|
+public class ConvertUtil {
|
|
|
|
+
|
|
|
|
+ // =================== 基础类型转换 ===================
|
|
|
|
+
|
|
|
|
+ public static byte[] intToBytes(int value) {
|
|
|
|
+ return new byte[] {
|
|
|
|
+ (byte)(value >> 24),
|
|
|
|
+ (byte)(value >> 16),
|
|
|
|
+ (byte)(value >> 8),
|
|
|
|
+ (byte)value
|
|
|
|
+ };
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static int bytesToInt(byte[] bytes) {
|
|
|
|
+ return ((bytes[0] & 0xFF) << 24) |
|
|
|
|
+ ((bytes[1] & 0xFF) << 16) |
|
|
|
|
+ ((bytes[2] & 0xFF) << 8) |
|
|
|
|
+ (bytes[3] & 0xFF);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static byte[] longToBytes(long value) {
|
|
|
|
+ byte[] result = new byte[8];
|
|
|
|
+ for (int i = 7; i >= 0; i--) {
|
|
|
|
+ result[i] = (byte)(value & 0xFF);
|
|
|
|
+ value >>= 8;
|
|
|
|
+ }
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static long bytesToLong(byte[] bytes) {
|
|
|
|
+ long value = 0;
|
|
|
|
+ for (byte b : bytes) {
|
|
|
|
+ value = (value << 8) + (b & 0xFF);
|
|
|
|
+ }
|
|
|
|
+ return value;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static byte[] shortToBytes(short value) {
|
|
|
|
+ return new byte[] {
|
|
|
|
+ (byte)(value >> 8),
|
|
|
|
+ (byte)value
|
|
|
|
+ };
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static short bytesToShort(byte[] bytes) {
|
|
|
|
+ return (short)(((bytes[0] & 0xFF) << 8) | (bytes[1] & 0xFF));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static int byteToUnsignedInt(byte b) {
|
|
|
|
+ return b & 0xFF;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // =================== 十六进制转换 ===================
|
|
|
|
+
|
|
|
|
+ public static String toHex(byte[] bytes) {
|
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
|
+ for (byte b : bytes) {
|
|
|
|
+ sb.append(String.format("%02x", b));
|
|
|
|
+ }
|
|
|
|
+ return sb.toString();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ 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));
|
|
|
|
+ }
|
|
|
|
+ return data;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static String hexToString(String hex, Charset charset) {
|
|
|
|
+ return new String(hexToBytes(hex), charset);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // =================== 对象到基础类型转换 ===================
|
|
|
|
+
|
|
|
|
+ public static Byte toByte(Object value) {
|
|
|
|
+ return toByte(value, (Byte) null);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static Byte toByte(Object value, Byte defaultValue) {
|
|
|
|
+ if (value == null) {return defaultValue;};
|
|
|
|
+ if (value instanceof Number) {
|
|
|
|
+ return ((Number) value).byteValue();
|
|
|
|
+ }
|
|
|
|
+ try {
|
|
|
|
+ return Byte.parseByte(value.toString());
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static byte[] toPrimitiveByteArray(Object value) {
|
|
|
|
+ if (value instanceof byte[]) {
|
|
|
|
+ return (byte[]) value;
|
|
|
|
+ }
|
|
|
|
+ if (value instanceof Byte[]) {
|
|
|
|
+ Byte[] bytes = (Byte[]) value;
|
|
|
|
+ byte[] result = new byte[bytes.length];
|
|
|
|
+ for (int i = 0; i < bytes.length; i++) {
|
|
|
|
+ result[i] = bytes[i];
|
|
|
|
+ }
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+ return new byte[0];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static Integer toInt(Object value) {
|
|
|
|
+ return toInt(value, null);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static Integer toInt(Object value, Integer defaultValue) {
|
|
|
|
+ if (value == null) {return defaultValue;};
|
|
|
|
+ if (value instanceof Number) {
|
|
|
|
+ return ((Number) value).intValue();
|
|
|
|
+ }
|
|
|
|
+ try {
|
|
|
|
+ return Integer.parseInt(value.toString().trim());
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 其他基本类型转换(Long, Short, Float, Double, Boolean, Character)实现类似
|
|
|
|
+ // 为节省篇幅,这里省略具体实现,实际使用时需补充完整
|
|
|
|
+
|
|
|
|
+ // =================== 数字转换 ===================
|
|
|
|
+
|
|
|
|
+ public static BigDecimal toBigDecimal(Object value) {
|
|
|
|
+ return toBigDecimal(value, null);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static BigDecimal toBigDecimal(Object value, BigDecimal defaultValue) {
|
|
|
|
+ if (value == null) {return defaultValue;};
|
|
|
|
+ if (value instanceof BigDecimal) {
|
|
|
|
+ return (BigDecimal) value;
|
|
|
|
+ }
|
|
|
|
+ if (value instanceof BigInteger) {
|
|
|
|
+ return new BigDecimal((BigInteger) value);
|
|
|
|
+ }
|
|
|
|
+ try {
|
|
|
|
+ return new BigDecimal(value.toString().trim());
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static BigInteger toBigInteger(Object value) {
|
|
|
|
+ return toBigInteger(value, null);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static BigInteger toBigInteger(Object value, BigInteger defaultValue) {
|
|
|
|
+ if (value == null) {return defaultValue;};
|
|
|
|
+ if (value instanceof BigInteger) {
|
|
|
|
+ return (BigInteger) value;
|
|
|
|
+ }
|
|
|
|
+ try {
|
|
|
|
+ return new BigInteger(value.toString().trim());
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // =================== 字符串转换 ===================
|
|
|
|
+
|
|
|
|
+ public static String toStr(Object value) {
|
|
|
|
+ return toStr(value, null);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static String toStr(Object value, String defaultValue) {
|
|
|
|
+ if (value == null) {return defaultValue;};
|
|
|
|
+ if (value instanceof String) {
|
|
|
|
+ return (String) value;
|
|
|
|
+ }
|
|
|
|
+ return value.toString();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // =================== 日期时间转换 ===================
|
|
|
|
+
|
|
|
|
+ public static Date toDate(Object value) {
|
|
|
|
+ return toDate(value, null);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static Date toDate(Object value, Date defaultValue) {
|
|
|
|
+ if (value == null) {
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
|
|
+ if (value instanceof Date) {
|
|
|
|
+ return (Date) value;
|
|
|
|
+ }
|
|
|
|
+ if (value instanceof LocalDateTime) {
|
|
|
|
+ LocalDateTime ldtValue = (LocalDateTime) value;
|
|
|
|
+ return Date.from(ldtValue.atZone(ZoneId.systemDefault()).toInstant());
|
|
|
|
+ }
|
|
|
|
+ // 可扩展:支持字符串解析等其他类型
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static LocalDateTime toLocalDateTime(Object value) {
|
|
|
|
+ return toLocalDateTime(value, null);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static LocalDateTime toLocalDateTime(Object value, LocalDateTime defaultValue) {
|
|
|
|
+ if (value == null) {
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
|
|
+ if (value instanceof LocalDateTime) {
|
|
|
|
+ return (LocalDateTime) value;
|
|
|
|
+ }
|
|
|
|
+ if (value instanceof Date) {
|
|
|
|
+ Date dateValue = (Date) value;
|
|
|
|
+ return dateValue.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
|
|
|
|
+ }
|
|
|
|
+ // 可扩展:支持字符串解析等其他类型
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // =================== 集合转换 ===================
|
|
|
|
+
|
|
|
|
+ public static <T> List<T> toList(Object value) {
|
|
|
|
+ return toList(value, ArrayList::new);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static <T> List<T> toList(Object value, java.util.function.Supplier<List<T>> supplier) {
|
|
|
|
+ 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()) {
|
|
|
|
+ int length = Array.getLength(value);
|
|
|
|
+ for (int i = 0; i < length; i++) {
|
|
|
|
+ result.add((T) Array.get(value, i));
|
|
|
|
+ }
|
|
|
|
+ } else if (value instanceof Map) {
|
|
|
|
+ for (Map.Entry<?, ?> entry : ((Map<?, ?>) value).entrySet()) {
|
|
|
|
+ result.add((T) entry);
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ result.add((T) value);
|
|
|
|
+ }
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static <K, V> Map<K, V> toMap(Object value) {
|
|
|
|
+ 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());
|
|
|
|
+ }
|
|
|
|
+ } else if (value instanceof Iterable) {
|
|
|
|
+ for (Object item : (Iterable<?>) value) {
|
|
|
|
+ if (item instanceof Map.Entry) {
|
|
|
|
+ Map.Entry<?, ?> entry = (Map.Entry<?, ?>) item;
|
|
|
|
+ result.put((K) entry.getKey(), (V) entry.getValue());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } else if (value.getClass().isArray()) {
|
|
|
|
+ // 处理数组类型的转换
|
|
|
|
+ }
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // =================== 枚举转换 ===================
|
|
|
|
+
|
|
|
|
+ public static <E extends Enum<E>> E toEnum(Class<E> enumClass, Object value) {
|
|
|
|
+ return toEnum(enumClass, value, null);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static <E extends Enum<E>> E toEnum(Class<E> enumClass, Object value, E defaultValue) {
|
|
|
|
+ if (value == null) {return defaultValue;};
|
|
|
|
+
|
|
|
|
+ // 尝试通过名称匹配
|
|
|
|
+ if (value instanceof String) {
|
|
|
|
+ try {
|
|
|
|
+ return Enum.valueOf(enumClass, (String) value);
|
|
|
|
+ } catch (IllegalArgumentException e) {
|
|
|
|
+ // 尝试忽略大小写匹配
|
|
|
|
+ for (E constant : enumClass.getEnumConstants()) {
|
|
|
|
+ if (constant.name().equalsIgnoreCase((String) value)) {
|
|
|
|
+ return constant;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 尝试通过ordinal匹配
|
|
|
|
+ if (value instanceof Number) {
|
|
|
|
+ int ordinal = ((Number) value).intValue();
|
|
|
|
+ E[] constants = enumClass.getEnumConstants();
|
|
|
|
+ if (ordinal >= 0 && ordinal < constants.length) {
|
|
|
|
+ return constants[ordinal];
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // =================== 中文数字转换 ===================
|
|
|
|
+
|
|
|
|
+ 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);
|
|
|
|
+
|
|
|
|
+ Map<Character, Integer> numMap = new HashMap<>();
|
|
|
|
+ numMap.put('零', 0);
|
|
|
|
+ numMap.put('一', 1);
|
|
|
|
+ numMap.put('二', 2);
|
|
|
|
+ numMap.put('三', 3);
|
|
|
|
+ numMap.put('四', 4);
|
|
|
|
+ numMap.put('五', 5);
|
|
|
|
+ numMap.put('六', 6);
|
|
|
|
+ numMap.put('七', 7);
|
|
|
|
+ numMap.put('八', 8);
|
|
|
|
+ numMap.put('九', 9);
|
|
|
|
+
|
|
|
|
+ int result = 0;
|
|
|
|
+ int temp = 0;
|
|
|
|
+ int base = 0;
|
|
|
|
+
|
|
|
|
+ for (char c : chineseNumber.toCharArray()) {
|
|
|
|
+ if (numMap.containsKey(c)) {
|
|
|
|
+ temp = numMap.get(c);
|
|
|
|
+ } else if (unitMap.containsKey(c)) {
|
|
|
|
+ int unit = unitMap.get(c);
|
|
|
|
+ if (unit > base) {
|
|
|
|
+ result = (result + temp) * unit;
|
|
|
|
+ base = unit;
|
|
|
|
+ } else {
|
|
|
|
+ result += temp * unit;
|
|
|
|
+ }
|
|
|
|
+ temp = 0;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return result + temp;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ 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) {
|
|
|
|
+ result.append(units[unitIndex]);
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ result.append(digits[digit]).append(units[unitIndex]);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (isMoney) {
|
|
|
|
+ result.append("元整");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return result.toString();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // =================== 其他转换 ===================
|
|
|
|
+
|
|
|
|
+ public static long convertTime(long sourceTime, TimeUnit sourceUnit, TimeUnit targetUnit) {
|
|
|
|
+ return targetUnit.convert(sourceTime, sourceUnit);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static String convertCharset(String str, String sourceCharset, String targetCharset) {
|
|
|
|
+ return new String(str.getBytes(Charset.forName(sourceCharset)),
|
|
|
|
+ Charset.forName(targetCharset));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static String strToUnicode(String str) {
|
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
|
+ for (char c : str.toCharArray()) {
|
|
|
|
+ sb.append("\\u").append(String.format("%04x", (int) c));
|
|
|
|
+ }
|
|
|
|
+ return sb.toString();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static String unicodeToStr(String unicode) {
|
|
|
|
+ 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);
|
|
|
|
+ }
|
|
|
|
+ return sb.toString();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // =================== 数组转换 ===================
|
|
|
|
+
|
|
|
|
+ public static int[] toIntArray(Object value) {
|
|
|
|
+ // 实现各种类型到int数组的转换
|
|
|
|
+ return new int[0];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static String[] toStrArray(Object value) {
|
|
|
|
+ // 实现各种类型到字符串数组的转换
|
|
|
|
+ return new String[0];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 其他数组类型转换(long, short, byte, char, float, double, boolean)实现类似
|
|
|
|
+}
|