Java日期格式ParseException [英] Java Date Formatting ParseException

查看:114
本文介绍了Java日期格式ParseException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的字符串为 2014年8月1日,星期五。我想设置其格式并显示为 2014-08-01

I have a String as Friday, August 01, 2014. I want to format this and show as 2014-08-01.

我已经尝试过了。但这给出了 java.text.ParseException:无法解析的日期: 2014年8月1日,星期五

I have tried this. but this gave java.text.ParseException: Unparseable date: "Friday, August 01, 2014"

SimpleDateFormat sdf = new SimpleDateFormat("E, MM d, yyyy");
String dateInString = "Friday, August 01, 2014";
Date date = sdf.parse(dateInString);
System.out.println(date);

我该怎么做?

推荐答案

您需要阅读 SimpleDateFormat API ,因为在此已对此进行了详细说明。

You need to read the SimpleDateFormat API as it's all well explained there.

请注意以下API说明:

Note this explanation from the API:



  • 文本:对于格式设置,如果图案字母的数量为4个或更多,则使用完整格式;否则,请使用简短形式或缩写形式。对于解析,两种格式都可以接受,而与模式字母的数量无关。

  • 数字:对于格式设置,模式字母的数量是最小位数,较短的数字将被零填充到该数量。对于解析,除非需要分隔两个相邻字段,否则忽略模式字母的数量。

  • Text: For formatting, if the number of pattern letters is 4 or more, the full form is used; otherwise a short or abbreviated form is used if available. For parsing, both forms are accepted, independent of the number of pattern letters.
  • Number: For formatting, the number of pattern letters is the minimum number of digits, and shorter numbers are zero-padded to this amount. For parsing, the number of pattern letters is ignored unless it's needed to separate two adjacent fields.

因此,例如,MM对应于数字月份,而不是月份名称。对于完整的月份名称,我将使用 MMMM ,对于完整的星期名称,我将使用 EEEE 。我会使用 dd 来表示两位数字,例如01。

So for instance, MM corresponds to a numeric month, not the month name. For the complete month name, I'd use MMMM, and for the complete week name, I'd use EEEE. I'd use dd for a two digit date such as 01.

例如,

SimpleDateFormat sdf = new SimpleDateFormat("EEEE, MMMM dd, yyyy");

这篇关于Java日期格式ParseException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆