Skip to content Skip to sidebar Skip to footer

Room Using Date Field

I am using date converter class to convert my date object. However, I still encounter an error saying. error: Cannot figure out how to save this field into a database. You can cons

Solution 1:

See my complete example.

Refer to the documentation : https://developer.android.com/training/data-storage/room/referencing-data

publicclassConverters {
    @TypeConverterpublicstaticDatefromTimestamp(Long value) {
        return value == null ? null : newDate(value);
    }

    @TypeConverterpublicstaticLongdateToTimestamp(Date date) {
        return date == null ? null : date.getTime();
    }
}

Then map it to the database.

@Database(entities = {User.class}, version = 1)@TypeConverters({Converters.class})publicabstractclassAppDatabaseextendsRoomDatabase {
    publicabstract UserDao userDao();
}

And the entity.

@EntitypublicclassUser {
    privateDate birthday;
}

Solution 2:

I had this same problem (how to store time to Room), but I was using Calendar, so I made this: [note: This anwer is for Calendar ; the main reason is that Calendar is now supported]

edit: the main reason for this answer is that Date is deprecated, so here you go

@TypeConverterpublicstaticCalendartoCalendar(Long l) {
    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(l);
    return c;
  }

  @TypeConverterpublicstaticLongfromCalendar(Calendar c){
    return c == null ? null : c.getTime().getTime();
  }

Solution 3:

Put converter class in class of data base, not in the model :

@Database(entities = {
    Patient.class,Medicine.class,Tooth.class,})

@TypeConverters({TimeConverter.class,OutBoundConverter.class})

public abstract class PatientDataBase extends RoomDatabase {//your data base}

Solution 4:

AndroidThreeTen is the port of Java8 new time classes, which unfortunately are available only for api>=26. Using https://github.com/JakeWharton/ThreeTenABP , we can use LocalDateTime on all versions of Android. Here in kotlin the converter,

classConverters{
    @TypeConverterfunfromTimestamp(value: Long?): LocalDateTime? {
        return value?.let {
            LocalDateTime.ofInstant(
                Instant.ofEpochMilli(it), ZoneId.systemDefault()
            )
        }
    }

    @TypeConverterfunLocalDateTimeToTimestamp(date: LocalDateTime?): Long? {
        return date?.atZone(ZoneId.systemDefault())?.toInstant()?.toEpochMilli()
    }
}

which, as other good answers already said, it's declared on the Database abstract class:

@Database(entities = {User.class}, version = 1)@TypeConverters({Converters.class})publicabstractclassAppDatabaseextendsRoomDatabase {
    publicabstract UserDao userDao();
}

Solution 5:

If you don't want to deal with Autoboxing and Unboxing, just use the primitive long data type as below:

publicclassDateConverter {

  @TypeConverterpublicstaticDatetoDate(long date) {
    returnnewDate(date);
  }

  @TypeConverterpublicstatic long fromDate(Date date) {
    return date == null ? Calendar.getInstance().getTimeInMillis() : date.getTime();
  }
}

Note that this will default the date to current date.

Post a Comment for "Room Using Date Field"