getting java.lang.IllegalArgumentException: Illegal pattern character 'o'? while parsing java.text.SimpleDateFormat

∥☆過路亽.° 提交于 2019-12-20 01:57:10

问题


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.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/4993132/getting-java-lang-illegalargumentexception-illegal-pattern-character-o-while

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!