I am getting this Date as String
String str = "Fri May 13 2011 19:59:09 GMT 0530 开发者_开发问答(India Standard Time)";
How to convert this Date Object into the format: yyyy-mm-dd HH24:Mi:ss
?
I have not seen any example using SimpleDateFormat with this format.
You need to parse it into a Date
first using SimpleDateFormat
.
Date date = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss Z", Locale.ENGLISH).parse(str);
Then, you can format it using another SimpleDateFormat
instance.
String str2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
Unrelated to the problem, I however wonder where the original string is coming from. It look much like the result of date.toString()
. Are you sure you're doing things the right way? Date should be stored and transferred as java.util.Date
and not as java.lang.String
. You should only convert it to String
at exactly the point whenever you want to display it to the enduser.
精彩评论