RubyDigger19
RubyDigger19

Reputation: 835

Format Timestamp from database to textview

I have a Timestamp column in database and I want to get in my textview this format "dd/MM/yyyy" and I get this one "yyyy/MM/dd".

This is how is defined in my DB in onCreate method

KEY_CREATED_AT + " TIMESTAMP DEFAULT CURRENT_DATE,"

and I'm displaying it like this and get this shape "yyyy/MM/dd".

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

I tried also with SimpleDateFormat like this

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

How should do that to get date like this "dd/MM/yyyy"?

this part is from my 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;
    }

And this part is from my activity where I calling this method from dbhandler and displaying it in 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;
        }

Upvotes: 0

Views: 646

Answers (1)

Vasily Kabunov
Vasily Kabunov

Reputation: 6761

SimpleDateFormat.format() method needs Date argument, not String.

As I can suggest, your log.getCreatedAt() is Date, so try to change your code:

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

Upvotes: 1

Related Questions