The Android Developer Challenge is back! Submit your idea before December 2.

TypeConverter

@Target([AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER]) class TypeConverter
androidx.room.TypeConverter

Marks a method as a type converter. A class can have as many @TypeConverter methods as it needs.

Each converter method should receive 1 parameter and have non-void return type.

// example converter for java.util.Date
  public static class Converters {
    @TypeConverter
    public Date fromTimestamp(Long value) {
      return value == null ? null : new Date(value);
    }
 
    @TypeConverter
    public Long dateToTimestamp(Date date) {
      if (date == null) {
        return null;
      } else {
        return date.getTime();
      }
    }
 }
  

Summary

Public constructors

Marks a method as a type converter.

Public constructors

<init>

TypeConverter()

Marks a method as a type converter. A class can have as many @TypeConverter methods as it needs.

Each converter method should receive 1 parameter and have non-void return type.

// example converter for java.util.Date
  public static class Converters {
    @TypeConverter
    public Date fromTimestamp(Long value) {
      return value == null ? null : new Date(value);
    }
 
    @TypeConverter
    public Long dateToTimestamp(Date date) {
      if (date == null) {
        return null;
      } else {
        return date.getTime();
      }
    }
 }
  

See Also