Ferrino
Ferrino

Reputation: 53

JSP: String to Date Format

I need to convert a string (extracted from SQL table) to a date format in JSP. I did this:

String PeerdateNtime="Wednesday, February 6, 2013 10:11:00 PM"; //this is the sample string
SimpleDateFormat formater = new SimpleDateFormat("E, M d, yyyy HH:mm:ss aaa");
Date newPeerdateNtime = formater.parse(PeerdateNtime);

However, there is something wrong and I could not get the date. Is there something I did wrong here?

Upvotes: 0

Views: 1708

Answers (1)

Brian Agnew
Brian Agnew

Reputation: 272237

I think you need "EEE, MMM dd, yyyy hh:mm:ss aa"

Note the hh to indicate the 12-hour clock, vs. HH for the 24-hour clock.

The numbers of formatting characters matter, since they dictate whether the string interpreted is the long or short form (e.g. Wed vs Wednesday)

e.g. from the doc:

Month: If the number of pattern letters is 3 or more, the month is interpreted as text; otherwise, it is interpreted as a number.

Upvotes: 1

Related Questions