-4


I'm new in android how do I format this "2013-07-02T04:17:24Z" into July 02, 2013 12:00 PM

Thanks in advance!

EDIT: Here is my code. Is there anything wrong in my code:

SimpleDateFormat sdf1 = new SimpleDateFormat("MMMM dd, yyyy");
String formattedDate1 = sdf1.format("2013-07-02T04:17:24Z");
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
Lian
  • 1,597
  • 3
  • 17
  • 30
  • Try to google first before posting question , you can use [DateFormat](http://developer.android.com/reference/java/text/DateFormat.html) – swapy Jul 27 '13 at 08:26
  • I'd googled it... and follow their examples but no luck.. That's why I ask here to get some help on how to parse/format a date in android – Lian Jul 27 '13 at 08:31
  • try `sdf1.format(new Date());` format method for [SimpleDateFormat](http://developer.android.com/reference/java/text/SimpleDateFormat.html) accepts Date object. please refer to [API DOC](http://developer.android.com/reference/java/text/SimpleDateFormat.html). – swapy Jul 27 '13 at 09:33

1 Answers1

0

Use SimpleDateFormat for the same. It does what you want and a sample example is also given in the documentation. Try something like below. You might have to change the variables values OLD_FORMAT and NEW_FORMAT as per your requirements. You can see the meaning of the symbols on above link.

final String OLD_FORMAT = "yyyy-MM-dd'T'HH:mm:ssZ";
final String NEW_FORMAT = "yyyy-MM-dd hh:mm a";

String oldDateString = "2013-07-02T04:17:24Z";
String newDateString;

SimpleDateFormat sdf = new SimpleDateFormat(OLD_FORMAT);
Date d = sdf.parse(oldDateString);
sdf.applyPattern(NEW_FORMAT);
newDateString = sdf.format(d);

I took the help from answers on How can I change the date format in Java? question as well. You may see the link for more.

Community
  • 1
  • 1
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
  • Having error `W/System.err(12406): java.text.ParseException: Unparseable date: "2013-07-02T04:17:24Z" (at offset 19)` – Lian Jul 29 '13 at 06:24
  • As I said in the answer, that the code provides you the way to parse one format to other. You might need to change the `OLD_FORMAT`, i.e the pattern of Date and Time which you are expecting to convert. Use the link I gave(http://developer.android.com/reference/java/text/SimpleDateFormat.html) to further check for the appropriate pattern for the input date format. There is a whole list of all the available options, which you can use for parsing a Date format. – Shobhit Puri Jul 29 '13 at 07:22