1 /*
2 * PROJECT : DAR Runtime and Tools
3 * COPYRIGHT : Copyright (C) 1999-2004 tim.stephenson@enableit.org
4 * LICENSE : GNU LESSER GENERAL PUBLIC LICENSE
5 * Version 2.1, February 1999
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21 package org.enableit.db;
22
23 import java.text.DateFormat;
24 import java.text.SimpleDateFormat;
25 import java.util.Date;
26
27 import org.apache.log4j.Category;
28
29
30 /***
31 * A converter that goes thru a long list of potential date formats in the
32 * hope of finding one that allows an unknown date string to be parsed.
33 *
34 * <p>Obviously the order that the formats are tried reflects the likely format
35 * within my own limited usage so this is really not very
36 * generic (or satisfactory). </p>
37 *
38 * @author TS
39 * @deprecated This is a desperate (and temporary) hack.
40 */
41 public class DateConverter implements org.apache.commons.beanutils.Converter {
42 /***
43 * The Log4J <code>Category</code> doing the logging.
44 */
45 protected static Category logger = Category.getInstance(DateConverter.class);
46
47 /*
48 * Properties
49 */
50
51 /***
52 * CVS info about this class and its current version
53 */
54 public static final String ABOUT = "$Id: DateConverter.java,v 1.7 2004/03/19 19:10:34 tim Exp $";
55
56 /*
57 * Constructors
58 */
59
60 /***
61 * Default Constructor
62 */
63 public DateConverter() {
64 }
65
66 /*
67 * Methods
68 */
69 protected static Object convert(String value) {
70 logger.info("METHOD_ENTRY: convert");
71 logger.info("METHOD_EXIT: convert");
72
73 return new DateConverter().convert(Date.class, value);
74 }
75
76 /***
77 * Method signature required by Converter interface.
78 */
79 public Object convert(Class clazz, Object oValue) {
80 logger.info("METHOD_ENTRY: convert");
81
82 Date date = null;
83 String value = oValue.toString();
84
85 try {
86 // This is the java.util.Date.toString format
87 DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.z");
88
89 date = df.parse(value);
90 } catch (java.text.ParseException e) {
91 try {
92 // Try default locale format
93 DateFormat df = DateFormat.getDateInstance();
94
95 date = df.parse(value);
96 } catch (java.text.ParseException e1) {
97 try {
98 DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
99
100 date = df.parse(value);
101 } catch (java.text.ParseException e2) {
102 try {
103 // ISO date format
104 DateFormat df = new SimpleDateFormat("yyyyMMdd");
105
106 date = df.parse(value);
107 } catch (java.text.ParseException e3) {
108 try {
109 // common UK / Euro format
110 DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
111
112 date = df.parse(value);
113 } catch (java.text.ParseException e4) {
114 try {
115 // US format
116 DateFormat df = new SimpleDateFormat(
117 "MM-dd-yyyy");
118
119 date = df.parse(value);
120 } catch (java.text.ParseException e5) {
121 try {
122 // UK format with alpha month
123 DateFormat df = new SimpleDateFormat(
124 "dd-MMM-yyyy");
125
126 date = df.parse(value);
127 } catch (java.text.ParseException e6) {
128 try {
129 DateFormat df = new SimpleDateFormat(
130 "dd MMM yyyy");
131
132 date = df.parse(value);
133 } catch (java.text.ParseException e7) {
134 try {
135 DateFormat df = new SimpleDateFormat(
136 "dd/MM/yyyy");
137
138 date = df.parse(value);
139 } catch (java.text.ParseException e8) {
140 try {
141 DateFormat df = new SimpleDateFormat(
142 "MM/dd/yyyy");
143
144 date = df.parse(value);
145 } catch (java.text.ParseException e9) {
146 logger.warn(
147 "Cannot parse date from "
148 + value);
149 }
150 }
151 }
152 }
153 }
154 }
155 }
156 }
157 }
158 }
159
160 logger.info("METHOD_EXIT: convert");
161
162 if (date != null) {
163 return date;
164 } else {
165 return value;
166 }
167 }
168 }
This page was automatically generated by Maven