android - 如何在不考虑时间的情况下从 unixtime(integer) 列包含当前日期的表中选择记录?

标签 android sqlite datetime timestamp android-sqlite

我在 Android SQLite 数据库中有一个包含以下字段的表。

enter image description here

在此,due_date 列是 INTEGER,我只希望在 due_date 不考虑时间匹配今天的日期(当前日期)的记录。

例如:如果我有 3 个值与当前日期匹配,我需要返回所有 3 行

Note: In my case, have to compare with current date not with current timestamp but my column type was INTEGER having unix-epoch value. My unix-epoch example is "1526565900000".

我试过:

 date('now', 'start of day') // not working

 date('now','localtime','start of day') // not working

select strftime('%d', date('now')), name, due_date from task group by due_date;

这也不符合我的要求。

Finally as per the comments below i changed all my datetime and timestamp column into INTEGER to store as unix-epoch value before that all my column were in timestamp.

最佳答案

第 1 步: 首先,我将列类型更改为 int 作为 @CL mentioned之前声明为 datetime 和 timestamp 的地方。

第 2 步: 然后按照 @pskink 在评论中所说的那样插入所有以秒为单位的日期时间值(即 10 位数字)。

第 3 步:为了以秒为单位获取当前日期的开始和结束值,我使用了以下 java 代码。

//getCurrentDateTimeInMilliseconds
    public void getCurrentDateTimeInMilliseconds() {
        Date todayStart = new Date();
        Date todayEnd = new Date();

        todayStart.setHours(0);
        todayStart.setMinutes(0);
        todayStart.setSeconds(0);
        todayEnd.setHours(23);
        todayEnd.setMinutes(59);
        todayEnd.setSeconds(59);
        long endTime = todayEnd.getTime();
    }

使用它你可以像这样获取当天的开始和结束值

todayStart.getTime()

它将返回以毫秒为单位的值,这是 13 位数字值,将其转换为秒,只需像这样除以 1000,

long startDateInSeconds = todayStart.getTime/1000;
long startDateInSeconds = todayEnd.getTime/1000;

并根据要求在 sqlite 查询中使用这些值,在我的例子中,查询将如下所示:

String query = "select * from task where due_date>" + startDateInSeconds + " and due_date<" + startDateInSeconds;

然后像这样在databasehelper类的方法中传递查询,

游标cursor = databasehelper.getData(query);

在 DatabaseHelper.java(应该扩展 SQLiteOpenHelper 类)文件中执行以下代码:

public Cursor getData(String query) {
        SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();
        return sqLiteDatabase.rawQuery(query, null);
    }

终于在 @pskink 的帮助下搞定了它,感谢您的意见对我帮助很大。

我的数据库中的数据是这样存储的。 enter image description here

关于android - 如何在不考虑时间的情况下从 unixtime(integer) 列包含当前日期的表中选择记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50266543/

相关文章:

c# - 计算给定日期和月份的下一个工作日期

datetime - AttributeError: 'datetime.timedelta' 对象没有属性 '_get_object_id' : pyspark

java - Android 中的问答游戏无法获得分数

android - 设置 View Gone 但仍然占用空间

android - Firestore onSnapshot() 和 get() 导致 Android 应用程序崩溃

今天是 Android SQLite

sqlite - 将 csv 导入 sqlite(firefox 附加组件)+TypeError : gFile. 内容为空

android - 没有通知显示安卓

sqlite - SQLite时间戳转换功能

c# - 使用 DateTime.ParseExact C# 未将字符串识别为有效的 DateTime