【问题标题】:Sqlite : Comparing timestamp value stored in a column to current DateSqlite:将存储在列中的时间戳值与当前日期进行比较
【发布时间】:2014-03-26 22:31:33
【问题描述】:

我将信息存储在一个 sqlite 数据库表中,如下所示: ActionDate 列的类型为:DEFAULT CURRENT_TIMESTAMP

    private String getDateTime() {
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        Date date = new Date();
        return dateFormat.format(date);
}

我想编写一个 SQL 查询,它从 sqlite DB 返回表中具有 ActionDate 值作为今天日期的所有行。但是,由于 ActionDate 是时间戳类型,因此将其转换为今天的日期最合适的方式是什么。

【问题讨论】:

    标签: java android sqlite timestamp


    【解决方案1】:

    如果您想要当前日期,请使用以下查询:

     SELECT date('now');
    

    要获取表中 ActionDate 等于今天日期的所有列,请使用以下查询:

    select * from table where strftime('%Y-%m-%d ', datetime(ActionDate, 'unixepoch'))  = date('now)' ;
    

    有关 SQLite 日期和时间查询的更多信息,请参阅here

    【讨论】:

    • 我要检索或选择今天的时间戳表单列 ActionDate
    • 什么是 ActionDate 列。它是您数据库表中的列吗?它存储什么?
    • 是的,它存储 DEFAULT CURRENT_TIMESTAMP 看我的第一篇文章
    • 但如果当前日期 == ActionDate 我只是选择它,但请注意,ActionDay 不能与仅日期相提并论!
    • 那么你想从你的表中获取所有具有 ActionDate = Todays date 的列吗?
    猜你喜欢
    • 2017-05-04
    • 1970-01-01
    • 2017-05-03
    • 2015-08-06
    • 1970-01-01
    • 2021-08-03
    • 1970-01-01
    • 2016-07-24
    • 1970-01-01
    相关资源
    最近更新 更多