Android - datetime field hell! - well, seems like it anyway! :)
I am storing values from Calendar.getTime() in my SQLite database field called 'Created' when new records are inserted..
The field is 'datetime' and it stores values in this format : EEE MMM dd HH:mm:ss z yyyy.
My app has 2 datepickers so you can select a start and end date/time to create a range - all I need to do is run a query to return records which were created within this timespan but it is giving me a major headache!
My query looks like this :
public Cursor GetAllByDateRange(Date dtStart, Date dtEnd) {
return database.query(DATABASE_TABLE, new String[] {
KEY_id, KEY_CREATED, KEY_VALUE, KEY_DESCRIPTION, KEY_ISEXPENSE},
KEY_CREATED + " BETWEEN '" + dtStart + "' AND '" + dtEnd + "'",
null, null, null, KEY_CREATED + " DESC");
}
I think I understand the problem but not how to resolve it, I believe this approach will not work because SQLite stores dates in String format (if this is true I'm not sure why they even have a Date' type!), so they get compared as Strings rather than dates..
It appears my options are limited because I need to record time as well as the date, having spent most of today trawling the web I am at a complete loss - c开发者_JAVA百科an anyone suggest how I can move forward?
Thanks!
If possible, store your dates as integers in the datbase, storing the milliseconds value from date.toTime(). It's then much easier to instantiate a date object from new Date(milliseconds) and you can then format the output according to the locale.
精彩评论