使用XStream进行XML转换:无法解析格式2016-02-04T18:01的日期(XML conversion by using XStream : not able parse date of format 2016-02-04T18:01)

我正在使用XStream转换XML。

我的XML如下所示。

<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>

我用于转换XML的Java代码就像

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));

上面的代码适用于日期格式

<creationDate>2016-02-04 18:01</creationDate>

但不适合

<creationDate>2016-02-04T18:01</creationDate>

我得到一个例外:无法解析日期2016-02-04T18:01

我尝试使用下面包中提供的thinkworks的ISO8601DateConverter

"com.thoughtworks.xstream.converters.extended.ISO8601DateConverter"

但那并没有解决我的问题......

有没有人有同样的问题,知道如何解决相同的问题。

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.

最满意答案

格式不正确。 当我使用“yyyy-MM-dd'T'HH:mm”格式时,它对我有用。

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));

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));

更多推荐