use of org.apache.poi.openxml4j.util.Nullable in project poi by apache.
the class PackagePropertiesPart method setDateValue.
/**
* Convert a string value represented a date into a Nullable<Date>.
*
* @throws InvalidFormatException
* Throws if the date format isnot valid.
*/
private Nullable<Date> setDateValue(String dateStr) throws InvalidFormatException {
if (dateStr == null || dateStr.equals("")) {
return new Nullable<Date>();
}
Matcher m = TIME_ZONE_PAT.matcher(dateStr);
if (m.find()) {
String dateTzStr = dateStr.substring(0, m.start()) + m.group(1) + m.group(2);
for (String fStr : TZ_DATE_FORMATS) {
SimpleDateFormat df = new SimpleDateFormat(fStr, Locale.ROOT);
df.setTimeZone(LocaleUtil.TIMEZONE_UTC);
Date d = df.parse(dateTzStr, new ParsePosition(0));
if (d != null) {
return new Nullable<Date>(d);
}
}
}
String dateTzStr = dateStr.endsWith("Z") ? dateStr : (dateStr + "Z");
for (String fStr : DATE_FORMATS) {
SimpleDateFormat df = new SimpleDateFormat(fStr, Locale.ROOT);
df.setTimeZone(LocaleUtil.TIMEZONE_UTC);
Date d = df.parse(dateTzStr, new ParsePosition(0));
if (d != null) {
return new Nullable<Date>(d);
}
}
//if you're here, no pattern matched, throw exception
StringBuilder sb = new StringBuilder();
int i = 0;
for (String fStr : TZ_DATE_FORMATS) {
if (i++ > 0) {
sb.append(", ");
}
sb.append(fStr);
}
for (String fStr : DATE_FORMATS) {
sb.append(", ").append(fStr);
}
throw new InvalidFormatException("Date " + dateStr + " not well formatted, " + "expected format in: " + sb);
}
Aggregations