Class Convert

java.lang.Object
cloud.opencode.base.core.convert.Convert

public final class Convert extends Object
Unified Type Conversion Entry - Static methods for type conversion 统一类型转换入口 - 类型转换的静态方法

Provides convenient static methods for common type conversions using registered converters.

提供使用注册转换器的常用类型转换便捷静态方法。

Features | 主要功能:

  • Primitive conversions (toInt, toLong, toDouble, toBoolean) - 基本类型转换
  • String conversion (toStr) - 字符串转换
  • Date/Time conversion (toDate, toLocalDate, toLocalDateTime) - 日期时间转换
  • Array conversion (toArray) - 数组转换
  • Generic conversion (convert with TypeReference) - 泛型转换

Usage Examples | 使用示例:

// Basic type conversion - 基本类型转换
Integer num = Convert.toInt("123", 0);
Boolean b = Convert.toBool("true");

// Date conversion - 日期转换
LocalDate date = Convert.toLocalDate("2024-01-15");

Security | 安全性:

  • Thread-safe: Yes (uses thread-safe registry) - 线程安全: 是 (使用线程安全注册表)
  • Null-safe: Yes (returns default on null) - 空值安全: 是 (null 返回默认值)
Since:
JDK 25, opencode-base-core V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • toInt

      public static Integer toInt(Object value)
      Converts the given value to Integer. 转换为 Integer
    • toInt

      public static Integer toInt(Object value, Integer defaultValue)
      Converts the given value to Integer, returning the default value if conversion fails. 转换为 Integer,带默认值
    • toLong

      public static Long toLong(Object value)
      Converts the given value to Long. 转换为 Long
    • toLong

      public static Long toLong(Object value, Long defaultValue)
      Converts the given value to Long, returning the default value if conversion fails. 转换为 Long,带默认值
    • toDouble

      public static Double toDouble(Object value)
      Converts the given value to Double. 转换为 Double
    • toDouble

      public static Double toDouble(Object value, Double defaultValue)
      Converts the given value to Double, returning the default value if conversion fails. 转换为 Double,带默认值
    • toFloat

      public static Float toFloat(Object value)
      Converts the given value to Float. 转换为 Float
    • toFloat

      public static Float toFloat(Object value, Float defaultValue)
      Converts the given value to Float, returning the default value if conversion fails. 转换为 Float,带默认值
    • toShort

      public static Short toShort(Object value)
      Converts the given value to Short. 转换为 Short
    • toShort

      public static Short toShort(Object value, Short defaultValue)
      Converts the given value to Short, returning the default value if conversion fails. 转换为 Short,带默认值
    • toByte

      public static Byte toByte(Object value)
      Converts the given value to Byte. 转换为 Byte
    • toByte

      public static Byte toByte(Object value, Byte defaultValue)
      Converts the given value to Byte, returning the default value if conversion fails. 转换为 Byte,带默认值
    • toBool

      public static Boolean toBool(Object value)
      Converts the given value to Boolean. 转换为 Boolean
    • toBool

      public static Boolean toBool(Object value, Boolean defaultValue)
      Converts the given value to Boolean, returning the default value if conversion fails. 转换为 Boolean,带默认值
    • toChar

      public static Character toChar(Object value)
      Converts the given value to Character. 转换为 Character
    • toChar

      public static Character toChar(Object value, Character defaultValue)
      Converts the given value to Character, returning the default value if conversion fails. 转换为 Character,带默认值
    • toStr

      public static String toStr(Object value)
      Converts the given value to String. 转换为 String
    • toStr

      public static String toStr(Object value, String defaultValue)
      Converts the given value to String, returning the default value if conversion fails. 转换为 String,带默认值
    • toIntArray

      public static int[] toIntArray(Object value)
      Converts the given value to an int array. 转换为 int 数组
    • toLongArray

      public static long[] toLongArray(Object value)
      Converts the given value to a long array. 转换为 long 数组
    • toStrArray

      public static String[] toStrArray(Object value)
      Converts the given value to a String array. 转换为 String 数组
    • toList

      public static <T> List<T> toList(Object value, Class<T> elementType)
      Converts the given value to a List of the specified element type. 转换为 List
    • toSet

      public static <T> Set<T> toSet(Object value, Class<T> elementType)
      Converts the given value to a Set of the specified element type. 转换为 Set
    • convert

      public static <T> T convert(Object value, Class<T> clazz)
      Generic type conversion 泛型转换
    • convert

      public static <T> T convert(Object value, TypeReference<T> typeRef)
      Generic type conversion (using TypeReference) 泛型转换(使用 TypeReference)