From 4a10622c7f4bfc9c0f7595434b3144fe654220f4 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Tue, 1 Jun 2010 15:31:51 +0000 Subject: [PATCH] Fix inspired by bug #48872 - allow DateFormatter.formatRawCellContents to handle 1904 as well as 1900 dates git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@950117 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/status.xml | 1 + .../org/apache/poi/ss/usermodel/DataFormatter.java | 10 +++++++++- .../apache/poi/ss/usermodel/TestDataFormatter.java | 13 ++++++++++--- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index d1cae47add..1b213bb52e 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 48872 - allow DateFormatter.formatRawCellContents to handle 1904 as well as 1900 dates 48872 - handle MMMMM and elapsed time formatting rules in DataFormatter 48872 - handle zero formatting rules, and better color detection in DataFormatter 48872 - support for more kinds of formatting in DataFormatter diff --git a/src/java/org/apache/poi/ss/usermodel/DataFormatter.java b/src/java/org/apache/poi/ss/usermodel/DataFormatter.java index 029f24cd40..d0d07c0a3e 100644 --- a/src/java/org/apache/poi/ss/usermodel/DataFormatter.java +++ b/src/java/org/apache/poi/ss/usermodel/DataFormatter.java @@ -511,6 +511,14 @@ public class DataFormatter { * @see #formatCellValue(Cell) */ public String formatRawCellContents(double value, int formatIndex, String formatString) { + return formatRawCellContents(value, formatIndex, formatString, false); + } + /** + * Formats the given raw cell value, based on the supplied + * format index and string, according to excel style rules. + * @see #formatCellValue(Cell) + */ + public String formatRawCellContents(double value, int formatIndex, String formatString, boolean use1904Windowing) { // Is it a date? if(DateUtil.isADateFormat(formatIndex,formatString) && DateUtil.isValidExcelDate(value)) { @@ -519,7 +527,7 @@ public class DataFormatter { // Hint about the raw excel value ((ExcelStyleDateFormatter)dateFormat).setDateToBeFormatted(value); } - Date d = DateUtil.getJavaDate(value); + Date d = DateUtil.getJavaDate(value, use1904Windowing); return performDateFormatting(d, dateFormat); } // else Number diff --git a/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java b/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java index bc3bb67847..35be8c4ff1 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java +++ b/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java @@ -18,13 +18,12 @@ package org.apache.poi.ss.usermodel; import java.util.Calendar; -import java.util.Date; import java.util.Locale; -import org.apache.poi.hssf.usermodel.TestHSSFDataFormatter; - import junit.framework.TestCase; +import org.apache.poi.hssf.usermodel.TestHSSFDataFormatter; + /** * Tests of {@link DataFormatter} * @@ -189,4 +188,12 @@ public class TestDataFormatter extends TestCase { assertEquals("60:00", dfUS.formatRawCellContents(1*hour, -1, "[mm]:ss")); assertEquals("120:00", dfUS.formatRawCellContents(2*hour, -1, "[mm]:ss")); } + + public void testDateWindowing() { + DataFormatter dfUS = new DataFormatter(Locale.US); + + assertEquals("1899-12-31 00:00:00", dfUS.formatRawCellContents(0.0, -1, "yyyy-mm-dd hh:mm:ss")); + assertEquals("1899-12-31 00:00:00", dfUS.formatRawCellContents(0.0, -1, "yyyy-mm-dd hh:mm:ss", false)); + assertEquals("1904-01-01 00:00:00", dfUS.formatRawCellContents(0.0, -1, "yyyy-mm-dd hh:mm:ss", true)); + } } -- 2.39.5