I am converting a XML using XStream.
My XML looks like below.
<reportUnit>
<creationDate>2016-02-04T18:01</creationDate>
<description>Days Late Report</description>
<label>Days Late Report</label>
<permissionMask>2</permissionMask>
<updateDate>2014-10-31T19:45</updateDate>
</reportUnit>
My Java code for converting the XML is like
XStream xStream = new XStream();
xStream.alias("reportUnit", ReportUnit.class);
xStream.registerConverter(
new com.thoughtworks.xstream.converters.basic.DateConverter("yyyy-MM-dd HH:mm", new String[] {"dd/MM/yyyy HH:mm"},new GregorianCalendar().getTimeZone()){
public boolean canConvert(Class type) {
return type.equals(Date.class) || type.equals(Timestamp.class);
}
public String toString(Object obj) {
return new SimpleDateFormat("yyyy-MM-dd HH:mm").format((Date) obj);
}
});
xStream.fromXML(objectXml.replace("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>", BLANK));
The above code works for the date format
<creationDate>2016-02-04 18:01</creationDate>
but not for the
<creationDate>2016-02-04T18:01</creationDate>
I am getting an exception as : Cannot parse date 2016-02-04T18:01
I tried using the ISO8601DateConverter from thoughtworks available at below package
"com.thoughtworks.xstream.converters.extended.ISO8601DateConverter"
But that did not resolve my issue ...
Did anyone had the same issue and know how to resolve the same.
The format was incorrect. When I used "yyyy-MM-dd'T'HH:mm" as format the it worked for me.
XStream xStream = new XStream();
xStream.alias("reportUnit", ReportUnit.class);
xStream.registerConverter(
new com.thoughtworks.xstream.converters.basic.DateConverter("yyyy-MM-dd'T'HH:mm", new String[] {"dd/MM/yyyy HH:mm"},new GregorianCalendar().getTimeZone()){
public boolean canConvert(Class type) {
return type.equals(Date.class) || type.equals(Timestamp.class);
}
public String toString(Object obj) {
return new SimpleDateFormat("yyyy-MM-dd HH:mm").format((Date) obj);
}
});
xStream.fromXML(objectXml.replace("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>", BLANK));