title: How can I work with Date and Time at Room
body: <p>I've the below model, which have among others, <code>date</code> and <code>time</code> fields</p>
  
  <pre><code>import androidx.room.Entity
  import androidx.room.PrimaryKey
  import java.time.LocalDate
  import java.time.LocalTime
  
  @Entity
  data class Assignment(
      @PrimaryKey(autoGenerate = true) val tid: Int,
  
     // @PrimaryKey val uid: Int,
      @ColumnInfo(name = "id") val id: Int,
      @ColumnInfo(name = "task") val task: String?,
      @ColumnInfo(name = "address") val address: String?,
      @ColumnInfo(name = "datePicker") val datePicker: LocalDate?,
      @ColumnInfo(name = "timePicker") val timePicker: LocalTime?,
      @ColumnInfo(name = "status") val status: String?
  )
  </code></pre>
  
  <p>But I got the below error:</p>
  
  <blockquote>
    <p>Cannot figure out how to save this field into database. You can
    consider adding a type converter for it.</p>
  </blockquote>
  
  <p>So, I wrote the below convertor:</p>
  
  <pre><code>import java.sql.Date
  import java.time.LocalDate
  import javax.persistence.AttributeConverter
  import javax.persistence.Converter
  import java.time.ZoneId.systemDefault
  
  @Converter(autoApply = true)
  class LocalDateAttributeConverter : AttributeConverter&lt;LocalDate, Date&gt; {
  
      override fun convertToDatabaseColumn(locDate: LocalDate?): Date? {
          return if (locDate == null) null else Date.valueOf(locDate.toString())
      }
  
      override fun convertToEntityAttribute(sqlDate: Date?): LocalDate? {
          val defaultZoneId = systemDefault()
          val instant = sqlDate?.toInstant()
          return instant?.atZone(defaultZoneId)?.toLocalDate()
      }
  }
  </code></pre>
  
  <p>But did not know how to use this <code>convertor</code> with the database <code>model</code>?</p>
  
  <p><strong>UPDATE</strong>
  Based on the received answers, I changed the code to be as below, but still getting the same error:</p>
  
  <p>Converter:</p>
  
  <pre><code>import androidx.room.TypeConverter
  import java.sql.Date
  import java.time.LocalDate
  import java.time.ZoneId.systemDefault
  
  class Converters {
      @TypeConverter
      fun convertToDatabaseColumn(locDate: LocalDate?): Date? {
          return locDate?.let { Date.valueOf(locDate.toString()) }
      }
  
      @TypeConverter
      fun convertToEntityAttribute(sqlDate: Date?): LocalDate? {
          val defaultZoneId = systemDefault()
          val instant = sqlDate?.toInstant()
          return instant?.atZone(defaultZoneId)?.toLocalDate()
      }
  }
  </code></pre>
  
  <p>DataBase:</p>
  
  <pre><code>import android.content.Context
  import androidx.room.Database
  import androidx.room.Room
  import androidx.room.RoomDatabase
  import androidx.room.TypeConverters
  import kotlinx.coroutines.CoroutineScope
  
  @Database(entities = [Assignment::class], version = 1)
  @TypeConverters(Converters::class)
  abstract class AppDatabase : RoomDatabase() {
      abstract fun assignmentDao(): AssignmentDao
  
      companion object {
          @Volatile
          var INSTANCE: AppDatabase? = null
  
          fun getDatabase(context: Context,
                          scope: CoroutineScope
          ): AppDatabase {
              val tempInstance = INSTANCE
              if (tempInstance != null) {
                  return tempInstance
              }
              synchronized(this) {
                  val instance = Room.databaseBuilder(
                      context.applicationContext,
                      AppDatabase::class.java,
                      "database-name"
                  ).addCallback(AppDatabaseCallback(scope))
                      .build()
                  INSTANCE = instance
                  return instance
              }
          }
      }
  }
  </code></pre>
  
  <p>And</p>
  
  <pre><code>import androidx.room.ColumnInfo
  import androidx.room.Entity
  import androidx.room.PrimaryKey
  import androidx.room.TypeConverters
  import java.time.LocalDate
  import java.time.LocalTime
  //import javax.persistence.Convert
  
  @Entity
  data class Assignment(
      @PrimaryKey(autoGenerate = true) val tid: Int,
  
     // @PrimaryKey val uid: Int,
      @ColumnInfo(name = "id") val id: Int,
      @ColumnInfo(name = "task") val task: String?,
      @ColumnInfo(name = "address") val address: String?,
      @ColumnInfo(name = "timePicker") val timePicker: LocalTime?,
      @ColumnInfo(name = "status") val status: String?,
  
      @TypeConverters(Converters::class)
      @ColumnInfo(name = "datePicker") val datePicker: LocalDate?
  )
  </code></pre>