将日期格式转换为其他格式时出错 [英] Error when converting date format to another
本文介绍了将日期格式转换为其他格式时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的格式为 YYYY-MM-dd ,我希望将其转换为 MMM dd,yyyy 格式。
I have a String in format "YYYY-MM-dd" and i want convert this into "MMM dd, yyyy" format.
我使用了以下代码来执行此操作;
但是当我转换 2014 -11-18时,输出结果为Sun Dec 29 00:00:00 IST 2013
I used bellow code to do this; But when i convert "2014-11-18" the output is this "Sun Dec 29 00:00:00 IST 2013"
我该如何解决这个问题?
How can I solve this?
DateFormat target=new SimpleDateFormat("MMM dd, yyyy");
String P_date="2014-11-18"
Date test1 = new SimpleDateFormat("YYYY-MM-dd").parse(P_date);
String converted_date=target.format(test1);
Date test=target.parse(converted_date);
推荐答案
y
(小写Y)格式表示年。您使用的 Y
(大写Y)表示WeekYear。
只需使用 y
就可以了:
The y
(lowercase Y) format means "year". Y
(uppercase Y) you were using means "WeekYear".
Just use y
and you should be OK:
DateFormat target=new SimpleDateFormat("MMM dd, yyyy");
String P_date="2014-11-18";
Date test1 = new SimpleDateFormat("yyyy-MM-dd").parse(P_date);
String converted_date=target.format(test1);
Date test=target.parse(converted_date);
这篇关于将日期格式转换为其他格式时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文