我遇到了使用时间戳获取数据的问题,但几周后我找到了一个好的解决方案,但无济于事。
- 将列时间戳设置为表格上的文本
public static final String CREATE_TABLE =
"CREATE TABLE " + TABLE_NAME + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ COLUMN_INCOME + " INTEGER,"
+ COLUMN_EXPENSE + " INTEGER,"
+ COLUMN_TIMESTAMP + " TEXT"
+ ")";
- 使用 new Date() 获取日期并转换为字符串
private String getDateTime() {
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
Date date = new Date();
return dateFormat.format(date);
}
- 使用 getDateTime() 作为时间戳在表中插入数据
ContentValues values = new ContentValues();
values.put(DailyTransaction.COLUMN_INCOME, income);
values.put(DailyTransaction.COLUMN_EXPENSE, expense);
values.put(DailyTransaction.COLUMN_TIMESTAMP, getDateTime());
- 那么您的 sqlite 查询将如下所示:
"SELECT * FROM "+ TABLE_NAME + " WHERE " + ClassName.COLUMN_TIMESTAMP + " <"+ "'"currentDate'";