0

with a query, I get a value of "date" from the SQLite database, in the format "YYYY-MM-DD". Now I want to convert this format to display the date in the format getInstance (). How can I do?

 DateFormat fmtDateAndTime=DateFormat.getDateInstance();  
 GregorianCalendar dateAndTime = (GregorianCalendar) GregorianCalendar.getInstance();

I get the date:

private void Mto() {
    String id_ricevuto = (i.getStringExtra("id_p"));    

    SQLiteDatabase db = mHelper.getReadableDatabase();
    String tabella = "SELECT _id, date FROM Table1 WHERE _id = '"+id_ricevuto+"'";
    Cursor c = db.rawQuery(tabella, null);
    while (c.moveToNext()){
        String id = c.getString(0);
        String date = c.getString(1);
        //here I want to convert the data before displaying it in the TextView          
        idw.setText(id);

        mBtnPickDate.setText(date);


    }

    c.close();
    db.close();
}

1 Answers1

0

Use can use SimpleDateFormat to acheive this:

private void Mto() {
    String id_ricevuto = (i.getStringExtra("id_p"));    
    SQLiteDatabase db = mHelper.getReadableDatabase();
    String tabella = "SELECT _id, date FROM Table1 WHERE _id = '"+id_ricevuto+"'";
    Cursor c = db.rawQuery(tabella, null);
    while (c.moveToNext()){
        String id = c.getString(0);
        String date = c.getString(1);
        DateFormat originalFormat = new SimpleDateFormat("YYYY-MM-DD", Locale.ENGLISH);
        DateFormat targetFormat = new SimpleDateFormat("yyyy:MM:dd");
        Date date = originalFormat.parse("August 21, 2012");
        String formattedDate = targetFormat.format(date);  // 20120821
        idw.setText(id);
        mBtnPickDate.setText(formattedDate);
    }
    c.close();
    db.close();
}

Also note that parse takes a String, not a Date object, which is already parsed. surely it will work!

Hamad
  • 5,096
  • 13
  • 37
  • 65