Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format Date Fields to String in XStream

I have a date field, which when converted to xml using XStream, gives time in milliseconds and zone. I just need to convert or format it as "MMMM dd, yyyy HH:mm:ss". How to do that using XStream? I don't want to change the getters and setters. Thanks.

My class:

    public class Datas {
    private String name;
    private Calendar dob;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Calendar getDob() {
        return dob;
    }

    public void setDob(Calendar dob) {
        this.dob = dob;
    }

    public static void main(String[] args) {
        Datas datas = new Datas();
        datas.setName("Ahamed");
        datas.setDob(Calendar.getInstance());
        XStream stream = new XStream();
        System.out.println(stream.toXML(datas));
    }
}

Output:

<Datas>
  <name>Ahamed</name>
  <dob>
    <time>1329081818801</time>
    <timezone>Asia/Calcutta</timezone>
  </dob>
</Datas>

I would like to format the dob tag without changing the getters and setters. Thanks.

like image 744
Ahamed Avatar asked Feb 12 '12 21:02

Ahamed


People also ask

Is XStream thread safe?

For most use cases, the XStream instance is thread-safe, once configured (there are caveats when using annotations) Clear messages are provided during exception handling to help diagnose issues. Starting with version 1.4.


2 Answers

An easy way is to register an (XStream!) DateConverter with the appropriate formats, e.g.:

import com.thoughtworks.xstream.converters.basic.DateConverter;

XStream xstream = new XStream();
String dateFormat = "yyyyMMdd";
String timeFormat = "HHmmss";
String[] acceptableFormats = {timeFormat};
xstream.registerConverter(new DateConverter(dateFormat, acceptableFormats));

This works for me and I didn't need to create a new converter class.

like image 102
djw Avatar answered Sep 22 '22 02:09

djw


Custom converter for Calendar fields:

public class DateConverter implements Converter {

    private SimpleDateFormat formatter = new SimpleDateFormat(
            "MMMM dd, yyyy HH:mm:ss");

    public boolean canConvert(Class clazz) {
        // This converter is only for Calendar fields.
        return Calendar.class.isAssignableFrom(clazz);
    }

    public void marshal(Object value, HierarchicalStreamWriter writer,
            MarshallingContext context) {
        Calendar calendar = (Calendar) value;
        Date date = calendar.getTime();
        writer.setValue(formatter.format(date));
    }

    public Object unmarshal(HierarchicalStreamReader reader,
            UnmarshallingContext context) {
        GregorianCalendar calendar = new GregorianCalendar();
        try {
            calendar.setTime(formatter.parse(reader.getValue()));
        } catch (ParseException e) {
            throw new ConversionException(e.getMessage(), e);
        }
        return calendar;
    }
}

Register the above converter to XStream object as follows:

XStream xStream = new XStream();
xStream.registerConverter(new DateConverter());

Now xStream object will look for Calendar fields and will marshall as defined in Custom Converter.

like image 35
Ahamed Avatar answered Sep 19 '22 02:09

Ahamed