01: package com.salmonllc.util;
02:
03: import java.util.*;
04: import java.text.*;
05:
06: /**
07: * A class that handles some standard date formats automatically.
08: * Creation date: (5/9/01 4:38:51 PM)
09: * @author: Fred Cahill
10: */
11: public class SalmonDateFormat extends java.text.SimpleDateFormat {
12: private static String[] USPARSEDATEFORMATS = { "MM/dd/yyyy",
13: "MM/dd/yy", "MMddyyyy", "MMddyy", "yyyy-MM-dd",
14: "yyyy-MM-dd a hh:mm:ss", "yyyy/MM/dd HH:mm:ss",
15: "MM/dd/yy HH:mm:ss", "yyyy-MM-dd hh:mm:ss.SS",
16: "dd/MM/yyyy", "dd/MM/yyyy HH:mm:ss" };
17:
18: /**
19: * SalmonDateFormat constructor.
20: */
21: public SalmonDateFormat() {
22: super ();
23: }
24:
25: /**
26: * SalmonDateFormat constructor.
27: * @param pattern java.lang.String A pattern of the date to be parsed
28: */
29: public SalmonDateFormat(String pattern) {
30: super (pattern);
31: }
32:
33: /**
34: * SalmonDateFormat constructor.
35: * @param pattern java.lang.String A pattern of the date to be parsed.
36: * @param formatData java.text.DateFormatSymbols See java.text.SimpleDateFormat
37: */
38: public SalmonDateFormat(String pattern,
39: java.text.DateFormatSymbols formatData) {
40: super (pattern, formatData);
41: }
42:
43: /**
44: * SalmonDateFormat constructor comment.
45: * @param pattern java.lang.String A pattern of the date to be parsed
46: * @param loc java.util.Locale See java.text.SimpleDateFormat
47: */
48: public SalmonDateFormat(String pattern, java.util.Locale loc) {
49: super (pattern, loc);
50: }
51:
52: /**
53: * Parses the specified date with the specified pattern, if it fails it uses the predefined patterns of this object.
54: * @param text java.lang.String A string representing the date to be parsed.
55: */
56: public Date parse(String text) throws ParseException {
57: ParseException peThrow;
58: try {
59: return super .parse(text);
60: } catch (ParseException pe) {
61: peThrow = pe;
62: Date dReturn = null;
63: for (int i = 0; i < USPARSEDATEFORMATS.length; i++) {
64: SimpleDateFormat sdf = new SimpleDateFormat(
65: USPARSEDATEFORMATS[i]);
66: try {
67: dReturn = sdf.parse(text);
68: String sDate = sdf.format(dReturn);
69: if (sDate.equals(text))
70: break;
71: dReturn = null;
72: } catch (Exception e) {
73: ;
74: }
75: }
76: if (dReturn == null)
77: throw (peThrow);
78: return dReturn;
79: }
80: }
81: }
|