In my Sqlite DataBase I saved date in a data type DATE. How can i fetch this date from cursor?
SQLite does not really have a DATE
type (the DATE
keyword just means the column has a NUMERIC
affinity, per Datatypes In SQLite Version 3), so it's up to you to choose a convention for how you'll store dates. Common conventions are (a) to use real numbers to store Julian dates or (b) to use integer numbers to store a Unix epoch (seconds since 1970, which the SQLite date and time function support with the 'unixepoch' argument per Date And Time Functions).
If you're doing storing dates as Unix epoch (convenient for Android since calling .getTime()
on a Date
object returns the number of milliseconds since 1970), then read the SQLite DATE
field as a long
and pass the millisecond equivalent of that into the java.util.Date
constructor Date(long milliseconds)
. So, it would look something like this:
SQLiteManager dbManager = new SQLiteManager(context, DB_NAME, null, VERSION);
SQLiteDatabase db = dbManager.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME,
new String[] { COLUMN_NAME_ID, COLUMN_NAME_DATE },
null, null, // selection, selectionArgs
null, null, null, null); // groupBy, having, orderBy, limit
try {
while(cursor.moveNext()) {
int id = cursor.getInt(0);
// Read the SQLite DATE as a long and construct a Java Date with it.
Date date = new Date(cursor.getLong(1)*1000);
// ...
}
} finally {
cursor.close();
db.close();
}