view raw
DanInDC DanInDC - 1 year ago 43
Java Question

simpledateformat parsing date with 'Z' literal

I am trying to parse a date that looks like this:


This is a valid date per The 'Z' literal "imply that UTC
is the preferred reference point for the specified time."

If I try to parse it using SimpleDateFormat and this pattern:


It will be parsed as a Mon Apr 05 17:16:00 EDT 2010

SimpleDateFormat is unable to parse the string with these patterns:


I can explicitly set the TimeZone to use on the SimpleDateFormat to get the expected output, but I don't think that should be necessary. Is there something I am missing? Is there an alternative date parser?


In the pattern, the inclusion of a 'z' date-time component indicates that timezone format needs to conform to the General time zone "standard", examples of which are Pacific Standard Time; PST; GMT-08:00.

A 'Z' indicates that the timezone conforms to the RFC 822 time zone standard, e.g. -0800.

I think you need a DatatypeConverter ...

public void testTimezoneIsGreenwichMeanTime() throws ParseException {
    final Calendar calendar = javax.xml.bind.DatatypeConverter.parseDateTime("2010-04-05T17:16:00Z");
    TestCase.assertEquals("gotten timezone", "GMT+00:00", calendar.getTimeZone().getID());