diff options
author | PJ Fanning <fanningpj@apache.org> | 2021-04-15 22:13:09 +0000 |
---|---|---|
committer | PJ Fanning <fanningpj@apache.org> | 2021-04-15 22:13:09 +0000 |
commit | e7a2df7dda2458b78a4d5495703289d3ec31c69f (patch) | |
tree | 99a7478d9aabfa9159051cf2fc1cb0593e424d3a | |
parent | 3b1c82f6cba7e140e2a8dbe5c35aee96d73dbdb7 (diff) | |
download | poi-e7a2df7dda2458b78a4d5495703289d3ec31c69f.tar.gz poi-e7a2df7dda2458b78a4d5495703289d3ec31c69f.zip |
[github-234] Ensure the hours position is correct. Thanks to Anthony Schott. This closes #234
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1888804 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r-- | poi/src/main/java/org/apache/poi/ss/format/CellDateFormatter.java | 21 | ||||
-rw-r--r-- | poi/src/main/java/org/apache/poi/ss/format/CellFormatPart.java | 8 | ||||
-rw-r--r-- | poi/src/main/java9/module-info.class | bin | 3387 -> 3388 bytes | |||
-rw-r--r-- | poi/src/test/java9/module-info.class | bin | 4134 -> 4135 bytes | |||
-rw-r--r-- | test-data/spreadsheet/DateFormatTests.xlsx | bin | 13322 -> 12394 bytes |
5 files changed, 23 insertions, 6 deletions
diff --git a/poi/src/main/java/org/apache/poi/ss/format/CellDateFormatter.java b/poi/src/main/java/org/apache/poi/ss/format/CellDateFormatter.java index b1d7f24431..4a963d7d3d 100644 --- a/poi/src/main/java/org/apache/poi/ss/format/CellDateFormatter.java +++ b/poi/src/main/java/org/apache/poi/ss/format/CellDateFormatter.java @@ -44,7 +44,7 @@ public class CellDateFormatter extends CellFormatter { private static /* final */ CellDateFormatter SIMPLE_DATE; - private class DatePartHandler implements CellFormatPart.PartHandler { + class DatePartHandler implements CellFormatPart.PartHandler { private int mStart = -1; private int mLen; private int hStart = -1; @@ -94,7 +94,10 @@ public class CellDateFormatter extends CellFormatter { case 'y': case 'Y': mStart = -1; - if (part.length() == 3) + // See https://issues.apache.org/bugzilla/show_bug.cgi?id=53369 + if (part.length() == 1) + part = "yy"; + else if (part.length() == 3) part = "yyyy"; return part.toLowerCase(Locale.ROOT); @@ -125,6 +128,15 @@ public class CellDateFormatter extends CellFormatter { } } + public void updatePositions(int pos, int offset) { + if (pos < hStart) { + hStart += offset; + } + if (pos < mStart) { + mStart += offset; + } + } + public void finish(StringBuffer toAppendTo) { if (hStart >= 0 && !showAmPm) { for (int i = 0; i < hLen; i++) { @@ -155,10 +167,7 @@ public class CellDateFormatter extends CellFormatter { StringBuffer descBuf = CellFormatPart.parseFormat(format, CellFormatType.DATE, partHandler); partHandler.finish(descBuf); - // tweak the format pattern to pass tests on JDK 1.7, - // See https://issues.apache.org/bugzilla/show_bug.cgi?id=53369 - String ptrn = descBuf.toString().replaceAll("((y)(?!y))(?<!yy)", "yy"); - dateFmt = new SimpleDateFormat(ptrn, locale); + dateFmt = new SimpleDateFormat(descBuf.toString(), locale); dateFmt.setTimeZone(LocaleUtil.getUserTimeZone()); } diff --git a/poi/src/main/java/org/apache/poi/ss/format/CellFormatPart.java b/poi/src/main/java/org/apache/poi/ss/format/CellFormatPart.java index 5aaec56b19..9a476d503d 100644 --- a/poi/src/main/java/org/apache/poi/ss/format/CellFormatPart.java +++ b/poi/src/main/java/org/apache/poi/ss/format/CellFormatPart.java @@ -535,12 +535,20 @@ public class CellFormatPart { int pos = 0; while ((pos = fmt.indexOf("''", pos)) >= 0) { fmt.delete(pos, pos + 2); + if (partHandler instanceof CellDateFormatter.DatePartHandler) { + CellDateFormatter.DatePartHandler datePartHandler = (CellDateFormatter.DatePartHandler) partHandler; + datePartHandler.updatePositions(pos, -2); + } } // Now the final pass for quoted chars: Replace any \u0000 with '' pos = 0; while ((pos = fmt.indexOf("\u0000", pos)) >= 0) { fmt.replace(pos, pos + 1, "''"); + if (partHandler instanceof CellDateFormatter.DatePartHandler) { + CellDateFormatter.DatePartHandler datePartHandler = (CellDateFormatter.DatePartHandler) partHandler; + datePartHandler.updatePositions(pos, 1); + } } } diff --git a/poi/src/main/java9/module-info.class b/poi/src/main/java9/module-info.class Binary files differindex 65a7086587..86d7379bf9 100644 --- a/poi/src/main/java9/module-info.class +++ b/poi/src/main/java9/module-info.class diff --git a/poi/src/test/java9/module-info.class b/poi/src/test/java9/module-info.class Binary files differindex 2cd1356efd..fabf8f27ed 100644 --- a/poi/src/test/java9/module-info.class +++ b/poi/src/test/java9/module-info.class diff --git a/test-data/spreadsheet/DateFormatTests.xlsx b/test-data/spreadsheet/DateFormatTests.xlsx Binary files differindex c6048309b8..31917ca968 100644 --- a/test-data/spreadsheet/DateFormatTests.xlsx +++ b/test-data/spreadsheet/DateFormatTests.xlsx |