I recently started learning java and how to build android apps. I'm currently working on an app which uses a CustomListAdapter to display MySQL data parsed via json into a ListView. The problem I am running into is how to change the way a TextView displays the DATETIME from my database. As it is now, it shows the complete YYYY-MM-DD HH:MM:SS format, and I'm actually just trying to display HH:MM A. I've seen other people ask similar questions and get responses to use the SimpleDateFormat class, however, I am not understanding how to fit into my code. due_time is the column from my database that is being parsed, which I also named the TextView id in my layout.
This was a snippet suggested by someone else to use:
SimpleDateFormat df1 = new SimpleDateFormat("YYYY-MM-DD"); SimpleDateFormat df2 = new SimpleDateFormat("DD-MM-YYYY"); try{ Date d = df1.parse(DateString); System.out.println("new date format " + df2.format(d)); }catch(Exception e){ e.printStackTrace(); } If that would work, I'm assuming I would insert due_time in there somewhere, then plug the snippet into the getView method of my CustomListAdapter, shown below. Am I on the right track?
@Override public View getView(int position, View convertView, ViewGroup parent) { if (inflater == null) inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); if (convertView == null) convertView = inflater.inflate(R.layout.list_row, null); TextView due_time = (TextView) convertView.findViewById(R.id.due_time); // getting order data for the row Order o = orderItems.get(position); // Due Time due_time.setText(o.getDueTime()); return convertView; }