开发者

The Date type and the Android database

I understand that SQLite stores dates as long integers. When I read rows using the standard method (i.e. using the query() method that reads data into a cursor), the result is a date string that includes the time:

2010-05-25 19:52:04

If I want a different format, I have to parse the string back into a date - possible, but a bit backwards.

By using a ViewBinder (as suggested in this question), I can pretty much do anything I want, but the date is already a string at the time the overridden method executes.

The accepted answer to the above question also suggests that storing dates as longs would help avoid this problem. I don't want to do that, just in case I want to interpret my data with something else than this application. Maybe I want to expose it via a p开发者_JAVA技巧rovider.

To make matters worse, a DateFormat instance obtained via

android.text.format.DateFormat.getDateFormat(getApplicationContext())

cannot parse such a date string that is returned by the cursor. There is actually no static method that can do that - it is a one-way transformation, apparently an oversight.

Is there a way to handle this in the database adapter instead, i.e. controlling the date format that the cursor contains, so that I don't have to change the schema, and don't have to parse the default output back into a Date type?

The answers so far suggest that I should do the output formatting in the database. I don't want to do that for multiple reasons, most notably because I cannot format dates based on the user's locale that way (not easily, that is).


SQLite has a number of built-in date and time functions.


You might want to try this solution:

http://androidcookbook.com/Recipe.seam?recipeId=413


No need to get the date column as string, you should be able to use getLong(..) and create a proper Date object like this:

Date created = new Date(cursor.getLong(createdDateIndex));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜