package org.apache.poi.ss.util;
import java.text.DateFormatSymbols;
+import java.time.DateTimeException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Calendar;
/**
* Parses a date from a string.
- *
+ *
* @param strVal a string with a date pattern.
* @return a date parsed from argument.
* @throws EvaluationException exception upon parsing.
: LocalDate.now(LocaleUtil.getUserTimeZone().toZoneId()).getYear();
int month = parseMonth(groups.get(format.monthIndex));
int day = Integer.parseInt(groups.get(format.dayIndex));
- return LocalDate.of(year, month, day);
+ try {
+ return LocalDate.of(year, month, day);
+ } catch (DateTimeException e) {
+ throw new DateTimeException("Failed to parse date-string " + strVal);
+ }
}
}
}
@Test
public void test63819() throws IOException {
- simpleTest("63819.xls");
+ LocaleUtil.setUserLocale(Locale.UK);
+ try {
+ simpleTest("63819.xls");
+ } finally {
+ LocaleUtil.resetUserLocale();
+ }
}
/**
evals.add(wb.getCreationHelper().createFormulaEvaluator());
evals.addAll(SIMPLE_REFS.values());
- HSSFFormulaEvaluator.setupEnvironment( files.toArray(new String[0]), evals.toArray(new HSSFFormulaEvaluator[0]) );
- evals.get(0).evaluateAll();
+ try {
+ HSSFFormulaEvaluator.setupEnvironment(files.toArray(new String[0]), evals.toArray(new HSSFFormulaEvaluator[0]));
+ evals.get(0).evaluateAll();
+ } catch (RuntimeException e) {
+ throw new RuntimeException("While handling files " + files + " and evals " + evals, e);
+ }
}
@Test
public void testDateValue() {
- confirmDateValue(new StringEval("2020-02-01"), 43862);
- confirmDateValue(new StringEval("01-02-2020"), 43862);
- confirmDateValue(new StringEval("2020-FEB-01"), 43862);
- confirmDateValue(new StringEval("2020-Feb-01"), 43862);
- confirmDateValue(new StringEval("2020-FEBRUARY-01"), 43862);
- confirmDateValue(new StringEval("FEB-01"), 43862);
- confirmDateValue(new StringEval("2/1/2020"), 43862);
- confirmDateValue(new StringEval("2/1"), 43862);
- confirmDateValue(new StringEval("2020/2/1"), 43862);
- confirmDateValue(new StringEval("2020/FEB/1"), 43862);
- confirmDateValue(new StringEval("FEB/1/2020"), 43862);
- confirmDateValue(new StringEval("2020/02/01"), 43862);
-
- confirmDateValue(new StringEval(""));
- confirmDateValue(BlankEval.instance);
-
- confirmDateValueError(new StringEval("non-date text"));
-
- // // EXCEL
- confirmDateValue(new StringEval("8/22/2011"), 40777); // Serial number of a date entered as text.
- confirmDateValue(new StringEval("22-MAY-2011"), 40685); // Serial number of a date entered as text.
- confirmDateValue(new StringEval("2011/02/23"), 40597); // Serial number of a date entered as text.
-
- // LibreOffice compatibility
- confirmDateValue(new StringEval("1954-07-20"), 19925);
+ LocaleUtil.setUserLocale(Locale.ENGLISH);
+ try {
+ confirmDateValue(new StringEval("2020-02-01"), 43862);
+ confirmDateValue(new StringEval("01-02-2020"), 43862);
+ confirmDateValue(new StringEval("2020-FEB-01"), 43862);
+ confirmDateValue(new StringEval("2020-Feb-01"), 43862);
+ confirmDateValue(new StringEval("2020-FEBRUARY-01"), 43862);
+ confirmDateValue(new StringEval("FEB-01"), 43862);
+ confirmDateValue(new StringEval("2/1/2020"), 43862);
+ confirmDateValue(new StringEval("2/1"), 43862);
+ confirmDateValue(new StringEval("2020/2/1"), 43862);
+ confirmDateValue(new StringEval("2020/FEB/1"), 43862);
+ confirmDateValue(new StringEval("FEB/1/2020"), 43862);
+ confirmDateValue(new StringEval("2020/02/01"), 43862);
+
+ confirmDateValue(new StringEval(""));
+ confirmDateValue(BlankEval.instance);
+
+ confirmDateValueError(new StringEval("non-date text"));
+
+ // // EXCEL
+ confirmDateValue(new StringEval("8/22/2011"), 40777); // Serial number of a date entered as text.
+ confirmDateValue(new StringEval("22-MAY-2011"), 40685); // Serial number of a date entered as text.
+ confirmDateValue(new StringEval("2011/02/23"), 40597); // Serial number of a date entered as text.
+
+ // LibreOffice compatibility
+ confirmDateValue(new StringEval("1954-07-20"), 19925);
+ } finally {
+ LocaleUtil.setUserLocale(null);
+ }
}
private ValueEval invokeDateValue(ValueEval text) {
assertEquals(ErrorEval.class, result.getClass());
assertEquals(ErrorEval.VALUE_INVALID.getErrorCode(), ((ErrorEval) result).getErrorCode());
}
-}
\ No newline at end of file
+}