Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

337
Visualizações
Calendar TypeConverter for Room (Kotlin)

I am trying to persist a timestamp in my room database using the following TypeConverter:

class Converters {

    @TypeConverter
    fun fromTimestamp(value: Long?): Calendar? {

        if(value == null) return null

        val cal = GregorianCalendar()
        cal.timeInMillis = value
        return cal
    }

    @TypeConverter
    fun toTimestamp(timestamp: Calendar?): Long? {

        if(timestamp == null) return null

        return timestamp.timeInMillis
    }
}

Two of my Entities include the following column :

@ColumnInfo(name = "timestamp")
val timestamp: Calendar?,

But I get a compilation error upon trying to build the project - I had no issues when using the Date TypeConverter example from the developer reference guide.

I am unable to see what the actual error is as I just get a bunch of databinding 'cannot find symbol' errors if there is something wrong with the code related to Room.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Use:

object Converters {
    @TypeConverter
    @JvmStatic
    fun fromTimestamp(value: Long?): Calendar? = value?.let { value ->
        GregorianCalendar().also { calendar ->
            calendar.timeInMillis = value
        }
    }

    @TypeConverter
    @JvmStatic
    fun toTimestamp(timestamp: Calendar?): Long? = timestamp?.timeInMillis
}

And

@TypeConverters(Converters::class)
abstract class AppDatabase : RoomDatabase() {
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda