【问题标题】:Format Timestamp from database to textview将时间戳从数据库格式化为 textview
【发布时间】:2016-02-27 13:20:46
【问题描述】:

我在数据库中有一个时间戳列,我想在我的文本视图中获取这种格式“dd/MM/yyyy”,我得到这个“yyyy/MM/dd”。

这是我的数据库中 onCreate 方法中的定义方式

KEY_CREATED_AT + " TIMESTAMP DEFAULT CURRENT_DATE,"

我这样显示它并得到这个形状“yyyy/MM/dd”。

((TextView) convertView.findViewById(R.id.textDate)).setText(log.getCreatedAt().toString());

我也尝试过这样的 SimpleDateFormat

    ((TextView) convertView.findViewById(R.id.textDate)).
setText(new SimpleDateFormat("dd/MM/yyyy").format(log.getCreatedAt().toString()));

应该怎么做才能得到像“dd/MM/yyyy”这样的日期?

这部分来自我的 dbHandler?

public ArrayList<Logs> getAllLogs(String place) {
        ArrayList<Logs> logList = new ArrayList<Logs>();
        String selectQuery = "SELECT * FROM " + TABLE_LOGS + " WHERE " + KEY_PLACE + "=?";

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, new String[]{place});

        //going throug all rows and adding it to list
        if (cursor.moveToFirst()) {
            do {
                Logs log = new Logs();
                log.setId(cursor.getLong(0));
                log.setCreatedAt(cursor.getString(1));
                log.setPlace(cursor.getString(2));
                log.setPlate_number(cursor.getString(3));
                log.setSort_id(cursor.getString(4));
                log.setGrade(cursor.getString(5));
                log.setDiameter(cursor.getString(6));
                log.setLength(cursor.getString(7));
                logList.add(log);
            }while (cursor.moveToNext());
        }

        return logList;
    }

这部分来自我的活动,我从 dbhandler 调用此方法并将其显示在 listView 中。

private class LogsArrayAdapter extends BaseAdapter {
        private LayoutInflater inflater;
        private List<Logs> logsList;

        public LogsArrayAdapter(List<Logs> logsList) {
            inflater = LayoutInflater.from(DisplayLogs.this);
            this.logsList = logsList;
        }

        @Override
        public int getCount() {
            return logsList.size();
        }

        @Override
        public Object getItem(int position) {
            return logsList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return logsList.get(position).getId();
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.activity_display_logs, parent, false);
            }
            Logs log = logsList.get(position);
            ((TextView) convertView.findViewById(R.id.textPlace)).setText(log.getPlace());
            ((TextView) convertView.findViewById(R.id.textNumber)).setText(log.getPlate_number());
            ((TextView) convertView.findViewById(R.id.textSort)).setText(log.getSort_id());
            ((TextView) convertView.findViewById(R.id.textGrade)).setText(log.getGrade());
            ((TextView) convertView.findViewById(R.id.textDiameter)).setText(log.getDiameter());
            ((TextView) convertView.findViewById(R.id.textLength)).setText(log.getLength());
           convertView.findViewById(R.id.textDate)).setText(log.getCreatedAt());
            return convertView;
        }

【问题讨论】:

    标签: android mysql date datetime


    【解决方案1】:

    SimpleDateFormat.format() 方法需要 Date 参数,而不是 String

    我可以建议,您的log.getCreatedAt()Date,因此请尝试更改您的代码:

    ((TextView) convertView.findViewById(R.id.textDate)).setText(new SimpleDateFormat("dd/MM/yyyy").format(log.getCreatedAt()));
    

    【讨论】:

    • 是的,log.getCreatedAt() 是我从数据库调用的Date。我像你说的那样改变了代码,但不幸的是没有工作......你还有其他建议吗?
    • 试试这个((TextView) convertView.findViewById(R.id.textDate)).setText(new SimpleDateFormat("dd/MM/yyyy").format(new Date()));。 textview 的当前日期值是否格式正确?
    • 这行得通,但这不是我需要的。它为我两天前创建的一些数据添加了今天的日期。它仍然需要以某种方式包含log.getCreatedAt(),然后解析为正确的格式,但我不明白怎么做?
    • 贴出你从 db 中读取日期的代码,并将其设置为 createdAt 变量。
    • 刚刚更新了一个问题!希望你能找到一些东西:)
    猜你喜欢
    • 2015-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-26
    • 2022-11-26
    • 1970-01-01
    • 2012-02-17
    • 2019-02-17
    相关资源
    最近更新 更多