Himanshu Yadav Himanshu Yadav - 1 month ago 6
Java Question

Jackson does not support ISO date format

Writing a web service which supports both Xml and JSON media types. Following ISO standard to support date format

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

To support this format in xml I have written a
DateAdapter
class which takes carecof marshaling and un-marshaling of date fields.

There is a CustomSerialization and Deserialization class for Json formats. It's implementation is almost similar to Adapter class. But it throws exception.

@Override
public void serialize(Date aDate, JsonGenerator aJsonGenerator, SerializerProvider aSerializerProvider)
throws IOException, JsonProcessingException {

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
String dateString = dateFormat.format(aDate);
aJsonGenerator.writeString(dateString);
}


Exception:

Illegal pattern character 'X'
at java.text.SimpleDateFormat.compile(SimpleDateFormat.java:768)



Using jackson v2.2.1
Working Adapter implementation is exactly same.


private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");

@Override
public String marshal(final Date v) throws Exception {
System.out.println(v);
return dateFormat.format(v);
}

@Override
public Date unmarshal(final String v) throws Exception {
System.out.println(v);
return dateFormat.parse(v);
}

Answer

Joop Eggen's comment fixed the issue. Simple Date Format from SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); to SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); fixed the issue.

Comments