Java – Android date format in the ListView

Android date format in the ListView… here is a solution to the problem.

Android date format in the ListView

Thanks to Giulio Piancastelli I now have a ListView with multi-row functionality. Now I’m having trouble formatting the date on the second line. All dates are the same. In the feed, they are different. I need someone to help me format the date as day, month, year (Thursday, October 27, 2011).

This is the code that doesn’t work:

 List<Map<String, String>> data = new ArrayList<Map<String, String>>();
    for (RSSItem item : feed.getAllItems()) {
        Map<String, String> datum = new HashMap<String, String>(2);
        datum.put("title", item.getTitle());

String dateStr = item.getPubDate();
        SimpleDateFormat curFormater = new SimpleDateFormat("EEEE, MMMM dd, yyyy"); 
        Date dateObj = new Date();
        try {
            dateObj = curFormater.parse(dateStr);
        } catch (ParseException e) {
             TODO Auto-generated catch block
            e.printStackTrace();
        } 
        SimpleDateFormat postFormater = new SimpleDateFormat("EEEE, MMMM dd, yyyy"); 

String newDateStr = postFormater.format(dateObj);

datum.put("date", newDateStr);
        data.add(datum);
    }
    SimpleAdapter adapter = new SimpleAdapter(this, data,
                                              android. R.layout.simple_list_item_2,
                                              new String[] {"title", "date"},
                                              new int[] {android. R.id.text1,
                                                         android. R.id.text2});

itemlist.setAdapter(adapter);

itemlist.setOnItemClickListener(this);

itemlist.setSelection(0);

Repeating date

If I remove the previous date code, it works fine, but it’s not formatted correctly.

This code is valid, but not malformed:

List<Map<String, String>> data = new ArrayList<Map<String, String>>();
    for (RSSItem item : feed.getAllItems()) {
        Map<String, String> datum = new HashMap<String, String>(2);
        datum.put("title", item.getTitle());
        datum.put("date", item.getPubDate().toString());
        data.add(datum);
    }
    SimpleAdapter adapter = new SimpleAdapter(this, data,
                                              android. R.layout.simple_list_item_2,
                                              new String[] {"title", "date"},
                                              new int[] {android. R.id.text1,
                                                         android. R.id.text2});

itemlist.setAdapter(adapter);

itemlist.setOnItemClickListener(this);

itemlist.setSelection(0);

incorrect date format

I need someone to help me format the date as day, month, year (Thursday, October 27, 2011). Thanks!

Solution

The problem is that the pattern you are trying to parse the string is the same as the pattern you used to format later. You should parse it with the pattern it already has, which obviously looks like “Fri, 21 Oct 2011 12:00:00 GMT”.

So I suspect you want something like this :

// You probably actually want to set the time zone of the
 formatting pattern - but you'll need to think what time zone you
 really want. We don't know enough to say. Ditto the locale...
private static final DateFormat PARSING_PATTERN = 
    new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US); 
private static final DateFormat FORMATTING_PATTERN = 
    new SimpleDateFormat("EEEE, MMMM dd, yyyy"); 

...

for (RSSItem item : feed.getAllItems()) {
    Map<String, String> datum = new HashMap<String, String>(2);
    datum.put("title", item.getTitle());

String outputDate;
    try {
       Date date = PARSING_PATTERN.parse(item.getPubDate());
       outputDate = FORMATTING_PATTERN.format(date);
    } catch (ParseException e) {
       outputDate = "Invalid date";  Or whatever...
    } 
    datum.put("date", outputDate);
    data.add(datum);
}

Related Problems and Solutions