Deepak Kataria
Deepak Kataria

Reputation: 143

Fetching Date from Sqlite Database in android

In my Sqlite DataBase I saved date in a data type DATE. How can i fetch this date from cursor?

Upvotes: 13

Views: 38036

Answers (3)

Rod
Rod

Reputation: 181

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();
}

Upvotes: 13

Yasmine GreenApple
Yasmine GreenApple

Reputation: 448

this code works

String s= cursor.getString(position);
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    Date d=new Date();
    try {
         d=  dateFormat.parse(s);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    YourObject.setDate(d);

Upvotes: 4

TNR
TNR

Reputation: 5869

SQLite stores a string representing the current time in UTC (GMT), using the ISO8601 date/time format. This format (YYYY-MM-DD HH:MM:SS), is by the way suitable for date/time comparisons.

Use the below code to retrieve the date.

Cursor row = databaseHelper.query(true, TABLE_NAME, new String[] {
COLUMN_INDEX}, ID_COLUMN_INDEX + "=" + rowId,
null, null, null, null, null);
String dateTime = row.getString(row.getColumnIndexOrThrow(COLUMN_INDEX));

This, returns a string, parse it and reformat to your local format and time zone:

DateFormat iso8601Format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
date = iso8601Format.parse(dateTime);
} catch (ParseException e) {
Log.e(TAG, "Parsing ISO8601 datetime failed", e);
}

long when = date.getTime();
int flags = 0;
flags |= android.text.format.DateUtils.FORMAT_SHOW_TIME;
flags |= android.text.format.DateUtils.FORMAT_SHOW_DATE;
flags |= android.text.format.DateUtils.FORMAT_ABBREV_MONTH;
flags |= android.text.format.DateUtils.FORMAT_SHOW_YEAR;

String finalDateTime = android.text.format.DateUtils.formatDateTime(context,
when + TimeZone.getDefault().getOffset(when), flags);

Hope this will help you.

Upvotes: 16

Related Questions