if (isPrecisionAllowed(YEAR)) {
setValue((ourYearFormat).parse(theValue));
setPrecision(YEAR);
clearTimeZone();
} else {
throw new DataFormatException("Invalid date/time string (datatype " + getClass().getSimpleName() + " does not support YEAR precision): " + theValue);
}
} else if (theValue.length() == 6 && ourYearMonthPattern.matcher(theValue).matches()) {
// Eg. 198401 (allow this just to be lenient)
if (isPrecisionAllowed(MONTH)) {
setValue((ourYearMonthNoDashesFormat).parse(theValue));
setPrecision(MONTH);
clearTimeZone();
} else {
throw new DataFormatException("Invalid date/time string (datatype " + getClass().getSimpleName() + " does not support DAY precision): " + theValue);
}
} else if (theValue.length() == 7 && ourYearDashMonthPattern.matcher(theValue).matches()) {
// E.g. 1984-01 (this is valid according to the spec)
if (isPrecisionAllowed(MONTH)) {
setValue((ourYearMonthFormat).parse(theValue));
setPrecision(MONTH);
clearTimeZone();
} else {
throw new DataFormatException("Invalid date/time string (datatype " + getClass().getSimpleName() + " does not support MONTH precision): " + theValue);
}
} else if (theValue.length() == 8 && ourYearMonthDayPattern.matcher(theValue).matches()) {
// Eg. 19840101 (allow this just to be lenient)
if (isPrecisionAllowed(DAY)) {
setValue((ourYearMonthDayNoDashesFormat).parse(theValue));
setPrecision(DAY);
clearTimeZone();
} else {
throw new DataFormatException("Invalid date/time string (datatype " + getClass().getSimpleName() + " does not support DAY precision): " + theValue);
}
} else if (theValue.length() == 10 && ourYearDashMonthDashDayPattern.matcher(theValue).matches()) {
// E.g. 1984-01-01 (this is valid according to the spec)
if (isPrecisionAllowed(DAY)) {
setValue((ourYearMonthDayFormat).parse(theValue));
setPrecision(DAY);
clearTimeZone();
} else {
throw new DataFormatException("Invalid date/time string (datatype " + getClass().getSimpleName() + " does not support DAY precision): " + theValue);
}
} else if (theValue.length() >= 18) {
int dotIndex = theValue.indexOf('.', 18);
if (dotIndex == -1 && !isPrecisionAllowed(SECOND)) {
throw new DataFormatException("Invalid date/time string (data type does not support SECONDS precision): " + theValue);
} else if (dotIndex > -1 && !isPrecisionAllowed(MILLI)) {
throw new DataFormatException("Invalid date/time string (data type " + getClass().getSimpleName() + " does not support MILLIS precision):" + theValue);
}
Calendar cal;
try {
cal = DatatypeConverter.parseDateTime(theValue);
} catch (IllegalArgumentException e) {
throw new DataFormatException("Invalid data/time string (" + e.getMessage() + "): " + theValue);
}
myValue = cal.getTime();
if (dotIndex == -1) {
setPrecision(TemporalPrecisionEnum.SECOND);
} else {
setPrecision(TemporalPrecisionEnum.MILLI);
}
clearTimeZone();
if (theValue.endsWith("Z")) {
myTimeZoneZulu = true;
} else if (theValue.indexOf('+', 19) != -1 || theValue.indexOf('-', 19) != -1) {
myTimeZone = cal.getTimeZone();
}
} else {
throw new DataFormatException("Invalid date/time string (invalid length): " + theValue);
}
} catch (ParseException e) {
throw new DataFormatException("Invalid date string (" + e.getMessage() + "): " + theValue);
}
}