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
  • 59,186
  • 91
  • 226
  • 333

4 Answers4

84

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
  • 44,617
  • 6
  • 35
  • 64
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
  • 202,709
  • 46
  • 318
  • 350
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,458
  • 4
  • 35
  • 78
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")
Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98