saving date type in room database

Solutions on MaxInterview for saving date type in room database by the best coders in the world

showing results for - "saving date type in room database"
Octavia
28 Mar 2017
1//Table names in SQLite are case insensitive.
2@Entity(indices = {@Index(value = "first_name")})
3public class User {
4    @PrimaryKey(autoGenerate = true)
5    @ColumnInfo(name = "user_id")
6    public long userId;
7    @ColumnInfo(name = "first_name")
8    public String firstName;
9    @ColumnInfo(name = "created_date")
10    @TypeConverters({TimestampConverter.class})
11    public Date createDate;
12    @ColumnInfo(name = "date_of_birth")
13    @TypeConverters({DateConverter.class})
14    public Date dob;
15    @Embedded
16    public Address address;
17}
Ozzie
29 Sep 2016
1public class TimestampConverter {
2    static DateFormat df = new SimpleDateFormat(Constants.TIME_STAMP_FORMAT);
3    @TypeConverter
4    public static Date fromTimestamp(String value) {
5        if (value != null) {
6            try {
7                return df.parse(value);
8            } catch (ParseException e) {
9                e.printStackTrace();
10            }
11            return null;
12        } else {
13            return null;
14        }
15    }