Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

338
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!