I have a Sqlite3 database table contains name,address,date of birth details.i want to display 1990-01-01 to 1995-01-01 details.
but Sqlite3 database stores only following data types.
TEXT
NUMERIC
INTEGER
REAL
NONE
Any one have some hint to store and retrieve date format data..?
You can do something like this
From my own experience on doing several projects with database in Android my answer is:
Do not store the date as a string. Never! Ever! Store them as Unix timestamps and format them as needed during runtime.
the important thing here is to separate what is your data and what is the on-screen representation of your data. Storing in a database the on-screen representation of your data is wrong.
You'll always store your dates as INTEGER types.
So for example to store the date now you'll store the value
System.currentTimeInMilis
To select between 1990-01-01 and 1995-01-01 you will:
and then you'll do the normal
SELECT
statement between those 2 values.to show those values in the screen as
yyyy-MM-dd
you'll use theSimpleDateFormat
class:Since the format you want to use (
yyyy-MM-dd
) is ordered in the same way as aString
(i.e. for any datesx
andy
you would choose, ifx < y
as aDate
, thenx < y
as aString
), you can simply store the dates asString
s (TEXT
) in your database.When selecting the values between them, you would just have to use a
WHERE
clause in yourSELECT
statement like this:You can then use
DateFormat.format
to set the values for the?
parameters of your prepared statement. The first parameter would be the "start" date, and the second would be the "end" date. You can replace<
with<=
and>
with>=
if you want the items on start and end dates included.This gives you a
String
representation of aDate
. To convert from that to an actualDate
object you can use date formatter'sparse
method (i.e.SimpleDateFormat.parse
).Another, "cleaner", approach would be to use the SQLite date and time functions (see here). While SQLite doesn't have a
DATE
type for storing date values, it has helper functions that you can use to interpretTEXT
andNUMBER
values as date in your statements.If you don't need extra processing for your date values, I'd recommend going for the first solution as it should be faster because it merely compares
TEXT
s rather than parsing and extracting a date from them, then comparing the extracted date (I haven't compared the speed of the two approaches, so don't take my word for it on this one). This approach also has less code to write and maintain and the code is easier to read.Sources:
SQLite data type - for the validity of comparing two
TEXT
valuesSimpleDateFormat - Android documentation
Use this code to convert your date into millisecond format and store it into your database as INTEGER types
date.getTime()-give the millisecond format
At the same way to convert your input (i.e from 1990-01-01 and to date 1995-01-01)
Retrieve from database using following query
You can use dates in yyyy-MM-dd format directly, JDBC will understand it. Assuming we a have a table t1 with c1 of DATE type
Reading dates is simple too
ResultSet.getDate returns result as java.sql.Date whose toString method returns date in yyyy-MM-dd format