Package org.aoju.bus.core.convert
Class Convert
java.lang.Object
org.aoju.bus.core.convert.Convert
类型转换器
- Since:
- Java 17+
- Author:
- Kimi Liu
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic intbytesToInt(byte[] bytes) byte[]转int值static longbytesToLong(byte[] bytes) byte数组转longstatic shortbytesToShort(byte[] bytes) byte数组转shortstatic intbyteToUnsignedInt(byte byteValue) byte转无符号intstatic BigDecimalchineseMoneyToNumber(String chineseMoneyAmount) 中文大写数字金额转换为数字,返回结果以元为单位的BigDecimal类型数字static intchineseToNumber(String number) 数字中文表示形式转数字 一百一十二 -》 112 一千零一十二 -》 1012static <T> T转换值为指定类型static <T> T转换值为指定类型static <T> T转换值为指定类型static <T> T转换值为指定类型static <T> T转换值为指定类型static <T> TconvertByClassName(String className, Object value) 转换值为指定类型,类型采用字符串表示static StringconvertCharset(String text, String sourceCharset, String destCharset) 给定字符串转换字符编码 如果参数为空,则返回原字符串,不报错static <T> TconvertQuietly(Type type, Object value) 转换值为指定类型,不抛异常转换 当转换失败时返回nullstatic <T> TconvertQuietly(Type type, Object value, T defaultValue) 转换值为指定类型,不抛异常转换 当转换失败时返回默认值static longconvertTime(long sourceDuration, TimeUnit sourceUnit, TimeUnit destUnit) 转换时间单位static <T> TconvertWithCheck(Type type, Object value, T defaultValue, boolean quietly) 转换值为指定类型,可选是否不抛异常转换 当转换失败时返回默认值static String金额转为中文形式static byte[]hexToBytes(String src) Hex字符串转换为Byte值static StringhexToString(String text, Charset charset) 十六进制转换字符串static byteintToByte(int intValue) int转bytestatic byte[]intToBytes(int intValue) int转byte数组static byte[]longToBytes(long longValue) long转byte数组static StringnumberToChinese(double number, boolean isUseTraditional) 将阿拉伯数字转为中文表达方式static StringnumberToSimple(Number number) 将阿拉伯数字转为精简表示形式,例如:static StringnumberToWord(Number number) 将阿拉伯数字转为英文表达方式static byte[]shortToBytes(short shortValue) short转byte数组static BigDecimaltoBigDecimal(Object value) 转换为BigDecimal 如果给定的值为空,或者转换失败,返回null 转换失败不会报错static BigDecimaltoBigDecimal(Object value, BigDecimal defaultValue) 转换为BigDecimal 如果给定的值为空,或者转换失败,返回默认值 转换失败不会报错static BigIntegertoBigInteger(Object value) 转换为BigInteger 如果给定的值为空,或者转换失败,返回默认值null转换失败不会报错static BigIntegertoBigInteger(Object value, BigInteger defaultValue) 转换为BigInteger 如果给定的值为空,或者转换失败,返回默认值 转换失败不会报错static Boolean转换为boolean 如果给定的值为空,或者转换失败,返回默认值null转换失败不会报错static Boolean转换为boolean String支持的值为:true、false、yes、ok、no,1,0 如果给定的值为空,或者转换失败,返回默认值 转换失败不会报错static Boolean[]toBooleanArray(Object value) 转换为Boolean数组static Byte转换为byte 如果给定的值为null,或者转换失败,返回默认值null转换失败不会报错static Byte转换为byte 如果给定的值为null,或者转换失败,返回默认值 转换失败不会报错static Byte[]toByteArray(Object value) 转换为Byte数组static Character转换为字符 如果给定的值为null,或者转换失败,返回默认值null转换失败不会报错static Character转换为字符 如果给定的值为null,或者转换失败,返回默认值 转换失败不会报错static Character[]toCharArray(Object value) 转换为Character数组static Collection<?>toCollection(Class<?> collectionType, Class<?> elementType, Object value) 转换为集合类static Date转换为Date 如果给定的值为空,或者转换失败,返回null转换失败不会报错static Date转换为Date 如果给定的值为空,或者转换失败,返回默认值 转换失败不会报错static String全角转半角static String替换全角为半角static Double转换为double 如果给定的值为空,或者转换失败,返回默认值null转换失败不会报错static Double转换为double 如果给定的值为空,或者转换失败,返回默认值 转换失败不会报错static Double[]toDoubleArray(Object value) 转换为Double数组static <E extends Enum<E>>
E转换为Enum对象 如果给定的值为空,或者转换失败,返回默认值nullstatic <E extends Enum<E>>
E转换为Enum对象 如果给定的值为空,或者转换失败,返回默认值static Float转换为Float 如果给定的值为空,或者转换失败,返回默认值null转换失败不会报错static Float转换为Float 如果给定的值为空,或者转换失败,返回默认值 转换失败不会报错static Float[]toFloatArray(Object value) 转换为Float数组static StringtoHex(byte[] bytes) byte数组转16进制串static String字符串转换成十六进制字符串,结果为小写static DateInstant 如果给定的值为空,或者转换失败,返回默认值 转换失败不会报错static Integer转换为int 如果给定的值为null,或者转换失败,返回默认值null转换失败不会报错static Integer转换为int 如果给定的值为空,或者转换失败,返回默认值 转换失败不会报错static Integer[]toIntArray(Object value) 转换为Integer数组static <T> List<T>转换为ArrayListstatic List<?>转换为ArrayList,元素类型默认Objectstatic LocalDateTimetoLocalDateTime(Object value) 如果给定的值为空,或者转换失败,返回默认值 转换失败不会报错static LocalDateTimetoLocalDateTime(Object value, LocalDateTime defaultValue) LocalDateTime 如果给定的值为空,或者转换失败,返回默认值 转换失败不会报错static Long转换为long 如果给定的值为null,或者转换失败,返回默认值null转换失败不会报错static Long转换为long 如果给定的值为空,或者转换失败,返回默认值 转换失败不会报错static Long[]toLongArray(Object value) 转换为Long数组static <K,V> Map<K, V> 转换为Mapstatic <K,V> Map<K, V> 转换为Mapstatic Number转换为Number 如果给定的值为空,或者转换失败,返回默认值null转换失败不会报错static Number转换为Number 如果给定的值为空,或者转换失败,返回默认值 转换失败不会报错static Number[]toNumberArray(Object value) 转换为Number数组static byte[]toPrimitiveByteArray(Object value) 转换为Byte数组static String半角转全角static String半角转全角static <T> Set<T>转换为HashSetstatic Short转换为Short 如果给定的值为null,或者转换失败,返回默认值null转换失败不会报错static Short转换为Short 如果给定的值为null,或者转换失败,返回默认值 转换失败不会报错static Short[]toShortArray(Object value) 转换为Short数组static String[]toStrArray(Object value) 转换为String数组static String转换为字符串 如果给定的值为null,或者转换失败,返回默认值null转换失败不会报错static String转换为字符串 如果给定的值为null,或者转换失败,返回默认值 转换失败不会报错static StringString的字符串转换成unicode的Stringstatic StringtoUnicodeString(String text) unicode的String转换成String的字符串static Class<?>包装类转为原始类,非包装类返回原类static Class<?>原始类转为包装类,非原始类返回原类
-
Constructor Details
-
Convert
public Convert()
-
-
Method Details
-
toString
转换为字符串 如果给定的值为null,或者转换失败,返回默认值null转换失败不会报错- Parameters:
value- 被转换的值- Returns:
- 结果
-
toString
转换为字符串 如果给定的值为null,或者转换失败,返回默认值 转换失败不会报错- Parameters:
value- 被转换的值defaultValue- 转换错误时的默认值- Returns:
- 结果
-
toStrArray
转换为String数组- Parameters:
value- 被转换的值- Returns:
- String数组
-
toChar
转换为字符 如果给定的值为null,或者转换失败,返回默认值null转换失败不会报错- Parameters:
value- 被转换的值- Returns:
- 结果
-
toChar
转换为字符 如果给定的值为null,或者转换失败,返回默认值 转换失败不会报错- Parameters:
value- 被转换的值defaultValue- 转换错误时的默认值- Returns:
- 结果
-
toCharArray
转换为Character数组- Parameters:
value- 被转换的值- Returns:
- Character数组
-
toByte
转换为byte 如果给定的值为null,或者转换失败,返回默认值null转换失败不会报错- Parameters:
value- 被转换的值- Returns:
- 结果
-
toByte
转换为byte 如果给定的值为null,或者转换失败,返回默认值 转换失败不会报错- Parameters:
value- 被转换的值defaultValue- 转换错误时的默认值- Returns:
- 结果
-
toByteArray
转换为Byte数组- Parameters:
value- 被转换的值- Returns:
- Byte数组
-
toPrimitiveByteArray
转换为Byte数组- Parameters:
value- 被转换的值- Returns:
- Byte数组
-
toShort
转换为Short 如果给定的值为null,或者转换失败,返回默认值null转换失败不会报错- Parameters:
value- 被转换的值- Returns:
- 结果
-
toShort
转换为Short 如果给定的值为null,或者转换失败,返回默认值 转换失败不会报错- Parameters:
value- 被转换的值defaultValue- 转换错误时的默认值- Returns:
- 结果
-
toShortArray
转换为Short数组- Parameters:
value- 被转换的值- Returns:
- Short数组
-
toNumber
转换为Number 如果给定的值为空,或者转换失败,返回默认值null转换失败不会报错- Parameters:
value- 被转换的值- Returns:
- 结果
-
toNumber
转换为Number 如果给定的值为空,或者转换失败,返回默认值 转换失败不会报错- Parameters:
value- 被转换的值defaultValue- 转换错误时的默认值- Returns:
- 结果
-
toNumberArray
转换为Number数组- Parameters:
value- 被转换的值- Returns:
- Number数组
-
toInt
转换为int 如果给定的值为null,或者转换失败,返回默认值null转换失败不会报错- Parameters:
value- 被转换的值- Returns:
- 结果
-
toInt
转换为int 如果给定的值为空,或者转换失败,返回默认值 转换失败不会报错- Parameters:
value- 被转换的值defaultValue- 转换错误时的默认值- Returns:
- 结果
-
toIntArray
转换为Integer数组- Parameters:
value- 被转换的值- Returns:
- 结果
-
toLong
转换为long 如果给定的值为null,或者转换失败,返回默认值null转换失败不会报错- Parameters:
value- 被转换的值- Returns:
- 结果
-
toLong
转换为long 如果给定的值为空,或者转换失败,返回默认值 转换失败不会报错- Parameters:
value- 被转换的值defaultValue- 转换错误时的默认值- Returns:
- 结果
-
toLongArray
转换为Long数组- Parameters:
value- 被转换的值- Returns:
- 结果
-
toDouble
转换为double 如果给定的值为空,或者转换失败,返回默认值null转换失败不会报错- Parameters:
value- 被转换的值- Returns:
- 结果
-
toDouble
转换为double 如果给定的值为空,或者转换失败,返回默认值 转换失败不会报错- Parameters:
value- 被转换的值defaultValue- 转换错误时的默认值- Returns:
- 结果
-
toDoubleArray
转换为Double数组- Parameters:
value- 被转换的值- Returns:
- 结果
-
toFloat
转换为Float 如果给定的值为空,或者转换失败,返回默认值null转换失败不会报错- Parameters:
value- 被转换的值- Returns:
- 结果
-
toFloat
转换为Float 如果给定的值为空,或者转换失败,返回默认值 转换失败不会报错- Parameters:
value- 被转换的值defaultValue- 转换错误时的默认值- Returns:
- 结果
-
toFloatArray
转换为Float数组- Parameters:
value- 被转换的值- Returns:
- 结果
-
toBoolean
转换为boolean 如果给定的值为空,或者转换失败,返回默认值null转换失败不会报错- Parameters:
value- 被转换的值- Returns:
- 结果
-
toBoolean
转换为boolean String支持的值为:true、false、yes、ok、no,1,0 如果给定的值为空,或者转换失败,返回默认值 转换失败不会报错- Parameters:
value- 被转换的值defaultValue- 转换错误时的默认值- Returns:
- 结果
-
toBooleanArray
转换为Boolean数组- Parameters:
value- 被转换的值- Returns:
- 结果
-
toBigInteger
转换为BigInteger 如果给定的值为空,或者转换失败,返回默认值null转换失败不会报错- Parameters:
value- 被转换的值- Returns:
- 结果
-
toBigInteger
转换为BigInteger 如果给定的值为空,或者转换失败,返回默认值 转换失败不会报错- Parameters:
value- 被转换的值defaultValue- 转换错误时的默认值- Returns:
- 结果
-
toBigDecimal
转换为BigDecimal 如果给定的值为空,或者转换失败,返回null 转换失败不会报错- Parameters:
value- 被转换的值- Returns:
- 结果
-
toBigDecimal
转换为BigDecimal 如果给定的值为空,或者转换失败,返回默认值 转换失败不会报错- Parameters:
value- 被转换的值defaultValue- 转换错误时的默认值- Returns:
- 结果
-
toDate
转换为Date 如果给定的值为空,或者转换失败,返回null转换失败不会报错- Parameters:
value- 被转换的值- Returns:
- 结果
-
toDate
转换为Date 如果给定的值为空,或者转换失败,返回默认值 转换失败不会报错- Parameters:
value- 被转换的值defaultValue- 转换错误时的默认值- Returns:
- 结果
-
toLocalDateTime
如果给定的值为空,或者转换失败,返回默认值 转换失败不会报错- Parameters:
value- 被转换的值- Returns:
- 结果
-
toLocalDateTime
LocalDateTime 如果给定的值为空,或者转换失败,返回默认值 转换失败不会报错- Parameters:
value- 被转换的值defaultValue- 转换错误时的默认值- Returns:
- 结果
-
toInstant
Instant 如果给定的值为空,或者转换失败,返回默认值 转换失败不会报错- Parameters:
value- 被转换的值defaultValue- 转换错误时的默认值- Returns:
- 结果
-
toEnum
转换为Enum对象 如果给定的值为空,或者转换失败,返回默认值null- Type Parameters:
E- 枚举类型- Parameters:
clazz- Enum的Classvalue- 值- Returns:
- Enum
-
toEnum
转换为Enum对象 如果给定的值为空,或者转换失败,返回默认值- Type Parameters:
E- 枚举类型- Parameters:
clazz- Enum的Classvalue- 值defaultValue- 默认值- Returns:
- Enum
-
toCollection
public static Collection<?> toCollection(Class<?> collectionType, Class<?> elementType, Object value) 转换为集合类- Parameters:
collectionType- 集合类型elementType- 集合中元素类型value- 被转换的值- Returns:
Collection
-
toList
转换为ArrayList,元素类型默认Object- Parameters:
value- 被转换的值- Returns:
List
-
toList
转换为ArrayList- Type Parameters:
T- 元素类型- Parameters:
elementType- 集合中元素类型value- 被转换的值- Returns:
List
-
toSet
转换为HashSet- Type Parameters:
T- 元素类型- Parameters:
elementType- 集合中元素类型value- 被转换的值- Returns:
HashSet
-
toMap
转换为Map- Type Parameters:
K- 键类型V- 值类型- Parameters:
keyType- 键类型valueType- 值类型value- 被转换的值- Returns:
Map
-
toMap
public static <K,V> Map<K,V> toMap(Class<? extends Map> mapType, Class<K> keyType, Class<V> valueType, Object value) 转换为Map- Type Parameters:
K- 键类型V- 值类型- Parameters:
mapType- 转后的具体Map类型keyType- 键类型valueType- 值类型value- 被转换的值- Returns:
Map
-
convert
转换值为指定类型- Type Parameters:
T- 目标类型- Parameters:
type- 类型value- 值- Returns:
- 转换后的值
- Throws:
ConvertException- 转换器不存在
-
convert
转换值为指定类型- Type Parameters:
T- 目标类型- Parameters:
reference- 类型参考,用于持有转换后的泛型类型value- 值- Returns:
- 转换后的值
- Throws:
ConvertException- 转换器不存在
-
convert
转换值为指定类型- Type Parameters:
T- 目标类型- Parameters:
type- 类型value- 值- Returns:
- 转换后的值
- Throws:
ConvertException- 转换器不存在
-
convert
转换值为指定类型- Type Parameters:
T- 目标类型- Parameters:
type- 类型value- 值defaultValue- 默认值- Returns:
- 转换后的值
- Throws:
ConvertException- 转换器不存在
-
convert
转换值为指定类型- Type Parameters:
T- 目标类型- Parameters:
type- 类型value- 值defaultValue- 默认值- Returns:
- 转换后的值
- Throws:
ConvertException- 转换器不存在
-
convertQuietly
转换值为指定类型,不抛异常转换 当转换失败时返回null- Type Parameters:
T- 目标类型- Parameters:
type- 目标类型value- 值- Returns:
- 转换后的值,转换失败返回null
-
convertQuietly
转换值为指定类型,不抛异常转换 当转换失败时返回默认值- Type Parameters:
T- 目标类型- Parameters:
type- 目标类型value- 值defaultValue- 默认值- Returns:
- 转换后的值
-
convertWithCheck
转换值为指定类型,可选是否不抛异常转换 当转换失败时返回默认值- Type Parameters:
T- 目标类型- Parameters:
type- 目标类型value- 值defaultValue- 默认值quietly- 是否静默转换,true不抛异常- Returns:
- 转换后的值
-
convertByClassName
转换值为指定类型,类型采用字符串表示- Type Parameters:
T- 目标类型- Parameters:
className- 类的字符串表示value- 值- Returns:
- 转换后的值
- Throws:
ConvertException- 转换器不存在
-
convertCharset
给定字符串转换字符编码 如果参数为空,则返回原字符串,不报错- Parameters:
text- 被转码的字符串sourceCharset- 原字符集destCharset- 目标字符集- Returns:
- 转换后的字符串
- See Also:
-
convertTime
转换时间单位- Parameters:
sourceDuration- 时长sourceUnit- 源单位destUnit- 目标单位- Returns:
- 目标单位的时长
-
toSBC
半角转全角- Parameters:
input- String.- Returns:
- 全角字符串.
-
toSBC
半角转全角- Parameters:
input- StringnotConvertSet- 不替换的字符集合- Returns:
- 全角字符串.
-
toDBC
全角转半角- Parameters:
input- String.- Returns:
- 半角字符串
-
toDBC
替换全角为半角- Parameters:
text- 文本notConvertSet- 不替换的字符集合- Returns:
- 替换后的字符
-
toHex
byte数组转16进制串- Parameters:
bytes- 被转换的byte数组- Returns:
- 转换后的值
- See Also:
-
toHex
字符串转换成十六进制字符串,结果为小写- Parameters:
text- 待转换的ASCII字符串charset- 编码- Returns:
- 16进制字符串
- See Also:
-
hexToBytes
Hex字符串转换为Byte值- Parameters:
src- Byte字符串,每个Byte之间没有分隔符- Returns:
- byte[]
- See Also:
-
hexToString
十六进制转换字符串- Parameters:
text- Byte字符串(Byte之间无分隔符 如:[616C6B])charset- 编码Charset- Returns:
- 对应的字符串
- See Also:
-
toUnicode
String的字符串转换成unicode的String- Parameters:
text- 全角字符串- Returns:
- String 每个unicode之间无分隔符
- See Also:
-
toUnicodeString
unicode的String转换成String的字符串- Parameters:
text- Unicode符- Returns:
- String 字符串
- See Also:
-
wrap
原始类转为包装类,非原始类返回原类- Parameters:
clazz- 原始类- Returns:
- 包装类
- See Also:
-
unWrap
包装类转为原始类,非包装类返回原类- Parameters:
clazz- 包装类- Returns:
- 原始类
- See Also:
-
numberToWord
将阿拉伯数字转为英文表达方式- Parameters:
number-Number对象- Returns:
- 英文表达式
-
numberToSimple
将阿拉伯数字转为精简表示形式,例如:1200 - 1.2k- Parameters:
number-Number对象- Returns:
- 英文表达式
-
numberToChinese
将阿拉伯数字转为中文表达方式- Parameters:
number- 数字isUseTraditional- 是否使用繁体字(金额形式)- Returns:
- 中文
-
chineseToNumber
数字中文表示形式转数字- 一百一十二 -》 112
- 一千零一十二 -》 1012
- Parameters:
number- 数字中文表示- Returns:
- 数字
-
digitToChinese
金额转为中文形式- Parameters:
n- 数字- Returns:
- 中文大写数字
-
chineseMoneyToNumber
中文大写数字金额转换为数字,返回结果以元为单位的BigDecimal类型数字如: “陆万柒仟伍佰伍拾陆元叁角贰分”返回“67556.32” “叁角贰分”返回“0.32”
- Parameters:
chineseMoneyAmount- 中文大写数字金额- Returns:
- 返回结果以元为单位的BigDecimal类型数字
-
intToByte
public static byte intToByte(int intValue) int转byte- Parameters:
intValue- int值- Returns:
- byte值
-
byteToUnsignedInt
public static int byteToUnsignedInt(byte byteValue) byte转无符号int- Parameters:
byteValue- byte值- Returns:
- 无符号int值
-
bytesToShort
public static short bytesToShort(byte[] bytes) byte数组转short- Parameters:
bytes- byte数组- Returns:
- short值
-
shortToBytes
public static byte[] shortToBytes(short shortValue) short转byte数组- Parameters:
shortValue- short值- Returns:
- byte数组
-
bytesToInt
public static int bytesToInt(byte[] bytes) byte[]转int值- Parameters:
bytes- byte数组- Returns:
- int值
-
bytesToLong
public static long bytesToLong(byte[] bytes) byte数组转long- Parameters:
bytes- byte数组- Returns:
- long值
-
intToBytes
public static byte[] intToBytes(int intValue) int转byte数组- Parameters:
intValue- int值- Returns:
- byte数组
-
longToBytes
public static byte[] longToBytes(long longValue) long转byte数组- Parameters:
longValue- long值- Returns:
- byte数组
-