android - 将时间戳从数据库格式化为 TextView

标签 android mysql date datetime

我在数据库中有一个时间戳列,我想在我的 TextView 中获取这种格式“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”这样的日期?

这部分来 self 的 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;
    }

这部分来 self 的 Activity ,我从 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;
        }

最佳答案

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

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

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

关于android - 将时间戳从数据库格式化为 TextView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35670311/

相关文章:

java - Android 的 Apache BasicCookieStore 上的 ConcurrentModificationException

从网络浏览器调用时出现 php 奇怪问题

javascript - JS : Get all months from specific date until today (also using moment. js)

机器人 : how does the OS chose a component implementation when multiple implementation respond to the same intent?

android - 'Hello Android' 教程问题

php - 一机多域,共享PHP MVC

mysql - SQL 连接查询选择行并检查它们是否存在于另一个表中

c# - 以编程方式更改系统日期

sql - 查找日期范围包含给定日期的记录?

用于获取虚拟键盘按键的 Android 虚拟键盘 KeyListener