KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > xstream > converters > extended > ISO8601DateConverter


1 package com.thoughtworks.xstream.converters.extended;
2
3 import java.util.Date JavaDoc;
4 import java.util.List JavaDoc;
5 import java.util.Vector JavaDoc;
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 /**
15  * A DateConverter conforming to the ISO8601 standard.
16  * http://www.iso.ch/iso/en/CatalogueDetailPage.CatalogueDetail?CSNUMBER=26780
17  *
18  * @author Mauro Talevi
19  */

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 JavaDoc type) {
28         return type.equals(Date JavaDoc.class);
29     }
30
31     protected Object JavaDoc fromString(String JavaDoc 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 JavaDoc e) {
38                 // try with next formatter
39
}
40         }
41         throw new ConversionException("Cannot parse date " + str);
42     }
43     
44     protected String JavaDoc toString(Object JavaDoc obj) {
45             DateTime dt = new DateTime(obj);
46         return dt.toString();
47     }
48
49     private DateTimeFormatter[] createISOFormatters() {
50         List JavaDoc isoFormatters = new Vector JavaDoc();
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