【问题标题】:Order by on date stored as text in SQLite按日期排序在 SQLite 中存储为文本
【发布时间】:2015-09-23 01:30:53
【问题描述】:

我已经看到了很多关于此的问题,但对我没有任何帮助。

我将日期保存为我的 SQLite 数据库中的文本。我想使用 order by 检索它

这是查询:

SELECT * FROM table_historynew ORDER BY datetime(eventdate) ;

我得到的结果:

Id  : 35487
date: Thu Jun 25 06:30:00 IST 2015

Id  : 35488
date: Thu Jan 01 05:30:00 IST 1970

Id  : 35533
date: Sat Jun 27 11:00:00 IST 2015

【问题讨论】:

  • 为什么将日期存储为文本?
  • @Nabin 将其存储为文本应该可以正常工作,对吧?或者另存为时间戳是唯一的选择?
  • 我猜没有理由将其存储为文本。尝试使用 Date 或 TimeStamp 而不是
  • @Nabin SQLite 文档说我们可以将其保存为文本。
  • 你可以,但为什么要这样做?

标签: android sql datetime android-sqlite


【解决方案1】:

正如@calvinfly 所说,下面是一个代码 sn-p。 必须将其转换为 ISO8601 字符串!

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

String format_date = df.format(date);

...并将 format_date 存储在数据库中!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-14
    • 2016-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多