开发者

Does simple date format support this conversion?

开发者 https://www.devze.com 2023-03-09 03:55 出处:网络
I am getting this Date as String String str = \"Fri May 13 2011 19:59:09 GMT 0530 开发者_开发问答(India Standard Time)\";

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消