1 package com.thoughtworks.xstream.converters.extended; 2 3 import com.thoughtworks.xstream.converters.Converter; 4 import com.thoughtworks.xstream.converters.MarshallingContext; 5 import com.thoughtworks.xstream.converters.UnmarshallingContext; 6 import com.thoughtworks.xstream.io.HierarchicalStreamReader; 7 import com.thoughtworks.xstream.io.HierarchicalStreamWriter; 8 import com.thoughtworks.xstream.core.JVM; 9 10 import java.util.GregorianCalendar ; 11 import java.util.Date ; 12 13 19 public class GregorianCalendarConverter implements Converter { 20 21 public boolean canConvert(Class type) { 22 return type.equals(GregorianCalendar .class); 23 } 24 25 public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) { 26 GregorianCalendar calendar = (GregorianCalendar ) source; 27 writer.startNode("time"); 28 long timeInMillis = calendar.getTime().getTime(); writer.setValue(String.valueOf(timeInMillis)); 30 writer.endNode(); 31 } 32 33 public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) { 34 reader.moveDown(); 35 long timeInMillis = Long.parseLong(reader.getValue()); 36 reader.moveUp(); 37 38 GregorianCalendar result = new GregorianCalendar (); 39 result.setTime(new Date (timeInMillis)); 41 return result; 42 } 43 44 } 45 | Popular Tags |