TypeConverter

The TypeConverter class allows you to convert arbitrary values into objects specified by a code type and to get the code type of an object.

See also TypeCode

Description

The TypeConverter class allows you to convert arbitrary values into objects specified by a code type and to get the code type of an object. The code types are defined in the TypeCode class.

Static methods

toNullableType

Converts a value into an object of the type specified by a TypeCode or returns null when the conversion is not possible.

public static T toNullableType(Class type, Object value)

  • type: Class for the data type into which ‘value’ is to be converted.
  • value: Object - value to convert.
  • returns: T - object value of type corresponding to TypeCode, or null when the conversion is not supported.

toString

Converts a TypeCode into its string name.

public static toString(type: TypeCode): string

  • type: TypeCode - TypeCode to convert into a string.
  • returns: string - name of the TypeCode passed as a string value.

toType

Converts a value into an object of the type specified by Type Code or returns an object of default type when the conversion is not possible.

public static Class toType(Class type, Object value)

  • type: Class - type to which the value is to be converted into.
  • value: Object - value to convert.
  • returns: Class - object value of type corresponding to TypeCode, or object of default type when the conversion is not supported.

toTypeCode

Gets a TypeCode for a specific value.

public static TypeCode toTypeCode(Object value)

  • value: Object - value whose TypeCode is to be resolved.
  • returns: TypeCode - TypeCode that corresponds to the passed object’s type.

toTypeWithDefault

Converts a value into an object of the type specified by Type Code or returns a default value when the conversion is not possible.

public static T toTypeWithDefault(Class type, Object value, T defaultValue)

  • type: Class - type for the data type into which ‘value’ is to be converted.
  • value: Object - value to convert.
  • defaultValue: T - default value to return if conversion is not possible (returns null).
  • returns: T - object value of type corresponding to TypeCode, or default value when the conversion is not supported.

Examples

{
  int value1 = TypeConverter.toType(TypeCode.Integer, "123.456"); // Result: 123
  ZonedDateTime value2 = TypeConverter.toType(TypeCode.DateTime, 123); // Result: ZonedDateTime(123)
  boolean value3 = TypeConverter.toType(TypeCode.Boolean, "F"); // Result: false
  }

See also