2013-01-09 57 views
1

我有一个sqlite列日期为这种格式的字符串yyyy-mm-dd,我试图用SimpleDateFormat转换它,但总是返回01-01-1970? 这是我的适配器代码:转换Sqlite列日期格式与SimpleDateFormat

adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() { 
    public boolean setViewValue(View view, Cursor cursor, int columnIndex) { 

     if (view.getId() == R.id.swimm_date) 
     { 
     int DateUSA = cursor.getInt(columnIndex); 
     TextView tv = (TextView)view; 
     DateFormat FormatDate = new SimpleDateFormat ("dd-MM-yyyy"); 
     String DateEUR = FormatDate.format(DateUSA); 
     tv.setText(DateEUR); 
     return true; 
     } 
+0

int DateUSA = cur sor.getInt(columnIndex);如果你将它存储为一个字符串..你为什么要检索它作为一个int .. – dymmeh

+0

'yyyy-MM-dd'或'dd-MM-yyyy'? –

+0

在数据库中我已经字符串yyyy-MM-dd,我想转换为dd-MM-yyyy – lucignolo

回答

1

也许这codefragment帮助你:

adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() { 
     public boolean setViewValue(View view, Cursor cursor, int columnIndex) { 
      if (view.getId() == R.id.swimm_date) { 
       DateFormat sdf = new SimpleDateFormat("dd-MM-yyyy"); 
       TextView tv = (TextView)view; 
       tv.setText(sdf.format(java.util.Date.parse(cursor.getString(columnIndex)))); 
       return true; 
      } 
0

使用本:

public static String getCurrentDate() { 
     SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); 
     String date = dateFormat.format(new Date(System.currentTimeMillis())); 
     return date; 
    } 
0

三江源,我已经用这个解决方案解决:

  if (view.getId() == R.id.swimm_date) 
    { 
    Date parsed = new Date(); 
    String DateUSA = cursor.getString(columnIndex); 
    SimpleDateFormat fmt = new SimpleDateFormat ("yyyy-MM-dd"); 
    SimpleDateFormat fmtOut = new SimpleDateFormat ("dd-MM-yyyy"); 

    try {parsed = fmt.parse(DateUSA);} 
    catch(ParseException pe) { 
    System.out.println("ERROR: Cannot parse \"" + parsed + "\""); 
    } 
    String DateEUR = fmtOut.format(parsed); 
    TextView tv = (TextView)view; 
    tv.setText(DateEUR); 
    return true; 
    }