使用SimpleDateFormat转换Sqlite列日期格式

时间:2013-01-09 15:52:44

标签: android sqlite

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

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

3 个答案:

答案 0 :(得分:1)

也许这段代码片段可以帮助您:

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

答案 1 :(得分: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;
    }

答案 2 :(得分: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;
    }