1

I've a sqlite column with date as string in this format yyyy-mm-dd, I'm trying to convert it with SimpleDateFormat but always return 01-01-1970? This is the code of my Adapter:

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

3 回答 3

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;
            }
于 2013-01-10T09:39:30.910 回答
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;
    }
于 2013-01-30T15:31:59.113 回答
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;
    }
于 2013-01-30T16:19:11.003 回答