Search in sources :

Example 1 with Nullable

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);
}
Also used : Matcher(java.util.regex.Matcher) SimpleDateFormat(java.text.SimpleDateFormat) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) Nullable(org.apache.poi.openxml4j.util.Nullable) Date(java.util.Date) ParsePosition(java.text.ParsePosition)

Aggregations

ParsePosition (java.text.ParsePosition)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 Matcher (java.util.regex.Matcher)1 InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)1 Nullable (org.apache.poi.openxml4j.util.Nullable)1