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
This commit is contained in:
Nick Burch 2013-02-19 16:28:40 +00:00
parent 9f23a59eaa
commit 522168dda4
2 changed files with 35 additions and 1 deletions

View File

@ -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;

View File

@ -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"));
}
}