Class Convert

java.lang.Object
org.miaixz.bus.core.convert.Convert

public class Convert extends Object
类型转换器
Since:
Java 17+
Author:
Kimi Liu
  • Constructor Details

    • Convert

      public Convert()
  • Method Details

    • toString

      public static String toString(Object value, String defaultValue)
      转换为字符串 如果给定的值为null,或者转换失败,返回默认值 转换失败不会报错
      Parameters:
      value - 被转换的值
      defaultValue - 转换错误时的默认值
      Returns:
      结果
    • toString

      public static String toString(Object value)
      转换为字符串 如果给定的值为null,或者转换失败,返回默认值null 转换失败不会报错
      Parameters:
      value - 被转换的值
      Returns:
      结果
    • toStringOrNull

      public static String toStringOrNull(Object value)
      转换为字符串 如果给定的值为null,或者转换失败,返回默认值"null"(即null这个字符串) 转换失败不会报错
      Parameters:
      value - 被转换的值
      Returns:
      结果
    • toStringArray

      public static String[] toStringArray(Object value)
      转换为String数组
      Parameters:
      value - 被转换的值
      Returns:
      String数组
    • toChar

      public static Character toChar(Object value, Character defaultValue)
      转换为字符 如果给定的值为null,或者转换失败,返回默认值 转换失败不会报错
      Parameters:
      value - 被转换的值
      defaultValue - 转换错误时的默认值
      Returns:
      结果
    • toChar

      public static Character toChar(Object value)
      转换为字符 如果给定的值为null,或者转换失败,返回默认值null 转换失败不会报错
      Parameters:
      value - 被转换的值
      Returns:
      结果
    • toCharArray

      public static Character[] toCharArray(Object value)
      转换为Character数组
      Parameters:
      value - 被转换的值
      Returns:
      Character数组
    • toByte

      public static Byte toByte(Object value, Byte defaultValue)
      转换为byte 如果给定的值为null,或者转换失败,返回默认值 转换失败不会报错
      Parameters:
      value - 被转换的值
      defaultValue - 转换错误时的默认值
      Returns:
      结果
    • toByte

      public static Byte toByte(Object value)
      转换为byte 如果给定的值为null,或者转换失败,返回默认值null 转换失败不会报错
      Parameters:
      value - 被转换的值
      Returns:
      结果
    • toByteArray

      public static Byte[] toByteArray(Object value)
      转换为Byte数组
      Parameters:
      value - 被转换的值
      Returns:
      Byte数组
    • toPrimitiveByteArray

      public static byte[] toPrimitiveByteArray(Object value)
      转换为Byte数组
      Parameters:
      value - 被转换的值
      Returns:
      Byte数组
    • toShort

      public static Short toShort(Object value, Short defaultValue)
      转换为Short 如果给定的值为null,或者转换失败,返回默认值 转换失败不会报错
      Parameters:
      value - 被转换的值
      defaultValue - 转换错误时的默认值
      Returns:
      结果
    • toShort

      public static Short toShort(Object value)
      转换为Short 如果给定的值为null,或者转换失败,返回默认值null 转换失败不会报错
      Parameters:
      value - 被转换的值
      Returns:
      结果
    • toShortArray

      public static Short[] toShortArray(Object value)
      转换为Short数组
      Parameters:
      value - 被转换的值
      Returns:
      Short数组
    • toNumber

      public static Number toNumber(Object value, Number defaultValue)
      转换为Number 如果给定的值为空,或者转换失败,返回默认值 转换失败不会报错
      Parameters:
      value - 被转换的值
      defaultValue - 转换错误时的默认值
      Returns:
      结果
    • toNumber

      public static Number toNumber(Object value)
      转换为Number 如果给定的值为空,或者转换失败,返回默认值null 转换失败不会报错
      Parameters:
      value - 被转换的值
      Returns:
      结果
    • toNumberArray

      public static Number[] toNumberArray(Object value)
      转换为Number数组
      Parameters:
      value - 被转换的值
      Returns:
      Number数组
    • toInt

      public static Integer toInt(Object value, Integer defaultValue)
      转换为int 如果给定的值为空,或者转换失败,返回默认值 转换失败不会报错
      Parameters:
      value - 被转换的值
      defaultValue - 转换错误时的默认值
      Returns:
      结果
    • toInt

      public static Integer toInt(Object value)
      转换为int 如果给定的值为null,或者转换失败,返回默认值null 转换失败不会报错
      Parameters:
      value - 被转换的值
      Returns:
      结果
    • toIntArray

      public static Integer[] toIntArray(Object value)
      转换为Integer数组
      Parameters:
      value - 被转换的值
      Returns:
      结果
    • toLong

      public static Long toLong(Object value, Long defaultValue)
      转换为long 如果给定的值为空,或者转换失败,返回默认值 转换失败不会报错
      Parameters:
      value - 被转换的值
      defaultValue - 转换错误时的默认值
      Returns:
      结果
    • toLong

      public static Long toLong(Object value)
      转换为long 如果给定的值为null,或者转换失败,返回默认值null 转换失败不会报错
      Parameters:
      value - 被转换的值
      Returns:
      结果
    • toLongArray

      public static Long[] toLongArray(Object value)
      转换为Long数组
      Parameters:
      value - 被转换的值
      Returns:
      结果
    • toDouble

      public static Double toDouble(Object value, Double defaultValue)
      转换为double 如果给定的值为空,或者转换失败,返回默认值 转换失败不会报错
      Parameters:
      value - 被转换的值
      defaultValue - 转换错误时的默认值
      Returns:
      结果
    • toDouble

      public static Double toDouble(Object value)
      转换为double 如果给定的值为空,或者转换失败,返回默认值null 转换失败不会报错
      Parameters:
      value - 被转换的值
      Returns:
      结果
    • toDoubleArray

      public static Double[] toDoubleArray(Object value)
      转换为Double数组
      Parameters:
      value - 被转换的值
      Returns:
      结果
    • toFloat

      public static Float toFloat(Object value, Float defaultValue)
      转换为Float 如果给定的值为空,或者转换失败,返回默认值 转换失败不会报错
      Parameters:
      value - 被转换的值
      defaultValue - 转换错误时的默认值
      Returns:
      结果
    • toFloat

      public static Float toFloat(Object value)
      转换为Float 如果给定的值为空,或者转换失败,返回默认值null 转换失败不会报错
      Parameters:
      value - 被转换的值
      Returns:
      结果
    • toFloatArray

      public static Float[] toFloatArray(Object value)
      转换为Float数组
      Parameters:
      value - 被转换的值
      Returns:
      结果
    • toBoolean

      public static Boolean toBoolean(Object value, Boolean defaultValue)
      转换为boolean String支持的值为:true、false、yes、ok、no,1,0 如果给定的值为空,或者转换失败,返回默认值 转换失败不会报错
      Parameters:
      value - 被转换的值
      defaultValue - 转换错误时的默认值
      Returns:
      结果
    • toBoolean

      public static Boolean toBoolean(Object value)
      转换为boolean 如果给定的值为空,或者转换失败,返回默认值null 转换失败不会报错
      Parameters:
      value - 被转换的值
      Returns:
      结果
    • toBooleanArray

      public static Boolean[] toBooleanArray(Object value)
      转换为Boolean数组
      Parameters:
      value - 被转换的值
      Returns:
      结果
    • toBigInteger

      public static BigInteger toBigInteger(Object value, BigInteger defaultValue)
      转换为BigInteger 如果给定的值为空,或者转换失败,返回默认值 转换失败不会报错
      Parameters:
      value - 被转换的值
      defaultValue - 转换错误时的默认值
      Returns:
      结果
    • toBigInteger

      public static BigInteger toBigInteger(Object value)
      转换为BigInteger 如果给定的值为空,或者转换失败,返回默认值null 转换失败不会报错
      Parameters:
      value - 被转换的值
      Returns:
      结果
    • toBigDecimal

      public static BigDecimal toBigDecimal(Object value, BigDecimal defaultValue)
      转换为BigDecimal 如果给定的值为空,或者转换失败,返回默认值 转换失败不会报错
      Parameters:
      value - 被转换的值
      defaultValue - 转换错误时的默认值
      Returns:
      结果
    • toBigDecimal

      public static BigDecimal toBigDecimal(Object value)
      转换为BigDecimal 如果给定的值为空,或者转换失败,返回null 转换失败不会报错
      Parameters:
      value - 被转换的值
      Returns:
      结果
    • toDate

      public static Date toDate(Object value, Date defaultValue)
      转换为Date 如果给定的值为空,或者转换失败,返回默认值 转换失败不会报错
      Parameters:
      value - 被转换的值
      defaultValue - 转换错误时的默认值
      Returns:
      结果
    • toLocalDateTime

      public static LocalDateTime toLocalDateTime(Object value, LocalDateTime defaultValue)
      LocalDateTime 如果给定的值为空,或者转换失败,返回默认值 转换失败不会报错
      Parameters:
      value - 被转换的值
      defaultValue - 转换错误时的默认值
      Returns:
      结果
    • toLocalDateTime

      public static LocalDateTime toLocalDateTime(Object value)
      转换为LocalDateTime 如果给定的值为空,或者转换失败,返回null 转换失败不会报错
      Parameters:
      value - 被转换的值
      Returns:
      结果
    • toInstant

      public static Date toInstant(Object value, Date defaultValue)
      Instant 如果给定的值为空,或者转换失败,返回默认值 转换失败不会报错
      Parameters:
      value - 被转换的值
      defaultValue - 转换错误时的默认值
      Returns:
      结果
    • toDate

      public static Date toDate(Object value)
      转换为Date 如果给定的值为空,或者转换失败,返回null 转换失败不会报错
      Parameters:
      value - 被转换的值
      Returns:
      结果
    • toEnum

      public static <E extends Enum<E>> E toEnum(Class<E> clazz, Object value, E defaultValue)
      转换为Enum对象 如果给定的值为空,或者转换失败,返回默认值
      Type Parameters:
      E - 枚举类型
      Parameters:
      clazz - Enum的Class
      value - 值
      defaultValue - 默认值
      Returns:
      Enum
    • toEnum

      public static <E extends Enum<E>> E toEnum(Class<E> clazz, Object value)
      转换为Enum对象 如果给定的值为空,或者转换失败,返回默认值null
      Type Parameters:
      E - 枚举类型
      Parameters:
      clazz - Enum的Class
      value - 值
      Returns:
      Enum
    • toCollection

      public static Collection<?> toCollection(Class<?> collectionType, Class<?> elementType, Object value)
      转换为集合类
      Parameters:
      collectionType - 集合类型
      elementType - 集合中元素类型
      value - 被转换的值
      Returns:
      Collection
    • toList

      public static List<?> toList(Object value)
      转换为ArrayList,元素类型默认Object
      Parameters:
      value - 被转换的值
      Returns:
      List
    • toList

      public static <T> List<T> toList(Class<T> elementType, Object value)
      转换为ArrayList
      Type Parameters:
      T - 元素类型
      Parameters:
      elementType - 集合中元素类型
      value - 被转换的值
      Returns:
      ArrayList
    • toSet

      public static <T> Set<T> toSet(Class<T> elementType, Object value)
      转换为HashSet
      Type Parameters:
      T - 元素类型
      Parameters:
      elementType - 集合中元素类型
      value - 被转换的值
      Returns:
      HashSet
    • toMap

      public static <K, V> Map<K,V> toMap(Class<K> keyType, Class<V> valueType, Object value)
      转换为Map,若value原本就是Map,则转为原始类型,若不是则默认转为HashMap
      Type Parameters:
      K - 键类型
      V - 值类型
      Parameters:
      keyType - 键类型
      valueType - 值类型
      value - 被转换的值
      Returns:
      Map
    • toMap

      public static <K, V> Map<K,V> toMap(Class<?> mapType, Class<K> keyType, Class<V> valueType, Object value)
      转换为Map
      Type Parameters:
      K - 键类型
      V - 值类型
      Parameters:
      mapType - 转后的具体Map类型
      keyType - 键类型
      valueType - 值类型
      value - 被转换的值
      Returns:
      Map
    • convertByClassName

      public static <T> T convertByClassName(String className, Object value) throws ConvertException
      转换值为指定类型,类型采用字符串表示
      Type Parameters:
      T - 目标类型
      Parameters:
      className - 类的字符串表示
      value - 值
      Returns:
      转换后的值
      Throws:
      ConvertException - 转换器不存在
    • convert

      public static <T> T convert(Class<T> type, Object value) throws ConvertException
      转换值为指定类型
      Type Parameters:
      T - 目标类型
      Parameters:
      type - 类型
      value - 值
      Returns:
      转换后的值
      Throws:
      ConvertException - 转换器不存在
    • convert

      public static <T> T convert(TypeReference<T> reference, Object value) throws ConvertException
      转换值为指定类型
      Type Parameters:
      T - 目标类型
      Parameters:
      reference - 类型参考,用于持有转换后的泛型类型
      value - 值
      Returns:
      转换后的值
      Throws:
      ConvertException - 转换器不存在
    • convert

      public static <T> T convert(Type type, Object value) throws ConvertException
      转换值为指定类型
      Type Parameters:
      T - 目标类型
      Parameters:
      type - 类型
      value - 值
      Returns:
      转换后的值
      Throws:
      ConvertException - 转换器不存在
    • convert

      public static <T> T convert(Class<T> type, Object value, T defaultValue) throws ConvertException
      转换值为指定类型
      Type Parameters:
      T - 目标类型
      Parameters:
      type - 类型
      value - 值
      defaultValue - 默认值
      Returns:
      转换后的值
      Throws:
      ConvertException - 转换器不存在
    • convert

      public static <T> T convert(Type type, Object value, T defaultValue) throws ConvertException
      转换值为指定类型
      Type Parameters:
      T - 目标类型
      Parameters:
      type - 类型
      value - 值
      defaultValue - 默认值
      Returns:
      转换后的值
      Throws:
      ConvertException - 转换器不存在
    • convertQuietly

      public static <T> T convertQuietly(Type type, Object value)
      转换值为指定类型,不抛异常转换 当转换失败时返回null
      Type Parameters:
      T - 目标类型
      Parameters:
      type - 目标类型
      value - 值
      Returns:
      转换后的值,转换失败返回null
    • convertQuietly

      public static <T> T convertQuietly(Type type, Object value, T defaultValue)
      转换值为指定类型,不抛异常转换 当转换失败时返回默认值
      Type Parameters:
      T - 目标类型
      Parameters:
      type - 目标类型
      value - 值
      defaultValue - 默认值
      Returns:
      转换后的值
    • convertWithCheck

      public static <T> T convertWithCheck(Type type, Object value, T defaultValue, boolean quietly)
      转换值为指定类型,可选是否不抛异常转换 当转换失败时返回默认值
      Type Parameters:
      T - 目标类型
      Parameters:
      type - 目标类型
      value - 值
      defaultValue - 默认值
      quietly - 是否静默转换,true不抛异常
      Returns:
      转换后的值
    • toSBC

      public static String toSBC(String input)
      半角转全角
      Parameters:
      input - String.
      Returns:
      全角字符串.
    • toSBC

      public static String toSBC(String input, Set<Character> notConvertSet)
      半角转全角,null返回null
      Parameters:
      input - String
      notConvertSet - 不替换的字符集合
      Returns:
      全角字符串,null返回null
    • toDBC

      public static String toDBC(String input)
      全角转半角
      Parameters:
      input - String.
      Returns:
      半角字符串
    • toDBC

      public static String toDBC(String text, Set<Character> notConvertSet)
      替换全角为半角
      Parameters:
      text - 文本
      notConvertSet - 不替换的字符集合
      Returns:
      替换后的字符
    • toHex

      public static String toHex(String text, Charset charset)
      字符串转换成十六进制字符串,结果为小写
      Parameters:
      text - 待转换的ASCII字符串
      charset - 编码
      Returns:
      16进制字符串
      See Also:
    • toHex

      public static String toHex(byte[] bytes)
      byte数组转16进制串
      Parameters:
      bytes - 被转换的byte数组
      Returns:
      转换后的值
      See Also:
    • hexToBytes

      public static byte[] hexToBytes(String src)
      Hex字符串转换为Byte值
      Parameters:
      src - Byte字符串,每个Byte之间没有分隔符
      Returns:
      byte[]
      See Also:
    • hexToString

      public static String hexToString(String hexStr, Charset charset)
      十六进制转换字符串
      Parameters:
      hexStr - Byte字符串(Byte之间无分隔符 如:[616C6B])
      charset - 编码 Charset
      Returns:
      对应的字符串
      See Also:
    • strToUnicode

      public static String strToUnicode(String strText)
      String的字符串转换成unicode的String
      Parameters:
      strText - 全角字符串
      Returns:
      String 每个unicode之间无分隔符
      See Also:
    • unicodeToString

      public static String unicodeToString(String unicode)
      unicode的String转换成String的字符串
      Parameters:
      unicode - Unicode符
      Returns:
      String 字符串
      See Also:
    • convertCharset

      public static String convertCharset(String text, String sourceCharset, String destCharset)
      给定字符串转换字符编码 如果参数为空,则返回原字符串,不报错。
      Parameters:
      text - 被转码的字符串
      sourceCharset - 原字符集
      destCharset - 目标字符集
      Returns:
      转换后的字符串
      See Also:
    • convertTime

      public static long convertTime(long sourceDuration, TimeUnit sourceUnit, TimeUnit destUnit)
      转换时间单位
      Parameters:
      sourceDuration - 时长
      sourceUnit - 源单位
      destUnit - 目标单位
      Returns:
      目标单位的时长
    • wrap

      public static Class<?> wrap(Class<?> clazz)
      原始类转为包装类,非原始类返回原类
      Parameters:
      clazz - 原始类
      Returns:
      包装类
      See Also:
    • unWrap

      public static Class<?> unWrap(Class<?> clazz)
      包装类转为原始类,非包装类返回原类
      Parameters:
      clazz - 包装类
      Returns:
      原始类
      See Also:
    • numberToWord

      public static String numberToWord(Number number)
      将阿拉伯数字转为英文表达方式
      Parameters:
      number - Number对象
      Returns:
      英文表达式
    • numberToSimple

      public static String numberToSimple(Number number)
      将阿拉伯数字转为精简表示形式,例如:
           1200 - 1.2k
       
      Parameters:
      number - Number对象
      Returns:
      英文表达式
    • numberToChinese

      public static String numberToChinese(double number, boolean isUseTraditional)
      将阿拉伯数字转为中文表达方式
      Parameters:
      number - 数字
      isUseTraditional - 是否使用繁体字(金额形式)
      Returns:
      中文
    • chineseToNumber

      public static BigDecimal chineseToNumber(String number)
      数字中文表示形式转数字
      • 一百一十二 - 112
      • 一千零一十二 - 1012
      Parameters:
      number - 数字中文表示
      Returns:
      数字
    • digitToChinese

      public static String digitToChinese(Number n)
      金额转为中文形式
      Parameters:
      n - 数字
      Returns:
      中文大写数字
    • chineseMoneyToNumber

      public static BigDecimal chineseMoneyToNumber(String chineseMoneyAmount)
      中文大写数字金额转换为数字,返回结果以元为单位的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值
    • intToBytes

      public static byte[] intToBytes(int intValue)
      int转byte数组 默认以小端序转换
      Parameters:
      intValue - int值
      Returns:
      byte数组
    • longToBytes

      public static byte[] longToBytes(long longValue)
      Parameters:
      longValue - long值
      Returns:
      byte数组
    • bytesToLong

      public static long bytesToLong(byte[] bytes)
      Parameters:
      bytes - byte数组
      Returns:
      long值