We have a very strange situation. We expect some data from web service and then we process it in java code. Suddenly, it started to fail.
We found out, that we receive the date in such format: 2007-12-13+01:00 and it throws out an exception: java.lang.NumberFormatException: Invalid date/time. We use JAXB to Marshall the response and expect date to be java.util.Date. So far I didn't find any line of a code which handles the date, no trimming, converting or anything. Just marshaling.
Now there is a dispute whether this format is correct or incorrect and who caused this problem. Funny thing is, that there were no changes done in java code recently, the only difference is in the computer on which the build was made.
Might this be caused by different java version, ANT configuration? Do you have any suggestions what else can cause such problem? If you have any questions, please ask. I'll try to answer if it's possible.
LOG
Yes, see the link below for the official description of the format:
We found out, that we receive the date in such format: 2007-12-13+01:00 and it throws out an exception: java.lang.NumberFormatException: Invalid date/time
java.lang.NumberFormatException: Invalid date/time
at org.apache.axis.encoding.ser.SimpleDeserializer.onEndElement(SimpleDeserializer.java:180)
at org.apache.axis.encoding.DeserializerImpl.endElement(DeserializerImpl.java:502)
at org.apache.axis.encoding.DeserializationContext.endElement(DeserializationContext.java:1087)
at org.apache.axis.message.SAX2EventRecorder.replay(SAX2EventRecorder.java:171)
at org.apache.axis.message.MessageElement.publishToHandler(MessageElement.java:1141)
at org.apache.axis.message.RPCElement.deserialize(RPCElement.java:236)
at org.apache.axis.message.RPCElement.getParams(RPCElement.java:384)
at org.apache.axis.client.Call.invoke(Call.java:2467)
at org.apache.axis.client.Call.invoke(Call.java:2366)
at org.apache.axis.client.Call.invoke(Call.java:1812)
This appears to be a bug in Axis and not JAXB.
Below is an example of working with this format in JAXB.
In this example we will use Date
, Calendar
, and XMLGregorianCalendar
to handle the dates:
import java.util.*;
import javax.xml.bind.annotation.*;
import javax.xml.datatype.XMLGregorianCalendar;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Foo {
private Date date;
private Calendar cal;
private XMLGregorianCalendar xgc;
}
input.xml
Below is the XML document we will unmarshal, note how all the date values are the same as in your question.
<?xml version="1.0" encoding="UTF-8"?>
<foo>
<date>2007-12-13+01:00</date>
<cal>2007-12-13+01:00</cal>
<xgc>2007-12-13+01:00</xgc>
</foo>
Demo
Below is some demo code where we will unmarshal an XML document to populate each of the date fields and then marshal the object back to XML.
import javax.xml.bind.*;
import java.io.File;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Foo.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("input.xml");
Foo foo = (Foo) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(foo, System.out);
}
}
Output
Below is the output from running the demo code, we see that all the values got converted properly. Note that the Date
and Calendar
values default to the default dateTime representation. You can control this using the @XmlSchemaType
annotation.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<foo>
<date>2007-12-12T18:00:00-05:00</date>
<cal>2007-12-13T00:00:00+01:00</cal>
<xgc>2007-12-13+01:00</xgc>
</foo>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With