0

I tried to parse this string 20130718T090000Z using SimpleDateFormat. I do like this:

        String timeString= "20130718T090000Z";
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmssZ");
        Date date= dateFormat.parse(timeString);

I received an Unparsable error. Referencing from here http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html#rfc822timezone I found I did nothing wrong, or did I? Tks.

EyeQ Tech
  • 7,198
  • 18
  • 72
  • 126
  • Tks all for quick and correct answers. I selected @sanbhat's one because he provides more info in his comment. – EyeQ Tech Jul 19 '13 at 08:22

4 Answers4

6

Use this format string "yyyyMMdd'T'HHmmss'Z'" . The reason why it did not parse was, the input had not parsable character Z at the end. "20130718T090000Z"

sanbhat
  • 17,522
  • 6
  • 48
  • 64
  • This will not help under all circumstances as if a time with another time zone appears it will fail. The letter `Z` is used to denote the time zone. – Uwe Plonus Jul 19 '13 at 07:15
  • @UwePlonus unfortunately java doesn't recognize ISO date format, so this is a way to ignore those charaters.http://stackoverflow.com/questions/2201925/converting-iso8601-compliant-string-to-java-util-date – sanbhat Jul 19 '13 at 07:16
  • I know this therefore you have to handle this manually in Java 6. Java 7 has a better support for this. – Uwe Plonus Jul 19 '13 at 07:18
2

Z is for Time Zone. You did not provide the time zone in the date string.

You should have done some thing like String timeString= "20130718T090000-0700";

In case you wanted to treat Z as constant then you need to change the format to yyyyMMdd'T'HHmmss'Z'

Subir Kumar Sao
  • 8,171
  • 3
  • 26
  • 47
0

You have the problem that the Z in your time String is not recognized as a valid time zone.

With Java 6 you have to handle the timezone Z on your own and either remove it or replace it with +0000.

Uwe Plonus
  • 9,803
  • 4
  • 41
  • 48
0

you have to give the value of Z in your input string . as String timeString= "20130718T090000+0000";

This will parse your date according to the format ..

Hope it helps

Taranjit Kaur
  • 216
  • 1
  • 7