1 package com.thoughtworks.xstream.converters.extended; 2 3 import java.util.Date ; 4 import java.util.List ; 5 import java.util.Vector ; 6 7 import org.joda.time.DateTime; 8 import org.joda.time.format.DateTimeFormatter; 9 import org.joda.time.format.ISODateTimeFormat; 10 11 import com.thoughtworks.xstream.converters.ConversionException; 12 import com.thoughtworks.xstream.converters.basic.AbstractBasicConverter; 13 14 20 public class ISO8601DateConverter extends AbstractBasicConverter{ 21 private DateTimeFormatter[] formatters; 22 23 public ISO8601DateConverter(){ 24 this.formatters = createISOFormatters(); 25 } 26 27 public boolean canConvert(Class type) { 28 return type.equals(Date .class); 29 } 30 31 protected Object fromString(String str) { 32 for (int i = 0; i < formatters.length; i++) { 33 DateTimeFormatter formatter = formatters[i]; 34 try { 35 DateTime dt = formatter.parseDateTime(str); 36 return dt.toDate(); 37 } catch (IllegalArgumentException e) { 38 } 40 } 41 throw new ConversionException("Cannot parse date " + str); 42 } 43 44 protected String toString(Object obj) { 45 DateTime dt = new DateTime(obj); 46 return dt.toString(); 47 } 48 49 private DateTimeFormatter[] createISOFormatters() { 50 List isoFormatters = new Vector (); 51 isoFormatters.add(ISODateTimeFormat.dateTime()); 52 isoFormatters.add(ISODateTimeFormat.dateTimeNoMillis()); 53 isoFormatters.add(ISODateTimeFormat.basicDate()); 54 isoFormatters.add(ISODateTimeFormat.basicDateTime()); 55 isoFormatters.add(ISODateTimeFormat.basicDateTimeNoMillis()); 56 isoFormatters.add(ISODateTimeFormat.basicTime()); 57 isoFormatters.add(ISODateTimeFormat.basicTimeNoMillis()); 58 isoFormatters.add(ISODateTimeFormat.basicTTime()); 59 isoFormatters.add(ISODateTimeFormat.basicTTimeNoMillis()); 60 isoFormatters.add(ISODateTimeFormat.basicWeekDate()); 61 isoFormatters.add(ISODateTimeFormat.basicWeekDateTime()); 62 isoFormatters.add(ISODateTimeFormat.basicWeekDateTimeNoMillis()); 63 isoFormatters.add(ISODateTimeFormat.date()); 64 isoFormatters.add(ISODateTimeFormat.dateHour()); 65 isoFormatters.add(ISODateTimeFormat.dateHourMinute()); 66 isoFormatters.add(ISODateTimeFormat.dateHourMinuteSecond()); 67 isoFormatters.add(ISODateTimeFormat.dateHourMinuteSecondFraction()); 68 isoFormatters.add(ISODateTimeFormat.dateHourMinuteSecondMillis()); 69 isoFormatters.add(ISODateTimeFormat.hour()); 70 isoFormatters.add(ISODateTimeFormat.hourMinute()); 71 isoFormatters.add(ISODateTimeFormat.hourMinuteSecond()); 72 isoFormatters.add(ISODateTimeFormat.hourMinuteSecondFraction()); 73 isoFormatters.add(ISODateTimeFormat.hourMinuteSecondMillis()); 74 isoFormatters.add(ISODateTimeFormat.time()); 75 isoFormatters.add(ISODateTimeFormat.timeNoMillis()); 76 isoFormatters.add(ISODateTimeFormat.tTime()); 77 isoFormatters.add(ISODateTimeFormat.tTimeNoMillis()); 78 isoFormatters.add(ISODateTimeFormat.weekDate()); 79 isoFormatters.add(ISODateTimeFormat.weekDateTime()); 80 isoFormatters.add(ISODateTimeFormat.weekDateTimeNoMillis()); 81 isoFormatters.add(ISODateTimeFormat.weekyear()); 82 isoFormatters.add(ISODateTimeFormat.weekyearWeek()); 83 isoFormatters.add(ISODateTimeFormat.weekyearWeekDay()); 84 return (DateTimeFormatter[])isoFormatters.toArray(new DateTimeFormatter[isoFormatters.size()]); 85 } 86 } 87 | Popular Tags |