Room Using Date field
Asked Answered
C

9

53

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 consider adding a type converter for it.

My Date Converter class

public class DateConverter {

    @TypeConverter
    public static Date toDate(Long dateLong){
        return dateLong == null ? null: new Date(dateLong);
    }

    @TypeConverter
    public static long fromDate(Date date){
        return date == null ? null :date.getTime();
    }
}

My Database table for using the date object.

@Entity(tableName = "userFitnessDailyRecords")

    @TypeConverters(DateConverter.class)
    public class UserFitnessDailyRecords {

        @NonNull
        @PrimaryKey(autoGenerate = true)
        public int id;
        public Date forDay;

        public Date getForDay() {
            return forDay;
        }

        public void setForDay(Date forDay) {
            this.forDay = forDay;
        }
    }

I followed the example from google code persistence labs and from commonwares room respective GitHub example. I am using room version 1.0.0.

Chiaki answered 13/5, 2018 at 6:52 Comment(1)
For me the best way is to use Gson() to convert it into string and vice versa. You can use any type of object with Gson conversion.Propagandize
E
57

You're converting from Date to Long (wrapper) and from long (primitive) to Date. I changed it to Long and it compiled. Besides, unboxing null in your converter produces a NPE.

public class DateConverter {

    @TypeConverter
    public static Date toDate(Long dateLong){
        return dateLong == null ? null: new Date(dateLong);
    }

    @TypeConverter
    public static Long fromDate(Date date){
        return date == null ? null : date.getTime();
    }
}
Exsert answered 13/5, 2018 at 12:13 Comment(0)
S
23

See my complete example.

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

public class Converters {
    @TypeConverter
    public static Date fromTimestamp(Long value) {
        return value == null ? null : new Date(value);
    }

    @TypeConverter
    public static Long dateToTimestamp(Date date) {
        return date == null ? null : date.getTime();
    }
}

Then map it to the database.

@Database(entities = {User.class}, version = 1)
@TypeConverters({Converters.class})
public abstract class AppDatabase extends RoomDatabase {
    public abstract UserDao userDao();
}

And the entity.

@Entity
public class User {
    private Date birthday;
}
Szeged answered 6/10, 2019 at 17:32 Comment(2)
Thanks! Contains an example of using. Should be accepted.Morganica
if we make the inner element as a list how do we do it? #70816392Marder
P
13

Date TypeConverter for Kotlin:

class Converters {
  @TypeConverter
  fun fromTimestamp(value: Long?): Date? {
    return value?.let { Date(it) }
  }

  @TypeConverter
  fun dateToTimestamp(date: Date?): Long? {
    return date?.time?.toLong()
  }
}

and here is how to attach it to your database:

@Database(entities = [User::class], version = 1)
@TypeConverters(Converters::class)
abstract class AppDatabase : RoomDatabase() {
  abstract fun userDao(): UserDao
}

Copied from official docs

Pantry answered 26/12, 2021 at 14:23 Comment(0)
L
9

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

  @TypeConverter
  public static Calendar toCalendar(Long l) {
    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(l);
    return c;
  }

  @TypeConverter
  public static Long fromCalendar(Calendar c){
    return c == null ? null : c.getTime().getTime();
  }
Lemuellemuela answered 9/5, 2019 at 14:32 Comment(0)
T
7

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}
Tarpaulin answered 17/3, 2019 at 7:13 Comment(0)
O
4

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

public class DateConverter {

  @TypeConverter
  public static Date toDate(long date) {
    return new Date(date);
  }

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

Note that this will default the date to current date.

Osmious answered 4/11, 2020 at 4:35 Comment(0)
T
2

Using Calendar with Kotlin (adapted from O95's answer):

@TypeConverter
fun toCalendar(l: Long?): Calendar? =
    if (l == null) null else Calendar.getInstance().apply { timeInMillis = l }

@TypeConverter
fun fromCalendar(c: Calendar?): Long? = c?.time?.time
Tilsit answered 31/8, 2019 at 8:49 Comment(0)
U
2

You can write it on Kotlin as well

@TypeConverter
fun toDate(dateLong:Long):Date {
    return Date(dateLong)
}

@TypeConverter
fun fromDate(date: Date):Long{
    return date.time;
}
Unhallow answered 20/11, 2019 at 12:16 Comment(0)
G
2

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,

class Converters {
    @TypeConverter
    fun fromTimestamp(value: Long?): LocalDateTime? {
        return value?.let {
            LocalDateTime.ofInstant(
                Instant.ofEpochMilli(it), ZoneId.systemDefault()
            )
        }
    }

    @TypeConverter
    fun LocalDateTimeToTimestamp(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})
public abstract class AppDatabase extends RoomDatabase {
    public abstract UserDao userDao();
}
Grodno answered 12/3, 2020 at 10:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.