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
This commit is contained in:
Nick Burch 2010-06-01 15:31:51 +00:00
parent fafc079bfe
commit e6879026b5
3 changed files with 20 additions and 4 deletions

View File

@ -34,6 +34,7 @@
<changes> <changes>
<release version="3.7-SNAPSHOT" date="2010-??-??"> <release version="3.7-SNAPSHOT" date="2010-??-??">
<action dev="POI-DEVELOPERS" type="fix">48872 - allow DateFormatter.formatRawCellContents to handle 1904 as well as 1900 dates</action>
<action dev="POI-DEVELOPERS" type="fix">48872 - handle MMMMM and elapsed time formatting rules in DataFormatter</action> <action dev="POI-DEVELOPERS" type="fix">48872 - handle MMMMM and elapsed time formatting rules in DataFormatter</action>
<action dev="POI-DEVELOPERS" type="fix">48872 - handle zero formatting rules, and better color detection in DataFormatter</action> <action dev="POI-DEVELOPERS" type="fix">48872 - handle zero formatting rules, and better color detection in DataFormatter</action>
<action dev="POI-DEVELOPERS" type="fix">48872 - support for more kinds of formatting in DataFormatter</action> <action dev="POI-DEVELOPERS" type="fix">48872 - support for more kinds of formatting in DataFormatter</action>

View File

@ -511,6 +511,14 @@ public class DataFormatter {
* @see #formatCellValue(Cell) * @see #formatCellValue(Cell)
*/ */
public String formatRawCellContents(double value, int formatIndex, String formatString) { 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? // Is it a date?
if(DateUtil.isADateFormat(formatIndex,formatString) && if(DateUtil.isADateFormat(formatIndex,formatString) &&
DateUtil.isValidExcelDate(value)) { DateUtil.isValidExcelDate(value)) {
@ -519,7 +527,7 @@ public class DataFormatter {
// Hint about the raw excel value // Hint about the raw excel value
((ExcelStyleDateFormatter)dateFormat).setDateToBeFormatted(value); ((ExcelStyleDateFormatter)dateFormat).setDateToBeFormatted(value);
} }
Date d = DateUtil.getJavaDate(value); Date d = DateUtil.getJavaDate(value, use1904Windowing);
return performDateFormatting(d, dateFormat); return performDateFormatting(d, dateFormat);
} }
// else Number // else Number

View File

@ -18,13 +18,12 @@
package org.apache.poi.ss.usermodel; package org.apache.poi.ss.usermodel;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date;
import java.util.Locale; import java.util.Locale;
import org.apache.poi.hssf.usermodel.TestHSSFDataFormatter;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.apache.poi.hssf.usermodel.TestHSSFDataFormatter;
/** /**
* Tests of {@link DataFormatter} * Tests of {@link DataFormatter}
* *
@ -189,4 +188,12 @@ public class TestDataFormatter extends TestCase {
assertEquals("60:00", dfUS.formatRawCellContents(1*hour, -1, "[mm]:ss")); assertEquals("60:00", dfUS.formatRawCellContents(1*hour, -1, "[mm]:ss"));
assertEquals("120:00", dfUS.formatRawCellContents(2*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));
}
} }