开发者

SimpleDateFormat giving ParseException for unknown reason

I need to format my date (in Java) which is in String format. When I use the following code, taken from an tutorial/reference source, it just gives the following error. I have tried everything I can think of.

My code:

SimpleDateFormat format = new SimpleDateFormat ("E dd/MMM/yy");
java.util.Date date = format.parse ("12/31/2006");
System.out.println (date);

开发者_运维问答This results in the following error:

java.text.ParseException: Unparseable date: "12/31/2006"

I am actually trying to format this string "2011-7-27" but it gave the same error, which resulted me in trying that format for the string I feed to it.

Is there anything you can see which I mess up somewhere?


The code you've given isn't taken from the page you linked to. That format string doesn't appear anywhere on the page.

You've included an "E" in the format specifier, which corresponds to the day of the week - but then you're not providing that. You've also used "MMM" as the month specifier, which would be a textual representation (e.g. "Jul"). Finally, you've specified "yy" for the year, but then given 4 digits. (This won't actually cause an issue - it'll cope with four digits - but it's better to specify the format you actually expect.) If you just want to give "12/31/2006" your format should be "MM/dd/yyyy".

If you actually want to parse "2011-7-27" you should use "yyyy-M-dd" as the format string.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜