从android中的sqlite数据库中获取字符串,日期,时间和int

CtJ*_*Jnx 6 sqlite android

我使用此代码将字符串,日期,时间和int值插入sqlite数据库

void addRemAction(RemActions remaction) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(KEY_ACTOINS_TITLE, remaction.getTitle()); // Action title
    values.put(KEY_ACTIONS_MSG, remaction.getMsg()); // action msg



    values.put(KEY_ACTIONS_DATE, dateFormat.format(new Date()));  // action date
    values.put(KEY_ACTIONS_TIME, remaction.getTime()); // action time
    values.put(KEY_ACTIONS_PLACE_LONG, remaction.getPlong()); // action place long
    values.put(KEY_ACTIONS_PLACE_LAT, remaction.getPlat());  // action place lat

    // Inserting Row
    db.insert(TABLE_ACTIONS, null, values);
    db.close(); // Closing database connection
Run Code Online (Sandbox Code Playgroud)

这是我的代码来检索它

RemActions getRemAction(int id) {
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query(TABLE_ACTIONS, new String[] {KEY_ACTOINS_TITLE, KEY_ACTIONS_MSG, KEY_PLACES_NAME, KEY_ACTIONS_DATE, KEY_ACTIONS_TIME}, KEY_ACTIONS_ID + "=?",
            new String[] { String.valueOf(id) }, null, null, null, null);
    if (cursor != null)
        cursor.moveToFirst();

    RemActions remActions = new RemActions(cursor.getString(0),
            cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getInt(4));
    // return remActions
    return remActions;
Run Code Online (Sandbox Code Playgroud)

现在我的ide给了我错误,因为我的RemAction类的构造函数中的第4个参数是java.util.date类中的Date类型.

我的问题是"我如何设置光标以日期格式获取它?"

生病了很高兴根据要求提供额外的代码

Cla*_*reG 11

由于您无法在SQLite中保存日期/时间值,因此应首先将它们转换为毫秒并将其存储为.

Date c = new Date(System.currentTimeMillis());
long milliseconds = c.getTime();
//To get current time
Run Code Online (Sandbox Code Playgroud)

如果您需要将毫秒更改回日期格式:

Date c = new Date(cursor.getLong(4));
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用格式化日期SimpleDateFormat.