【问题标题】:Convert Sqlite Column Date format with SimpleDateFormat使用 SimpleDateFormat 转换 Sqlite 列日期格式
【发布时间】:2013-01-09 15:52:44
【问题描述】:

我有一个以 yyyy-mm-dd 格式将日期作为字符串的 sqlite 列,我正在尝试使用 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;
        }

【问题讨论】:

  • int DateUSA = cursor.getInt(columnIndex);如果您将其存储为字符串..为什么要将其检索为 int..
  • yyyy-MM-dddd-MM-yyyy?
  • 在数据库中我有字符串 yyyy-MM-dd 我想在 dd-MM-yyyy 中转换

标签: android sqlite


【解决方案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;
            }

【讨论】:

    【解决方案2】:

    使用这个:

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

    【讨论】:

      【解决方案3】:

      谢谢,我已经用这个解决方案解决了:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-05-05
        • 1970-01-01
        • 2015-08-05
        • 1970-01-01
        • 1970-01-01
        • 2011-06-04
        相关资源
        最近更新 更多