. lang。IllegalArgumentException:非法模式字符“o”?在解析java.text.SimpleDateFormat

时间:2022-11-20 20:51:54

I wanted to convert from string to java.util.Date. for the same purpose I used following code,

我想把字符串转换成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");
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,

SimpleDateFormat()的格式是java.util.Date的格式。当您将util的日期转换为字符串时,它以这种格式('dow mon dd hh:mm:ss zzz yyyy')。但是当我执行代码时,它会给我一个异常。我不知道我到底需要做些什么才能摆脱这个问题。我发布了异常的StackTrace部分。如果有人知道答案,

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.

. lang。在java.text.SimpleDateFormat. simpledateformat .compile(SimpleDateFormat.java:769)在java.text.SimpleDateFormat. simpledateformat . simpledateformat . simpledateformat .(SimpleDateFormat.java:501)在java.text.SimpleDateFormat.(SimpleDateFormat.java:476)。

2 个解决方案

#1


13  

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.

E被用来作为“星期”的文本,M是月的名字。

#2


3  

You've formatted it wrong I believe, if you look here http://download.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html its E for day of the week and M for month

我认为,如果你看这里http://download.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html,你就会对它进行格式化。

#1


13  

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.

E被用来作为“星期”的文本,M是月的名字。

#2


3  

You've formatted it wrong I believe, if you look here http://download.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html its E for day of the week and M for month

我认为,如果你看这里http://download.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html,你就会对它进行格式化。