SimpleDateFormat的ParseException的 [英] Simpledateformat ParseException

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

问题描述

我需要输入的日期格式更改为我所需的格式。

 字符串时间=星期五,2012 11月02日下午11时58 CET
SimpleDateFormat的displayFormat =
    新的SimpleDateFormat(DD.MM.YYYY,HH:MM);
SimpleDateFormat的parseFormat =
    新的SimpleDateFormat(EEE,DD MMM YYYY HH:MM AA Z);
日期日期= parseFormat.parse(时间);
的System.out.println(输出+ displayFormat.format(日期));

它给了我这个错误

  java.text.ParseException:无法解析的日期:星期五,2012 11月02日下午11时58分CET
    在java.text.DateFormat.parse(来源不明)
    在Main.main(Main.java:10)

能否anyody帮助我吗?因为这code不起作用。


解决方案

看来Android的以Z 不接受的格式XXX时区(如CET) 。 (从的SimpleDateFormat 文档。)

试试这个:

 字符串时间=星期五,2012 11月02日下午11点58分+0100; // CET = + 1小时= +0100
SimpleDateFormat的parseFormat =
    新的SimpleDateFormat(EEE,DD MMM YYYY HH:MM AA Z); //大写字母Z.
日期日期= parseFormat.parse(时间);SimpleDateFormat的displayFormat =
    新的SimpleDateFormat(DD.MM.YYYY,HH:MM);
的System.out.println(输出+ displayFormat.format(日期));


  

输出为2012年2月11日,22:58


注:另外,我觉得你的意思是 HH 而不是 HH ,因为你有 PM

结果显示这里。 (这将使用java7目录的的SimpleDateFormat ,而Android应该支持RFC 822时区( +0100 )为好。)

NB :另外,因为它出现Android的以Z 接受全名(太平洋标准时间是他们给出的例子),你可以只需指定Centural欧洲时间,而不是CET。

I need to change the input date format to my desired format.

String time = "Fri, 02 Nov 2012 11:58 pm CET";
SimpleDateFormat displayFormat = 
    new SimpleDateFormat("dd.MM.yyyy, HH:mm");
SimpleDateFormat parseFormat = 
    new SimpleDateFormat("EEE, dd MMM yyyy HH:mm aa z");
Date date = parseFormat.parse(time);
System.out.println("output is " + displayFormat.format(date));

it gives me this error

java.text.ParseException: Unparseable date: "Fri, 02 Nov 2012 11:58 pm CET"
    at java.text.DateFormat.parse(Unknown Source)
    at Main.main(Main.java:10)

Can anyody help me? Because this code doesn't work.

解决方案

It appears Android's z does not accept time zones in the format XXX (such as "CET"). (Pulling from the SimpleDateFormat documentation.)

Try this instead:

String time = "Fri, 02 Nov 2012 11:58 pm +0100"; // CET = +1hr = +0100
SimpleDateFormat parseFormat = 
    new SimpleDateFormat("EEE, dd MMM yyyy hh:mm aa Z"); // Capital Z
Date date = parseFormat.parse(time);

SimpleDateFormat displayFormat = 
    new SimpleDateFormat("dd.MM.yyyy, HH:mm");
System.out.println("output is " + displayFormat.format(date));

output is 02.11.2012, 22:58

Note: Also, I think you meant hh instead of HH, since you have PM.

Result is shown here. (This uses Java7's SimpleDateFormat, but Android should support RFC 822 timezones (+0100) as well.)

NB: Also, as it appears Android's z accepts full names ("Pacific Standard Time" is the example they give), you could simply specify "Centural European Time" instead of "CET".

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

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