2

I have a string with the following time and date format: "13-Dec-2013 20:24:50" how can I change it to this format "Fri, Dec 13, 2013 8:24 pm"? I'm using Java as my language.

Arya
  • 8,473
  • 27
  • 105
  • 175

1 Answers1

5

Create a DateFormat that fits yours, parse your input string, create another DateFormat which fits your desired output format, format your date.

DateFormat inputFormat = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
Date d = inputFormat.parse("13-Dec-2013 20:24:50");
DateFormat outputFormat = new SimpleDateFormat("HH:mm:ss dd-MMM-yyyy");
System.out.println(outputFormat.format(d));

I already wrote a DateFormat which fits your input string. Have a look at SimpleDateFormat, you should be able to figure out the rest on your own.

Steffen
  • 3,999
  • 1
  • 23
  • 30
  • 1
    Just wondering - is there an _easy & nice_ way to get "pm" instead of "PM", which yields from `SimpleDateFormat` formatting? It would be nice if one could change the AM/PM strings in an existing (derived) DateFormat object. – Jindra Helcl Dec 16 '13 at 23:17
  • 1
    Have a look at this question: http://stackoverflow.com/questions/13581608/displaying-am-and-pm-in-small-letter-after-date-formatting – Steffen Dec 16 '13 at 23:32
  • I changed the format to this: DateFormat outputFormat = new SimpleDateFormat("EEE, MMM dd, yyyy hh:mm:ss a"); and it works perfectly now – Arya Dec 17 '13 at 04:20