Android Room长期使用TypeConverters

时间:2018-07-31 13:42:31

标签: android android-room

我想将日期从String转换为Long并插入数据库空间。为此,我尝试使用TypeConverters。

open class DateStringLongConverter {

private val DATE_PATTERN = "dd.MM.yyyy"

@TypeConverter
fun stringToLong(string: String): Long {
    UIDateUtils.getDayBeginTime()
    val form = SimpleDateFormat(DATE_PATTERN, Locale.getDefault())
    return form.parse(string).time
}

@TypeConverter
fun longToString(long: Long): String {
    val form = SimpleDateFormat(DATE_PATTERN, Locale.getDefault())
    return form.format(long)
}
}

但日期未转换。这是我的实体

 @TypeConverters(StringListConverter::class)
  class WaybillEntity(
    @PrimaryKey(autoGenerate = false)
    val id: Int? = null,
    @ColumnInfo(name = "idWaybill")
    val idWaybill: Int,
    val number: String,
    @TypeConverters(DateStringLongConverter::class)
    val date: String
  )

0 个答案:

没有答案