View Javadoc
1   package net.logAnalyzer.converters;
2   
3   import java.text.ParseException;
4   import java.text.SimpleDateFormat;
5   import java.util.Date;
6   import java.util.Locale;
7   
8   import net.logAnalyzer.handlers.ParsingException;
9   
10  /***
11   * This class implements a converter used to parse or convert
12   * {@link java.util.Date} values.
13   * 
14   * @author Karim REFEYTON
15   * @version 0.1
16   */
17  public class DateConverter extends LAConverter {
18  
19      /***
20       * Default date format.
21       */
22      private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss,SSS";
23  
24      /***
25       * Format used to parse date.
26       */
27      private SimpleDateFormat format = null;
28  
29      /***
30       * Constructs a new converter from a converter definition.
31       * 
32       * @param definition
33       *            Definition of the converter.
34       * @param literal
35       *            Literal value identifying the converter.
36       */
37      public DateConverter(ConverterDefinition definition, String literal) {
38          super(definition, literal);
39      }
40  
41      /***
42       * Returns formatting string for date values.
43       * 
44       * @return Format.
45       */
46      public String getFormat() {
47          if (getOption() != null) {
48              return getOption();
49          } else {
50              return DATE_FORMAT;
51          }
52      }
53  
54      /***
55       * Returns the class {@link java.util.Date}.
56       * 
57       * @see LAConverter#getValueClass()
58       */
59      public Class getValueClass() {
60          return Date.class;
61      }
62  
63      /***
64       * Returns the value parsed with
65       * {@link java.text.DateFormat#parse(java.lang.String)}.
66       * 
67       * @return The parsed value.
68       * @see LAConverter#parse(String)
69       * @throws ParsingException
70       *             If the value can't be parsed.
71       */
72      public Object parse(String value) throws ParsingException {
73          Date parsedDate = null;
74          try {
75              if (format == null) {
76                  format = new SimpleDateFormat(getFormat());
77              }
78              parsedDate = format.parse(value);
79          } catch (ParseException pe) {
80              // Tries with english locale
81              try {
82                  format = new SimpleDateFormat(getFormat(), Locale.ENGLISH);
83                  parsedDate = format.parse(value);
84              } catch (ParseException pe2) {
85                  // Can't parse...
86                  format = new SimpleDateFormat(getFormat());
87                  throw new ParsingException(pe.getMessage(), pe);
88              }
89          }
90          return parsedDate;
91      }
92  
93      /***
94       * Returns a string representation of the value calling
95       * {@link java.text.DateFormat#format(java.util.Date)}.
96       * 
97       * @param value
98       *            Value to convert as String.
99       * @return String representation of the value.
100      * @see LAConverter#toString(Object)
101      */
102     public String toString(Object value) {
103         if (format == null) {
104             format = new SimpleDateFormat(getFormat(), Locale.ENGLISH);
105         }
106         return format.format(value);
107     }
108 }