in Java, how to parse a date string that contains a letter that does not represent a pattern?
"2007-11-02T14:46:03+01:00"
String date ="2007-11-02T14:46:03+01:00"; String format = "yyyy-MM-ddTHH:mm:ssz"; new SimpleDateFormat(format).parse(date); Exception in thread "main" java.lang.IllegalArgumentException: Illegal pattern character 'T' 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)
Advertisement
Answer
The time you’re trying to parse appears to be in ISO 8601 format. SimpleDateFormat
unfortunately doesn’t support all the same timezone specifiers as ISO 8601. If you want to be able to properly handle all the forms specified in the ISO, the best thing to do is use Joda time.
This example is straight out of the user guide:
DateTime dt = new DateTime("2004-12-13T21:39:45.618-08:00");