01: package uk.org.ponder.conversion;
02:
03: import java.util.Date;
04:
05: import java.text.ParseException;
06: import java.text.SimpleDateFormat;
07:
08: import uk.org.ponder.dateutil.LocalSDF;
09: import uk.org.ponder.util.UniversalRuntimeException;
10:
11: public class DateParser implements LeafObjectParser {
12: // one of these is null
13: private LocalSDF format;
14: private SimpleDateFormat sdf;
15:
16: public DateParser() {
17: format = LocalSDF.w3cformat;
18: }
19:
20: public DateParser(String dateformat) {
21: sdf = new SimpleDateFormat(dateformat);
22: }
23:
24: public Object parse(String bulk) {
25: try {
26: return sdf == null ? format.get().parse(bulk) : sdf
27: .parse(bulk);
28: } catch (ParseException pe) {
29: throw UniversalRuntimeException.accumulate(pe,
30: "Error parsing date " + bulk);
31: }
32: }
33:
34: public String render(Object torendero) {
35: Date torender = (Date) torendero;
36: return sdf == null ? format.format(torender) : sdf
37: .format(torender);
38: }
39:
40: public Object copy(Object tocopy) {
41: return new Date(((Date) tocopy).getTime());
42: }
43:
44: public String toString() {
45: return sdf == null ? null : sdf.toPattern();
46: }
47: }
|