By user775187


2011-06-20 03:04:41 8 Comments

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)

4 comments

@j2gl 2012-10-26 03:37:54

If you don't care about the time zone, you can use this method.

  public static Date convertToDate(String strDate) throws ParseException {
    Date date = null;
    if (strDate != null) {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
      date = sdf.parse(strDate);
    }
    return date;
  }

I don't know if it's still useful for you, but I encounter with the same problem now, and after a little I come up with this.

@Brad Mace 2011-06-20 03:23:04

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");

@user775187 2011-06-20 06:13:48

this doesn't work, +01:00 needs to be +0100 for joda to work.

@Brad Mace 2011-06-20 06:26:14

@user - you're right, the ISO version doesn't like colons. Good news is you can pass the version with colons straight to the DateTime constructor.

@gmhk 2011-06-20 03:10:34

String testDate = "2007-11-02T14:46:03+01:00";
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz");
Date date = formatter.parse(testDate);
System.out.println(date);

You can try similar to the above

You can use following link for reference

@Bala R 2011-06-20 03:08:43

You can try

String format = "yyyy-MM-dd'T'HH:mm:ssz";

Reference : from Javadoc

Text can be quoted using single quotes (') to avoid interpretation.

@Hendra Jaya 2011-06-20 03:19:06

It works only if the string contains 'GMT'. Just like this : String date ="2007-11-02T14:46:03GMT+01:00";

@Bala R 2011-06-20 03:21:49

@Hendra that is true or it has to be String date ="2007-11-02T14:46:03+0100"; SimpleDateTime is is pretty limited in such situations an I guess if you(OP) don't have much control over the input, using libraries such as joda time would work out best.

Related Questions

Sponsored Content

88 Answered Questions

[SOLVED] Is Java "pass-by-reference" or "pass-by-value"?

49 Answered Questions

[SOLVED] Does a finally block always get executed in Java?

42 Answered Questions

[SOLVED] How do I convert a String to an int in Java?

28 Answered Questions

42 Answered Questions

[SOLVED] How do I efficiently iterate over each entry in a Java Map?

55 Answered Questions

[SOLVED] How to create a memory leak in Java?

66 Answered Questions

[SOLVED] How do I generate random integers within a specific range in Java?

  • 2008-12-12 18:20:57
  • user42155
  • 3963726 View
  • 3426 Score
  • 66 Answer
  • Tags:   java random integer

32 Answered Questions

[SOLVED] When to use LinkedList over ArrayList in Java?

58 Answered Questions

[SOLVED] How do I read / convert an InputStream into a String in Java?

Sponsored Content