如何从房间数据库获取日期

时间:2019-08-07 05:47:23

标签: android sqlite android-sqlite android-room android-database

我使用room database,想在数据库中保存日期。

我想将"dd/MM/yy"格式的日期保存到数据库中。

因此,我创建了DateConverter类来从数据库写入数据库。

现在,我可以将自己喜欢的格式的日期保存到数据库中,但是我无法从自己喜欢的格式的数据库中获取日期,而当我获得日期时,就会得到日期(日期,时间,日期)的所有信息。

我可以使用toTimestamp中的DateConverter方法,但是我认为这不是一个好主意。

我想知道有没有一种方法可以直接获取日期而不使用DateConverter.toTimestamp

@Entity(tableName = "notes")
public class NoteEntity {
    @PrimaryKey(autoGenerate = true)
    private int id;
    private Date date;
    private String text;

    @Ignore
    public NoteEntity() {
    }

    public NoteEntity(int id, Date date, String text) {
        this.id = id;
        this.date = date;
        this.text = text;
    }}

    @Ignore
    public NoteEntity(Date date, String text) {
        this.date = date;
        this.text = text;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

DateConverter

public class DateConverter {

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

    @TypeConverter
    public static String toTimestamp(Date date){
        SimpleDateFormat dateTimeFormat = new SimpleDateFormat("dd/MM/yy");
        return dateTimeFormat.format(date);
    }
}

Dao

@Dao
public interface NoteDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insertAll(List<NoteEntity> noteEntity);

    @Query("SELECT * FROM notes")
    List<NoteEntity> loadAllNotes();
}

样本数据

public static List<NoteEntity> getNotes() {
    List<NoteEntity> notes = new ArrayList<>();
    notes.add(new NoteEntity(new Date(2019,8,5), SAMPLE_TEXT_1));
    .
    .
    .
    return notes;
}

主要

AppDatabase appDatabase = AppDatabase.getInstance(context);

appDatabase.noteDao().insertAll(getNotes());

List<NoteEntity> notesData = new ArrayList<>();
notesData = appDatabase.noteDao().loadAllNotes();
        Log.i("TestOutput1", String.valueOf(notesData.get(0).getDate()));
        Log.i("TestOutput2", DateConverter.toTimestamp(notesData .get(0).getDate()));

LogCat

I/TestOutput1: Mon Aug 05 00:00:00 GMT+04:30 2019
I/TestOutput2: 05/08/19

2 个答案:

答案 0 :(得分:0)

您应该从Date转换为String,并从String(原始)转换为Date。我将其更改为String并进行了编译。

public class DateConverter {

  public static Date toDate(String stringdate){
     return timestamp == null ? null : new Date(stringdate);
  }

  public static String toTimestamp(Date date){
      SimpleDateFormat dateTimeFormat = new SimpleDateFormat("dd/MM/yy");        
      return dateTimeFormat.format(date);
  }
} 

答案 1 :(得分:0)

另存为字符串

val dateFormat : DateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") 
try {  
    val date: Date = Date(); 
    val dateTime = dateFormat.format(date)
    println("Current Date Time : " + dateTime)
} catch (e:ParseException ) {
    println(e)
}