我在数据库中有一个Timestamp列,我希望在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"?
这部分来自我的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;
}
答案 0 :(得分:1)
SimpleDateFormat.format()
方法需要Date
参数,而不是String
。
根据我的建议,您的log.getCreatedAt()
为Date
,因此请尝试更改您的代码:
((TextView) convertView.findViewById(R.id.textDate)).setText(new SimpleDateFormat("dd/MM/yyyy").format(log.getCreatedAt()));