javaxmldatetimexstreamoxm

Convert string datetime into Java Long with XStream


I'm trying to get XStream to be able to convert a string that contains a datetime (such as 2013-01-23 16:50:39.495855) into a java.lang.Long instance.

Currently, I have XML like so:

<widget>
    <timestamp val="2013-01-23 16:50:39.495855"/>
</widget>

I want to convert this into a standard Unix epoch timestamp (number of millis since Jan 1, 1970). Since the above datetime translates into a Unix epoch timestamp of (if my math is right) 1358959839000, I'd like XStream to convert this into a new Long(1358959839000) instance.

I don't believe this is possible with XStream's alias methods, and I would probably need to write my own Converter, however a com.thoughtworks.xstream.converters.basic.LongConverter already exists, so I'm not sure how to write my own UnixEpochLongConverter seeing that both converters are attempting to convert a String to a Long. Any ideas? Thanks in advance!


Solution

  • Either register your Custom converter as local using registerLocalConverter or with priority above XStream.PRIORITY_NORMAL.

    xstream.registerLocalConverter(
        Widget.class, 
        "timestamp", 
         new UnixEpochLongConverter());