- // Date formats
- ALL_DATE_FORMATS = new DateFormat[] {
- DEFAULT_DATETIME_FORMAT,
- new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy"),
- new SimpleDateFormat("HH:mm:ss dd MMM yyyy"),
- new SimpleDateFormat("dd MMM yyyy HH:mm:ss"),
- new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss"),
- new SimpleDateFormat("yyyy MMM dd HH:mm:ss"),
- new SimpleDateFormat("MMM dd, yyyy hh:mm:ss aa"),
- ISO_8601_FORMAT, ISO_8601_FORMAT_NOZ
- };
- for (DateFormat df : ALL_DATE_FORMATS) {
- df.setLenient(false);
- }
- }
-
-
- /**
- * Constructor
- * @param inString String containing timestamp
- */
- public Timestamp(String inString)
- {
- _valid = false;
- _text = null;
- if (inString != null && !inString.equals(""))
- {
- // Try each of the parse types in turn
- for (ParseType type : ALL_PARSE_TYPES)
- {
- if (parseString(inString, type))
- {
- ALL_PARSE_TYPES[0] = type;
- _valid = true;
- _text = inString;
- return;
- }
- }
- }
- }
-
- /**
- * Try to parse the given string in the specified way
- * @param inString String to parse
- * @param inType parse type to use
- * @return true if successful
- */
- private boolean parseString(String inString, ParseType inType)
- {
- if (inString == null || inString.equals("")) {
- return false;
- }
- switch (inType)
- {
- case NONE: return false;
- case LONG:
- // Try to parse into a long
- try
- {
- long rawValue = Long.parseLong(inString.trim());
- _milliseconds = getMilliseconds(rawValue);
- return true;
- }
- catch (NumberFormatException nfe)
- {}
- break;
-
- case ISO8601_FRACTIONAL:
- final Matcher fmatcher = ISO8601_FRACTIONAL_PATTERN.matcher(inString);
- if (fmatcher.matches())
- {
- try {
- _milliseconds = getMilliseconds(Integer.parseInt(fmatcher.group(1)), // year
- Integer.parseInt(fmatcher.group(2)), // month
- Integer.parseInt(fmatcher.group(3)), // day
- Integer.parseInt(fmatcher.group(4)), // hour
- Integer.parseInt(fmatcher.group(5)), // minute
- Integer.parseInt(fmatcher.group(6)), // second
- fmatcher.group(7), // fractional seconds
- fmatcher.group(8)); // timezone, if any
- return true;
- }
- catch (NumberFormatException nfe) {}
- }
- break;
-
- case FIXED_FORMAT0: return parseString(inString, ALL_DATE_FORMATS[0]);
- case FIXED_FORMAT1: return parseString(inString, ALL_DATE_FORMATS[1]);
- case FIXED_FORMAT2: return parseString(inString, ALL_DATE_FORMATS[2]);
- case FIXED_FORMAT3: return parseString(inString, ALL_DATE_FORMATS[3]);
- case FIXED_FORMAT4: return parseString(inString, ALL_DATE_FORMATS[4]);
- case FIXED_FORMAT5: return parseString(inString, ALL_DATE_FORMATS[5]);
- case FIXED_FORMAT6: return parseString(inString, ALL_DATE_FORMATS[6]);
- case FIXED_FORMAT7: return parseString(inString, ALL_DATE_FORMATS[7]);
- case FIXED_FORMAT8: return parseString(inString, ALL_DATE_FORMATS[8]);
-
- case GENERAL_STRING:
- if (inString.length() == 19)
- {
- final Matcher matcher = GENERAL_TIMESTAMP_PATTERN.matcher(inString);
- if (matcher.matches())
- {
- try {
- _milliseconds = getMilliseconds(Integer.parseInt(matcher.group(1)),
- Integer.parseInt(matcher.group(2)),
- Integer.parseInt(matcher.group(3)),
- Integer.parseInt(matcher.group(4)),
- Integer.parseInt(matcher.group(5)),
- Integer.parseInt(matcher.group(6)),
- null, null); // no fractions of a second and no timezone
- return true;
- }
- catch (NumberFormatException nfe2) {} // parse shouldn't fail if matcher matched
- }
- }
- return false;
- }
- return false;