获取java.lang.IllegalArgumentException:非法模式字符'o'?同时解析java.text.SimpleDateFormat [英] getting java.lang.IllegalArgumentException: Illegal pattern character 'o'? while parsing java.text.SimpleDateFormat
问题描述
我想将字符串转换为java.util.Date。为了相同的目的,我使用以下代码,
String timeStamp =Mon Feb 14 18:15:39 IST 2011;
DateFormat formatter = new SimpleDateFormat(dow mon dd hh:mm:ss zzz yyyy);
日期ts =(Date)formatter.parse(timeStamp);
为SimpleDateFormat()提供的格式为java.util.Date格式。当您将util的Date转换为字符串时,会使用此格式('dow mon dd hh:mm:ss zzz yyyy')。
但是当我执行代码时,它会给我异常。我不知道我需要做什么来摆脱这个问题。我发布了StackTrace的一部分例外。如果任何人知道解决方案,
java.lang.IllegalArgumentException:
上的非法模式字符'o' .text.SimpleDateFormat.compile(SimpleDateFormat.java:769)
at
java.text.SimpleDateFormat.initialize(SimpleDateFormat.java:576)
at
java.text.SimpleDateFormat (SimpleDateFormat.java:501)
java.text.SimpleDateFormat。(SimpleDateFormat.java:476)
提前谢谢。
请改为:
DateFormat formatter = new SimpleDateFormat(EEE MMM dd hh:mm:ss zzz yyyy);
E
用于星期几作为文本, M
是月份名称。
I wanted to convert from string to java.util.Date. for the same purpose I used following code,
String timeStamp = "Mon Feb 14 18:15:39 IST 2011";
DateFormat formatter = new SimpleDateFormat("dow mon dd hh:mm:ss zzz yyyy");
Date ts = (Date)formatter.parse(timeStamp);
The format given to SimpleDateFormat() is format of java.util.Date. When you convert util's Date to string it comes in this format('dow mon dd hh:mm:ss zzz yyyy'). But when I execute code, It gives me Exception. I Don't know what exactly I needed to do to get rid of this problem. I am posting the part of StackTrace of exception. If anybody knows the solution,
java.lang.IllegalArgumentException: Illegal pattern character 'o' at java.text.SimpleDateFormat.compile(SimpleDateFormat.java:769) at java.text.SimpleDateFormat.initialize(SimpleDateFormat.java:576) at java.text.SimpleDateFormat.(SimpleDateFormat.java:501) at java.text.SimpleDateFormat.(SimpleDateFormat.java:476) Thank you in advance.
Try this instead:
DateFormat formatter = new SimpleDateFormat("EEE MMM dd hh:mm:ss zzz yyyy");
E
is used for "Day in Week" as text, M
is the month name.
这篇关于获取java.lang.IllegalArgumentException:非法模式字符'o'?同时解析java.text.SimpleDateFormat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!