Fetching Date from Sqlite Database in android

后端 未结 3 1476
青春惊慌失措
青春惊慌失措 2021-02-05 17:05

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

相关标签:
3条回答
  • 2021-02-05 17:18

    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);
    
    0 讨论(0)
  • 2021-02-05 17:24

    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.

    0 讨论(0)
  • 2021-02-05 17:27

    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();
    }
    
    0 讨论(0)
提交回复
热议问题