Search code examples
androidsqliteandroid-roomandroid-architecture-components

Room database, SELECT * FROM table by a given day


I'm using Room Persistence Library in my Android project, and store date using date type converter:

object DateConverter {
    @TypeConverter
    @JvmStatic
    fun fromTimestamp(value: Long?): Date? = if (null == value) null else Date(value)

    @TypeConverter
    @JvmStatic
    fun dateToTimestamp(date: Date?): Long? = date?.time
}

How could I select from my table by a given day using using Room DAO?


Solution

  •  @Query("SELECT * FROM table_name WHERE date BETWEEN :dayst AND :dayet")
     Object getFromTable(long dayst, long dayet);
    

    date dayst will be the timestamp for 00:00, i.e., 09/11/2017 00:00:00

    date dayet will be the timestamp for 23:59, i.e., 09/11/2017 23:59:59

    The Object is what you are getting from the database.