From 522168dda4c36f07ce97102261f0280eb0651eee Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Tue, 19 Feb 2013 16:28:40 +0000 Subject: [PATCH] Fix bug #54579 - Handle milliseconds in date formats eg ss.000 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1447798 13f79535-47bb-0310-9956-ffa450edef68 --- .../ss/usermodel/ExcelStyleDateFormatter.java | 2 +- .../poi/ss/usermodel/TestDataFormatter.java | 34 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/java/org/apache/poi/ss/usermodel/ExcelStyleDateFormatter.java b/src/java/org/apache/poi/ss/usermodel/ExcelStyleDateFormatter.java index a1307ee06..22c8b541a 100644 --- a/src/java/org/apache/poi/ss/usermodel/ExcelStyleDateFormatter.java +++ b/src/java/org/apache/poi/ss/usermodel/ExcelStyleDateFormatter.java @@ -84,7 +84,7 @@ public class ExcelStyleDateFormatter extends SimpleDateFormat { t = t.replaceAll("\\[mm\\]", String.valueOf(MM_BRACKET_SYMBOL)); t = t.replaceAll("\\[s\\]", String.valueOf(S_BRACKET_SYMBOL)); t = t.replaceAll("\\[ss\\]", String.valueOf(SS_BRACKET_SYMBOL)); - t = t.replaceAll("s.000", "s.S"); + t = t.replaceAll("s.000", "s.SSS"); t = t.replaceAll("s.00", "s." + LL_BRACKET_SYMBOL); t = t.replaceAll("s.0", "s." + L_BRACKET_SYMBOL); return t; diff --git a/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java b/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java index 8bc4f0f26..c6233fcf3 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java +++ b/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java @@ -21,7 +21,9 @@ package org.apache.poi.ss.usermodel; +import java.text.DateFormat; import java.util.Calendar; +import java.util.Date; import java.util.Locale; import junit.framework.TestCase; @@ -489,4 +491,36 @@ public class TestDataFormatter extends TestCase { fmt = "0 \"dollars and\" .00 \"cents\""; assertEquals("19 dollars and .99 cents", dfUS.formatRawCellContents(19.99, -1, fmt)); } + + /** + * ExcelStyleDateFormatter should work for Milliseconds too + */ + public void testExcelStyleDateFormatterStringOnMillis() { + // Test directly with the .000 style + DateFormat formatter1 = new ExcelStyleDateFormatter("ss.000"); + + assertEquals("00.001", formatter1.format(new Date(1L))); + assertEquals("00.010", formatter1.format(new Date(10L))); + assertEquals("00.100", formatter1.format(new Date(100L))); + assertEquals("01.000", formatter1.format(new Date(1000L))); + assertEquals("01.001", formatter1.format(new Date(1001L))); + assertEquals("10.000", formatter1.format(new Date(10000L))); + assertEquals("10.001", formatter1.format(new Date(10001L))); + + // Test directly with the .SSS style + DateFormat formatter2 = new ExcelStyleDateFormatter("ss.SSS"); + + assertEquals("00.001", formatter2.format(new Date(1L))); + assertEquals("00.010", formatter2.format(new Date(10L))); + assertEquals("00.100", formatter2.format(new Date(100L))); + assertEquals("01.000", formatter2.format(new Date(1000L))); + assertEquals("01.001", formatter2.format(new Date(1001L))); + assertEquals("10.000", formatter2.format(new Date(10000L))); + assertEquals("10.001", formatter2.format(new Date(10001L))); + + + // Test via DataFormatter + DataFormatter dfUS = new DataFormatter(Locale.US, true); + assertEquals("01.010", dfUS.formatRawCellContents(0.0000116898, -1, "ss.000")); + } }