Getting java.lang.IllegalArgumentException: Invalid wildcard character 'o'? when parsing java.text.SimpleDateFormat

I wanted to convert from string to java.util.Date. For the same purpose, I used the 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 by SimpleDateFormat () is the java.util.Date format. When you convert util Date to a string, it comes in this format ('dow mon dd hh: mm: ss zzz yyyy'). But when I execute the code, it gives me an exception. I don’t know what exactly I needed to do to get rid of this problem. I am sending a StackTrace exception part. If anyone knows a solution,

java.lang.IllegalArgumentException: Invalid template character 'o' at java.text.SimpleDateFormat.compile (SimpleDateFormat.java:769) in java.text.SimpleDateFormat.initialize (SimpleDateFormat.java∗76) in java.text.SimpleDateForm. (SimpleDateFormat.java//01) in java.text.SimpleDateFormat. (SimpleDateFormat.java-00-0076) Thank you in advance.

+3
source share
2 answers

Try this instead:

DateFormat formatter = new SimpleDateFormat("EEE MMM dd hh:mm:ss zzz yyyy");

Eused for "Day of the week" as text, Mis the name of the month.

+13
source

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

+3

Source: https://habr.com/ru/post/1792013/


All Articles