I'm having some trouble working with dates on my Android application that uses SQLite. I have a couple questions:
- What type should I use to store dates in SQLite (text, integer, ...)?
- Given the best way to store dates how do I store It properly using ContentValues?
- What's the best way to retrieve the date from the SQLite database?
- How to make a sql select on SQLite, ordering the results by date?
1 -Exactly like StErMi said.
2 - Please read this: http://www.vogella.de/articles/AndroidSQLite/article.html
3 -
see here:
Query() in SQLiteDatabase
4 - see answer 3
The best way is to store the dates as a number, received by using the Calendar command.
Why do this? First of all, getting values from a date range is easy. Just convert your date into milliseconds, and then query appropriately. Sorting by date is similarly easy. The calls to convert among various formats are also likewise easy, as I included. Bottom line is, with this method, you can do anything you need to do, no problems. It will be slightly difficult to read a raw value, but it more than makes up that slight disadvantage with being easily machine readable and usable. And in fact, it is relatively easy to build a reader (And I know there are some out there) that will automatically convert the time tag to date as such for easy of reading.
It's worth mentioning that the values that come out of this should be long, not int. Integer in sqlite can mean many things, anything from 1-8 bytes, but for almost all dates 64 bits, or a long, is what works.
EDIT: As has been pointed out in the comments, you have to use the
cursor.getLong()
to properly get the timestamp if you do this.For storing, you could use a utility method
like so:
Another utility method takes care of the loading
can be used like this:
Ordering by date is simple SQL ORDER clause (because we have a numeric column). The following will order descending (that is newest date goes first):
Always make sure to store the UTC/GMT time, especially when working with
java.util.Calendar
andjava.text.SimpleDateFormat
that use the default (i.e. your device's) time zone.java.util.Date.Date()
is safe to use as it creates a UTC value.Best way to store
date
in SQlite DB is to store the currentDateTimeMilliseconds
. Below is the code snippet to do so_Now, your data (date is in currentTimeMilliseconds) is get inserted in DB .
Next step is, when you want to retrieve data from DB you need to convert the respective date time milliseconds in to corresponding date. Below is the sample code snippet to do the same_
Hope this will help all! :)
You can use a text field to store dates within
SQLite
.Storing dates in UTC format, the default if you use
datetime('now')
(yyyy-MM-dd HH:mm:ss)
will then allow sorting by the date column.Retrieving dates as strings from
SQLite
you can then format/convert them as required into local regionalised formats using the Calendar or theandroid.text.format.DateUtils.formatDateTime
method.Here's a regionalised formatter method I use;