【问题标题】:Android date SQLite SimpleDateFormat Calendar [duplicate]Android日期SQLite SimpleDateFormat日历[重复]
【发布时间】:2014-08-12 17:03:14
【问题描述】:

通过查询,我从 SQLite 数据库中获得了“日期”值,格式为“YYYY-MM-DD”。现在我想把这种格式转换成getInstance()格式显示日期。我该怎么办?

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

我知道日期了:

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

【问题讨论】:

  • 发布完整代码,包括从数据库中获取值的位置。
  • 我编辑了我的帖子。谢谢

标签: android sqlite date calendar


【解决方案1】:

使用可以使用 SimpleDateFormat 来实现:

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

还要注意 parse 需要一个字符串,而不是一个已经被解析的 Date 对象。肯定会成功的!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-28
    • 1970-01-01
    • 2021-06-22
    相关资源
    最近更新 更多