How To Use Joda Datetime Date As Id In Room, Or How To Get Date In Query?
I need to write an app that uses date as id, and I must be able to update just by comparing date @Entity data class DailyCount(@PrimaryKey var date:DateTime,
Solution 1:
Room doesn't know how to save your primary field DateTime
into database.
You need to write a TypeConverter.
publicclassTypeConverter {
privatestaticGson gson = newGson();
@TypeConverterpublicstaticDateTimestringToDate(String data) {
Typetype = newTypeToken<DateTime>() {
}.getType();
return gson.fromJson(data, type);
}
@TypeConverterpublicstaticStringdateToString(DateTime dateTime) {
return gson.toJson(dateTime);
}
}
Then annotate your primary key
@PrimaryKey@TypeConverters(TypeConverter::class.java)
var date:DateTime
Solution 2:
I was needed to write this query :
@Query("update DailyCount set summ = :sum where date between :startTimeOfDay and :endTimeOfDay")funupdateCash(startTimeOfDay: DateTime, endTimeOfDay: DateTime, sum: Double)
I gave here a range with start and end time. This approach returns true when I want to set info in today's Id
Post a Comment for "How To Use Joda Datetime Date As Id In Room, Or How To Get Date In Query?"