Search code examples
javaandroiddate

What is the Time format for this "date": "2014-08-20 00:00:00 -0500"?


I tried converting this date the following way:

SimpleDateFormat fromFormat  = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSSZ");

but I got:

 java.text.ParseException: Unparseable date: "2014-09-20 00:00:00 -0500" (at offset 20)

Solution

  • That "-0500" is the offset from UTC, in RFC822 format. You just want Z, without the SSS.

    The Android SimpleDateFormat docs have it like this in the table:

    • Symbol: Z
    • Meaning: time zone (RFC 822)
    • Kind: (Time Zone)
    • Example: Z/ZZ/ZZZ:-0800 ZZZZ:GMT-08:00 ZZZZZ:-08:00

    I would also personally specify a locale, as a matter of course: this is a machine-readable format rather than a human-oriented format, so I'd usually specify Locale.US:

    SimpleDateFormat format  = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z",
                                                    Locale.US);
    String text = "2014-08-20 00:00:00 -0500";
    System.out.println(format.parse(text));