在 SQLite select 语句中将整数格式化为格式化日期
我的 Android 应用程序中有一个 SQLite 数据库,它将日期存储为整数。这些整数源自对 Java.util.Date.getTime(); 的调用。我试图运行数据库的原始查询,以将 Cursor 传递给 CursorAdapter 并显示在 ListView 中,但日期存储为 getTime()
返回的整数。
为了使我的程序保持简单,我想避免使用 SimpleArrayAdapter,并坚持使用 CursorAdapter。
是否可以以某种方式将日期列中的整数格式化为 mm-dd-yyyy
,以便光标指向的表列包含格式正确的值,而不是原来的整数当我将项目添加到数据库时,由 Java.util.Date.getTime();
返回?
I have an SQLite database within my Android application, which stores dates as integers. These integers are derived from a call to Java.util.Date.getTime();
. I am trying to run a raw query of my database to get a Cursor to pass to a CursorAdapter and display in a ListView, but the date is stored as an integer as returned by getTime()
.
To keep my program simple, I would like to avoid using a SimpleArrayAdapter, and stick with the CursorAdapter.
Is it somehow possible to format the integer within the date column as mm-dd-yyyy
so that the column of the table, that the cursor is pointing to, contains properly formatted values rather than the integer that was returned by Java.util.Date.getTime();
when I added the item to the database?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您的代码需要采用名为 date_col 的格式的结果集列,则该代码将起作用。
编辑:您需要注意的一件事是 getTime 自 1970 年起使用毫秒,而标准 UNIX 时间(包括 SQLite)使用秒。
Your code will work if it expects a result set column in that format called date_col.
EDIT: One thing you need to watch out for is that getTime uses milliseconds since 1970, while standard UNIX time (including SQLite) uses seconds.
Java.util.Date.getTime();
方法返回一个表示“将此数字读取为日期的最简单方法是按原样存储它,然后使用以下 Sqlite 查询读取它:
其返回:
如果需要其他格式,可以使用
strftime
函数可以按照您的喜好进行格式化,或者任何其他可用的日期格式和函数。正如 Matthew Flaschen 在下面的推荐中指出的那样,您必须将日期除以 1000,然后才能以这种方式使用它们。 “真实”unix 时间以自纪元以来的秒为单位进行测量,
Java.util.Date.getTime();
返回自纪元以来的毫秒。The
Java.util.Date.getTime();
method is returning an integer that represents the "unix time".The simplest way to read this number as a date is by storing it as-is, and reading it using the following Sqlite query:
which returns:
If you need another format, you can use the
strftime
function to format is as you like, or any of the other date formats and functions available.You'll have to, as Matthew Flaschen points out in a commend below, divide the date by 1000 before you are able to use them in this way. "Real" unix times are measured in seconds since the epoch, and
Java.util.Date.getTime();
returns milliseconds since epoch.SQLite 使用静态刚性类型。对于静态类型,值的数据类型由其容器(存储值的特定列)决定。
SQLite 数据库中存储的任何值都具有以下存储类之一:
所以我不确定您的意思
但日期存储为一个长的、无用的整数。
了解更多详细信息请参阅SQLite Version 3 中的数据类型。有关在 SQLite 中存储日期/时间的更多信息,请参阅SQLite 理解的 SQL 。
我希望这有帮助。
SQLite uses static rigid typing. With static typing, the datatype of a value is determined by its container - the particular column in which the value is stored.
Any value stored in the SQLite database has one of the following storage class:
so I am not sure what you meant by
but the date is stored as a long, unhelpful integer.
For more details please refer to Datatypes In SQLite Version 3. For further information on storing date/time in SQLite please refer to SQL As Understood By SQLite.
I hope this helps.
我从 Excel 中得到的日期约为 45000,这是自 1970 年 1 月以来的天数。
以下是在 SQLite 中对我有用的内容:
I had dates coming out of Excel around 45000, which is days since Jan 1970.
Here's what worked for me in SQLite: