Class ConvertUtil
java.lang.Object
com.sure.tool.util.ConvertUtil
类型转换工具类,参考 Hutool 的
Convert 设计。
English: Best-effort type conversion between strings, numbers, booleans and enums.
- Since:
- 0.1.0
- Author:
- suretool
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> T通用类型转换:支持基础类型、包装类型、String 及数组/集合转 List。static BigDecimaltoBigDecimal(Object value) 转 BigDecimal。static BigIntegertoBigInteger(Object value) 转换为 BigInteger。static boolean转 boolean。static boolean转 boolean。static boolean[]toBooleanArray(Object value) 转换为 boolean 数组(集合/数组/逗号分隔字符串)。static byte转 byte。static byte转 byte。static byte[]toByteArray(Object value) 数组或集合转 byte 数组。static char[]toCharArray(Object value) 转字符数组。static Date转 Date(支持 yyyy-MM-dd、yyyy-MM-dd HH:mm:ss、时间戳)。static double转 double。static double转 double。static double[]toDoubleArray(Object value) 转换为 double 数组(集合/数组/逗号分隔字符串)。static <E extends Enum<E>>
E字符串转枚举。static float转 float。static float转 float。static int转 int。static int转 int。static int[]toIntArray(Object value) 数组或集合转 int 数组。数组或集合转 List。static LocalDatetoLocalDate(Object value) 转换为 LocalDate(支持 yyyy-MM-dd / ISO_INSTANT 等标准格式)。static LocalDateTimetoLocalDateTime(Object value) 转换为 LocalDateTime(支持 yyyy-MM-dd HH:mm:ss 等标准格式)。static long转 long。static long转 long。static long[]toLongArray(Object value) 数组或集合转 long 数组。键值数组转为 Map(偶数个元素两两成键值对)。toMap(Collection<?> collection, String keyFieldName) 按 Bean 属性字段将集合转为 Map。数组或集合转 Set(去重,保序)。static short转 short。static short转 short。static Date转Date。static String转字符串。static String转字符串。static String[]toStrArray(Object value) 数组或集合转 String 数组。static TimestamptoTimestamp(Object value) 转Timestamp。static Object[]对象数组(含基本类型数组)装箱为包装类型数组。
-
Method Details
-
toInt
-
toInt
转 int。- Parameters:
value- 值defaultValue- 默认值- Returns:
- int 值或默认值
-
toLong
-
toLong
转 long。- Parameters:
value- 值defaultValue- 默认值- Returns:
- long 值或默认值
-
toDouble
转 double。- Parameters:
value- 值- Returns:
- double 值,失败返回 0
-
toDouble
转 double。- Parameters:
value- 值defaultValue- 默认值- Returns:
- double 值或默认值
-
toFloat
转 float。- Parameters:
value- 值defaultValue- 默认值- Returns:
- float 值或默认值
-
toBoolean
转 boolean。- Parameters:
value- 值- Returns:
- boolean 值,失败返回
false
-
toBoolean
转 boolean。- Parameters:
value- 值defaultValue- 默认值- Returns:
- boolean 值或默认值
-
toStr
-
toStr
-
convert
-
toList
-
toStrArray
-
toByteArray
数组或集合转 byte 数组。- Parameters:
value- 数组(含基本类型数组)或集合- Returns:
- byte 数组
-
toSet
-
toIntArray
数组或集合转 int 数组。- Parameters:
value- 数组(含基本类型数组)或集合- Returns:
- int 数组
-
toLongArray
数组或集合转 long 数组。- Parameters:
value- 数组(含基本类型数组)或集合- Returns:
- long 数组
-
wrap
-
toBigDecimal
转 BigDecimal。- Parameters:
value- 值- Returns:
- BigDecimal;null 或转换失败返回 null
-
toDate
-
toSqlDate
-
toTimestamp
-
toShort
-
toShort
转 short。- Parameters:
value- 值defaultValue- 默认值- Returns:
- short;转换失败返回默认值
-
toByte
-
toByte
转 byte。- Parameters:
value- 值defaultValue- 默认值- Returns:
- byte;转换失败返回默认值
-
toFloat
-
toCharArray
-
toEnum
-
toDoubleArray
转换为 double 数组(集合/数组/逗号分隔字符串)。- Parameters:
value- 值- Returns:
- double 数组;null 返回空数组
-
toBooleanArray
转换为 boolean 数组(集合/数组/逗号分隔字符串)。- Parameters:
value- 值- Returns:
- boolean 数组;null 返回空数组
-
toBigInteger
转换为 BigInteger。- Parameters:
value- 值- Returns:
- BigInteger 或
null
-
toLocalDate
-
toLocalDateTime
转换为 LocalDateTime(支持 yyyy-MM-dd HH:mm:ss 等标准格式)。- Parameters:
value- 值- Returns:
- LocalDateTime 或
null
-
toMap
-
toMap
按 Bean 属性字段将集合转为 Map。- Parameters:
collection- 对象集合keyFieldName- 作为 key 的字段名- Returns:
- Map(字段值 → 元素);字段值取不到则抛异常
-