/** * Reads the serialized version of this object. If the serialized data is only version 0, then the * date for the start of the century for interpreting two digit years is computed. The pattern is * parsed and compiled following the process of reading in the serialized data. * * @param stream the object stream to read the data from. * @throws IOException if an I/O error occurs. * @throws ClassNotFoundException if the class of the serialized data could not be found. * @throws InvalidObjectException if the pattern is invalid. */ private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { stream.defaultReadObject(); if (serialVersionOnStream < 1) { computeCenturyStart(); serialVersionOnStream = 1; } else // Ensure that defaultCentury gets set. set2DigitYearStart(defaultCenturyStart); // Set up items normally taken care of by the constructor. tokens = new ArrayList(); try { compileFormat(pattern); } catch (IllegalArgumentException e) { throw new InvalidObjectException("The stream pattern was invalid."); } }
/** * Parses the date value using the given date formats. * * @param dateValue the date value to parse * @param dateFormats the date formats to use * @param startDate During parsing, two digit years will be placed in the range <code>startDate * </code> to <code>startDate + 100 years</code>. This value may be <code>null</code>. When * <code>null</code> is given as a parameter, year <code>2000</code> will be used. * @return the parsed date * @throws DateParseException if none of the dataFormats could parse the dateValue */ public static Date parseDate(String dateValue, Collection dateFormats, Date startDate) throws DateParseException { if (dateValue == null) { throw new IllegalArgumentException("dateValue is null"); } if (dateFormats == null) { dateFormats = DEFAULT_PATTERNS; } if (startDate == null) { startDate = DEFAULT_TWO_DIGIT_YEAR_START; } // trim single quotes around date if present // see issue #5279 if (dateValue.length() > 1 && dateValue.startsWith("'") && dateValue.endsWith("'")) { dateValue = dateValue.substring(1, dateValue.length() - 1); } SimpleDateFormat dateParser = null; Iterator formatIter = dateFormats.iterator(); while (formatIter.hasNext()) { String format = (String) formatIter.next(); if (dateParser == null) { dateParser = new SimpleDateFormat(format, Locale.US); dateParser.setTimeZone(TimeZone.getTimeZone("GMT")); dateParser.set2DigitYearStart(startDate); } else { dateParser.applyPattern(format); } try { return dateParser.parse(dateValue); } catch (ParseException pe) { // ignore this exception, we will try the next format } } // we were unable to parse the date throw new DateParseException("Unable to parse the date " + dateValue); }
/** * Returns a copy of this instance of <code>SimpleDateFormat</code>. The copy contains clones of * the formatting symbols and the 2-digit year century start date. */ public Object clone() { SimpleDateFormat clone = (SimpleDateFormat) super.clone(); clone.setDateFormatSymbols((DateFormatSymbols) formatData.clone()); clone.set2DigitYearStart((Date) defaultCenturyStart.clone()); return clone; }
// Compute the start of the current century as defined by // get2DigitYearStart. private void computeCenturyStart() { int year = calendar.get(Calendar.YEAR); calendar.set(Calendar.YEAR, year - 80); set2DigitYearStart(calendar.getTime()); }