01: package net.sourceforge.squirrel_sql.plugins.dataimport.util;
02:
03: /*
04: * Copyright (C) 2007 Thorsten Mürell
05: *
06: * This program is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU General Public License
08: * as published by the Free Software Foundation; either version 2
09: * of the License, or any later version.
10: *
11: * This program is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14: * GNU General Public License for more details.
15: *
16: * You should have received a copy of the GNU General Public License
17: * along with this program; if not, write to the Free Software
18: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19: */
20:
21: import java.text.DateFormat;
22: import java.text.ParseException;
23: import java.text.SimpleDateFormat;
24: import java.util.Date;
25: import java.util.Vector;
26:
27: /**
28: * Contains a date utility function.
29: *
30: * @author Thorsten Mürell
31: */
32: public class DateUtils {
33: private static Vector<DateFormat> formats = new Vector<DateFormat>();
34:
35: static {
36: formats.add(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"));
37: formats.add(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
38: formats.add(new SimpleDateFormat("yyyy-MM-dd"));
39: formats.add(new SimpleDateFormat("HH:mm:ss"));
40: formats.add(new SimpleDateFormat("dd.MM.yyyy"));
41:
42: }
43:
44: /**
45: * This method tries to parse a date value with several formats.
46: *
47: * @param value
48: * @return A date or <code>null</code> if it couldn't be parsed
49: */
50: public static Date parseSQLFormats(String value) {
51:
52: Date parsedDate = null;
53: for (DateFormat f : formats) {
54: parsedDate = parse(f, value);
55: if (parsedDate != null)
56: break;
57: }
58: return parsedDate;
59: }
60:
61: private static Date parse(DateFormat format, String value) {
62: Date d = null;
63: try {
64: d = format.parse(value);
65: } catch (ParseException pe) {
66: /* Do nothing */
67: }
68: return d;
69: }
70:
71: }
|