29

Can anybody tell me why in the world I got this exception?

08-28 08:47:05.246: D/DateParser(4238): String received for parsing is 2013-08-05T12:13:49.000Z

private final static String DATE_FORMAT_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";


public static Date parseDate(String stringToParse) {
        Date date = null;
        try {
            date = new SimpleDateFormat(DATE_FORMAT_PATTERN).parse(stringToParse);
        } catch (ParseException e) {
            Logger.logError(TAG, e);
        }
        return null;
    }

08-28 08:47:05.246: E/DateParser(4238): Exception: java.text.ParseException: Unparseable date: "2013-08-05T12:13:49.000Z" (at offset 23)
Eugene
  • 57,641
  • 91
  • 219
  • 328

4 Answers4

82

try using

String DATE_FORMAT_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"

The Z at the end is usually the timezone offset. If you you don't need it maybe you can drop it on both sides.

Scary Wombat
  • 43,525
  • 5
  • 33
  • 63
12

Use X instead of Z at the end of the format string:

yyyy-MM-dd'T'HH:mm:ss.SSSX

to parse ISO-8601 format timezone offsets.

(Only works if you use Java 7. See this question).

Community
  • 1
  • 1
Jesper
  • 195,030
  • 44
  • 313
  • 345
2

The Z in your time string is not a valid timezone identifier, but the time format you specified expects a time zone identifier there. More specifically, it expects a RFC 822 timezone identifier, which is usually 4 digits long.

Daniel S.
  • 6,058
  • 4
  • 31
  • 73
0

From java-8 you can directly use ZonedDateTime or Instant if it is in ISO_INSTANT

ZonedDateTime.parse("2013-08-05T12:13:49.000Z")

Instant.parse("2013-08-05T12:13:49.000Z")
Deadpool
  • 33,221
  • 11
  • 51
  • 91