1 package com.thoughtworks.xstream.converters.basic;
2
3 import com.thoughtworks.xstream.converters.ConversionException;
4
5 import java.text.DateFormat;
6 import java.text.ParseException;
7 import java.text.SimpleDateFormat;
8 import java.util.Date;
9
10
11 public class DateConverter extends AbstractBasicConverter {
12
13 private DateFormat dateFormat;
14
15 public boolean canConvert(Class type) {
16 return type.equals(Date.class);
17 }
18
19 public DateConverter(DateFormat dateFormat) {
20 this.dateFormat = dateFormat;
21 }
22
23 public DateConverter() {
24 this(new SimpleDateFormat("yyyy-MM-dd HH:mm:ssa"));
25 }
26
27 protected Object fromString(String str) {
28 try {
29 return dateFormat.parse(str);
30 } catch (ParseException e) {
31 throw new ConversionException("Cannot parse date " + str, e);
32 }
33 }
34
35 protected String toString(Object obj) {
36 Date date = (Date) obj;
37 return dateFormat.format(date);
38 }
39
40 }
This page was automatically generated by Maven