01: package simpleorm.simplewebapp.scalarFields;
02:
03: import simpleorm.simplewebapp.core.WField;
04: import simpleorm.simplewebapp.core.WValidationException;
05:
06: import java.text.SimpleDateFormat;
07: import java.text.ParseException;
08: import java.util.Date;
09:
10: /**
11: * Reads and writes date (not time) values.
12: * Generally prefers ISO yyyy.mm.dd format, tolerant of many input formats.
13: * A small window into the Java Date Disaster.
14: * todo Clean up. I18N, timezones, java.sql.date, Long/Short formats that make sense etc.
15: */
16: public class WFieldDate extends WField {
17: static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
18:
19: public WFieldDate(String name) {
20: super (name);
21: }
22:
23: public Date getDateValue(Date defalt) {
24: return value != null ? (Date) value : defalt;
25: }
26:
27: protected String format() {
28: if (value == null)
29: return null;
30: return format.format(value);
31: }
32:
33: protected void parse(String rawText) throws Exception {
34: if (rawText == null)
35: this .value = null;
36: else {
37: try {
38: this .value = format.parse(rawText);
39: } catch (ParseException ex) {
40: try {
41: this .value = new Date(rawText); // conveniently takes many formats, unlike any of its replacements!
42: } catch (IllegalArgumentException ex2) {
43: throw new WValidationException("ParseException",
44: this );
45: }
46: }
47: }
48: this .rawText = format();
49: }
50:
51: public Class getValueClass() {
52: return Date.class;
53: }
54: }
|