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.converter.ConversionException;
25 import org.seasar.cubby.validator.MessageInfo;
26
27
28
29
30
31
32
33 public abstract class AbstractDateConverter extends AbstractConverter {
34
35
36
37
38
39
40
41
42
43
44
45
46
47 protected Date toDate(final String value, final String pattern)
48 throws ConversionException {
49 if (value == null || value.length() == 0) {
50 return null;
51 }
52
53 final ParsePosition parsePosition = new ParsePosition(0);
54 final DateFormat dateFormat = new SimpleDateFormat(pattern);
55 dateFormat.setLenient(false);
56 final Date date = dateFormat.parse(value, parsePosition);
57 if (date != null && parsePosition.getIndex() == value.length()) {
58 return date;
59 }
60
61 final MessageInfo messageInfo = new MessageInfo();
62 messageInfo.setKey("valid.dateFormat");
63 throw new ConversionException(messageInfo);
64 }
65
66
67
68
69
70
71
72
73
74
75 protected String toString(final Date date, final String pattern) {
76 final DateFormat dateFormat = new SimpleDateFormat(pattern);
77 return dateFormat.format(date);
78 }
79
80 }