Arya
Arya

Reputation: 8975

Change date and time format

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.

Upvotes: 2

Views: 182

Answers (1)

Steffen
Steffen

Reputation: 4239

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.

Upvotes: 5

Related Questions