0

Hello I have been trying to format this date but it keeps giving me in unparsable date error? I am trying to get a time stamp like 2011-06-24T19:55:37Z to be June 24, 2011. here is the code I am using. Also on a side note is contraction (like the 1st, 2nd, 3rd) possible?

 SimpleDateFormat sdf = new SimpleDateFormat("MM d, yyyy", Locale.US);
 Date dt = sdf.parse("2011-03-01T17:55:15Z");
 time.setText("Time: " + dt.toString());
Pengume
  • 550
  • 11
  • 27

2 Answers2

2

The problem is that the format provided to SimpleDateFormat's constructor doesn't match the format of your date.

The string MM d, yyyy tells SimpleDateFormat how to interpret 2011-03-01T17:55:15Z.

Building a format string is described in the docs.

David Snabel-Caunt
  • 57,804
  • 13
  • 114
  • 132
  • I am still having some trouble. I need the date to look like June 24, 2011 from the timestamp 2011-06-24T19:55:37Z . Obviously I should strip off all the things I do not need. But does the foramtter work if the format is some thing like MM d, yyyy but the timestamp has the year first or do I need to switch everything around? – Pengume Jul 18 '11 at 20:15
0

This comes from another question within stackoverflow

Date date = new Date(location.getTime()); 
DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext()); 
mTimeText.setText("Time: " + dateFormat.format(date)); 


DateFormat formatter = new SimpleDateFormat("yyyy-mm-dd HH:MM:SS -05:00");
Date date = (Date)formatter.parse("2011-06-24T19:55:37Z");

Make sure the SimpleDateFormat matches your string

Graham
  • 651
  • 2
  • 10
  • 23