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

时间:2013-07-30 11:32:37

标签: android sqlite

我使用此代码

将字符串,日期,时间和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

这是我检索它的代码

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;

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

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

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

3 个答案:

答案 0 :(得分:11)

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

Date c = new Date(System.currentTimeMillis());
long milliseconds = c.getTime();
//To get current time

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

Date c = new Date(cursor.getLong(4));

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

答案 1 :(得分:2)

java.time

java.util Date-Time API 已过时且容易出错。建议完全停止使用,切换到modern Date-Time API*

使用 java.time(现代日期时间 API)的解决方案:

  1. 使用 Instant#now 获取当前时刻。
  2. 使用 Instant#toEpochMilli 将瞬间转换为 Unix 纪元 (1970-01-01T00:00:00Z) 中的毫秒数。
  3. 使用 Instant#ofEpochMilli 以使用 Unix 纪元 (1970-01-01T00:00:00Z) 的毫秒数获取 Instant 的实例。

演示:

import java.time.Instant;

public class Main {
    public static void main(String[] args) {
        Instant instant = Instant.now();
        System.out.println(instant);

        long instantToEpochMilli = instant.toEpochMilli();
        System.out.println(instantToEpochMilli);

        Instant instantOfEpochMilli = Instant.ofEpochMilli(instantToEpochMilli);
        System.out.println(instantOfEpochMilli);
    }
}

示例运行的输出:

2021-07-13T21:23:46.836723Z
1626211426836
2021-07-13T21:23:46.836Z

ONLINE DEMO

Instant 表示 UTC 中时间线上的一个瞬时点。输出中的 Z 是零时区偏移的 timezone designator。它代表祖鲁语并指定 Etc/UTC 时区(时区偏移为 +00:00 小时)。

Trail: Date Time 了解有关现代 Date-Time API 的更多信息。


* 出于任何原因,如果您必须坚持使用 Java 6 或 Java 7,您可以使用 ThreeTen-Backport,它将大部分 java.time 功能向后移植到 Java 6 & 7. 如果您正在为 Android 项目工作并且您的 Android API 级别仍然不符合 Java-8,请检查 Java 8+ APIs available through desugaringHow to use ThreeTenABP in Android Project

答案 2 :(得分:1)

由于您将此值存储为Date#getTime(),因此可以使用new Date(cursor.getLong(4))恢复该值。

此外,你需要关闭你没有做的Cursor

相关问题