1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.seasar.cubby.converter.impl;
17
18 import java.text.DateFormat;
19 import java.text.ParseException;
20 import java.text.ParsePosition;
21 import java.text.SimpleDateFormat;
22 import java.util.Date;
23
24 import org.seasar.cubby.action.MessageInfo;
25 import org.seasar.cubby.converter.ConversionException;
26
27
28
29
30
31
32 public abstract class AbstractDateConverter extends AbstractConverter {
33
34
35
36
37
38
39
40
41
42
43
44
45
46 protected Date toDate(final String value, final String pattern)
47 throws ConversionException {
48 if (value == null || value.length() == 0) {
49 return null;
50 }
51
52 final ParsePosition parsePosition = new ParsePosition(0);
53 final DateFormat dateFormat = new SimpleDateFormat(pattern);
54 dateFormat.setLenient(false);
55 final Date date = dateFormat.parse(value, parsePosition);
56 if (date != null && parsePosition.getIndex() == value.length()) {
57 return date;
58 }
59
60 final MessageInfo messageInfo = new MessageInfo();
61 messageInfo.setKey("valid.dateFormat");
62 throw new ConversionException(messageInfo);
63 }
64
65
66
67
68
69
70
71
72
73
74 protected String toString(final Date date, final String pattern) {
75 final DateFormat dateFormat = new SimpleDateFormat(pattern);
76 return dateFormat.format(date);
77 }
78
79 }