bug 59227: parse dates formatted in Japanese or Chinese. Change javac source encoding from ASCII to UTF-8 (same as build.gradle). Patch from jzhao. This closes #48 on Github.

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1782119 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Javen O'Neal 2017-02-08 08:29:18 +00:00
parent 61d5a268fe
commit 0f09e9eae2
3 changed files with 40 additions and 3 deletions

View File

@ -65,7 +65,7 @@ under the License.
<property name="org.apache.poi.util.POILogger" value="org.apache.poi.util.NullLogger"/> <property name="org.apache.poi.util.POILogger" value="org.apache.poi.util.NullLogger"/>
<!-- issue warnings if source code contains unmappable characters for encoding ASCII --> <!-- issue warnings if source code contains unmappable characters for encoding ASCII -->
<property name="java.source.encoding" value="ASCII"/> <property name="java.source.encoding" value="UTF-8"/>
<scriptdef name="propertyreset" language="javascript" <scriptdef name="propertyreset" language="javascript"
description="Allows to assign @{property} new value"> description="Allows to assign @{property} new value">

View File

@ -49,10 +49,14 @@ public class DateUtil {
private static final Pattern date_ptrn1 = Pattern.compile("^\\[\\$\\-.*?\\]"); private static final Pattern date_ptrn1 = Pattern.compile("^\\[\\$\\-.*?\\]");
private static final Pattern date_ptrn2 = Pattern.compile("^\\[[a-zA-Z]+\\]"); private static final Pattern date_ptrn2 = Pattern.compile("^\\[[a-zA-Z]+\\]");
private static final Pattern date_ptrn3a = Pattern.compile("[yYmMdDhHsS]"); private static final Pattern date_ptrn3a = Pattern.compile("[yYmMdDhHsS]");
private static final Pattern date_ptrn3b = Pattern.compile("^[\\[\\]yYmMdDhHsS\\-T/,. :\"\\\\]+0*[ampAMP/]*$"); // add "\u5e74 \u6708 \u65e5"年月日 for Chinese/Japanese date format:2017年2月7日
private static final Pattern date_ptrn3b = Pattern.compile("^[\\[\\]yYmMdDhHsS\\-T/\u5e74\u6708\u65e5,. :\"\\\\]+0*[ampAMP/]*$");
// elapsed time patterns: [h],[m] and [s] // elapsed time patterns: [h],[m] and [s]
private static final Pattern date_ptrn4 = Pattern.compile("^\\[([hH]+|[mM]+|[sS]+)\\]"); private static final Pattern date_ptrn4 = Pattern.compile("^\\[([hH]+|[mM]+|[sS]+)\\]");
// for format which start with "[DBNum1]" or "[DBNum2]" or "[DBNum3]" could be a Chinese date
private static final Pattern date_ptrn5 = Pattern.compile("^\\[DBNum(1|2|3)\\]");
/** /**
* Given a Date, converts it into a double representing its internal Excel representation, * Given a Date, converts it into a double representing its internal Excel representation,
* which is the number of days since 1/1/1900. Fractional days represent hours, minutes, and seconds. * which is the number of days since 1/1/1900. Fractional days represent hours, minutes, and seconds.
@ -426,7 +430,9 @@ public class DateUtil {
cache(formatString, formatIndex, true); cache(formatString, formatIndex, true);
return true; return true;
} }
// If it starts with [DBNum1] or [DBNum2] or [DBNum3]
// then it could be a Chinese date
fs = date_ptrn5.matcher(fs).replaceAll("");
// If it starts with [$-...], then could be a date, but // If it starts with [$-...], then could be a date, but
// who knows what that starting bit is all about // who knows what that starting bit is all about
fs = date_ptrn1.matcher(fs).replaceAll(""); fs = date_ptrn1.matcher(fs).replaceAll("");

View File

@ -18,6 +18,7 @@
package org.apache.poi.ss.usermodel; package org.apache.poi.ss.usermodel;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
@ -92,4 +93,34 @@ public class TestDateUtil {
assertEquals(expCal, actCal[2]); assertEquals(expCal, actCal[2]);
assertEquals(expCal, actCal[3]); assertEquals(expCal, actCal[3]);
} }
@Test
public void isADateFormat() {
// Cell content 2016-12-8 as an example
// Cell show "12/8/2016"
assertTrue(DateUtil.isADateFormat(14, "m/d/yy"));
// Cell show "Thursday, December 8, 2016"
assertTrue(DateUtil.isADateFormat(182, "[$-F800]dddd\\,\\ mmmm\\ dd\\,\\ yyyy"));
// Cell show "12/8"
assertTrue(DateUtil.isADateFormat(183, "m/d;@"));
// Cell show "12/08/16"
assertTrue(DateUtil.isADateFormat(184, "mm/dd/yy;@"));
// Cell show "8-Dec-16"
assertTrue(DateUtil.isADateFormat(185, "[$-409]d\\-mmm\\-yy;@"));
// Cell show "D-16"
assertTrue(DateUtil.isADateFormat(186, "[$-409]mmmmm\\-yy;@"));
// Cell show "2016年12月8日"
assertTrue(DateUtil.isADateFormat(165, "yyyy\"\"m\"\"d\"\";@"));
// Cell show "2016年12月"
assertTrue(DateUtil.isADateFormat(164, "yyyy\"\"m\"\";@"));
// Cell show "12月8日"
assertTrue(DateUtil.isADateFormat(168, "m\"\"d\"\";@"));
// Cell show "十二月八日"
assertTrue(DateUtil.isADateFormat(181, "[DBNum1][$-404]m\"\"d\"\";@"));
// Cell show "贰零壹陆年壹拾贰月捌日"
assertTrue(DateUtil.isADateFormat(177, "[DBNum2][$-804]yyyy\"\"m\"\"d\"\";@"));
// Cell show "2016年12月8日"
assertTrue(DateUtil.isADateFormat(178, "[DBNum3][$-804]yyyy\"\"m\"\"d\"\";@"));
}
} }