Search code examples
javaxstream

Xstream date attribute


I want to format the date attribute in my xstream output, i created a date converter which converts tot eh right format however, it creates a new sub-element rather than adding tot he parent element attribute.

I want the following

When i register a date converter for the Date.class.. I get the following:

<output>
<date>20/11/2012</date>

Also having problems with offset there seems to be a difference in actual dates when parsed, for instance... if i parse 20/11/2012 10.30 as the current time.. what gets output is 20/11/2012 10.12... or something different.. do i need to do some conversion or offset?


Solution

  • I created this... seems to do what i want

    private DateTimeFormatter fmt = DateTimeFormat.forPattern("dd/MM/Y HH:mm:ss");
    
        public boolean canConvert(Class type) {
            return type.equals(Date.class);
        }
    
        public Object fromString(String str) {      
            DateTime dt = fmt.parseDateTime(str);
            return dt.toDate();
        }
    
        public String toString(Object obj) {        
            return fmt.print(((Date) obj).getTime());
        }