Summary: | with POI upgrade to 5.0.0, we see that it formats dates with extra slash | ||
---|---|---|---|
Product: | POI | Reporter: | hethal <hethal.java> |
Component: | XSSF | Assignee: | POI Developers List <dev> |
Status: | RESOLVED WORKSFORME | ||
Severity: | regression | ||
Priority: | P2 | ||
Version: | 5.0.0-FINAL | ||
Target Milestone: | --- | ||
Hardware: | PC | ||
OS: | All | ||
Attachments: | spreadsheet compare file with date format mismatch between 4.1.2 and 5.0.0. |
Description
hethal
2021-10-06 20:49:34 UTC
If this has changed, it is not likely to be intentional. Could you provide a more complex code sample for review? here is the code that we are using in our app return new DataFormatter() { @Override public String formatRawCellContents (double value, int formatIndex, String formatString) { if ("GENERAL".equalsIgnoreCase(formatString)) { return decimalFormat.format(value); } else if (DateUtil.isADateFormat(formatIndex, formatString) && DateUtil.isValidExcelDate(value)) { DateFormat dateFormat; if (StringUtils.containsIgnoreCase(formatString, "h")) { dateFormat = new SimpleDateFormat("dd MMM yyyy HH:mm:ss"); } else { dateFormat = new SimpleDateFormat("dd MMM yyyy"); } Date date = DateUtil.getJavaDate(value, false); return dateFormat.format(date); } else { return super.formatRawCellContents(value, formatIndex, formatString); } } }; you can take a look at the below code: dateFormat = new SimpleDateFormat("dd MMM yyyy HH:mm:ss"); I can't reproduce any issue - I put a test case at https://github.com/pjfanning/poi-gradle-example/tree/bug-65619 (NB a branch of this project called bug-65619). The code in this branch gives the same result if the build.gradle has poi 5.0.0 or poi 4.1.2. Please take a look at the below code that we are using for our app: public Builder dataFormat (String format) { return new Builder(fontTransform, cellStyleTransform.andThen( (cs, ch) -> cs.setDataFormat(ch.createDataFormat().getFormat("dd mmm yyyy")))); } and looks like this code is using the `setDataFormat`, `createDataFormat` and `getFormat` methods that are of the POI 5.0.0 jar. This is not a reproducible use case - maybe someone else will have a look but I don't have time to look at this without a reproducible use case. the code in last comment is not POI code - you probably need someone in your company to use a Java debugger to debug the code Created attachment 38059 [details]
spreadsheet compare file with date format mismatch between 4.1.2 and 5.0.0.
Showing that there are back slashes in POI 5.0.0 that are not present in POI 4.1.2
Below here is the code that we tried on POI 5.0.0 and POI 4.1.2 as well and were able to reproduce that the POI 5.0.0 added the extra slashes as attache d in the jpg image. Code: import java.io.FileOutputStream; import java.io.IOException; import java.util.Date; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.CreationHelper; import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.usermodel.HorizontalAlignment; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.xssf.streaming.SXSSFSheet; import org.apache.poi.xssf.streaming.SXSSFWorkbook; import org.junit.Test; public class TestPoi { @Test public void test () throws IOException { SXSSFWorkbook workbook = new SXSSFWorkbook(); SXSSFSheet sheet = workbook.createSheet("Test Sheet 1"); Font font = workbook.createFont(); CellStyle cs = workbook.createCellStyle(); cs.setFont(font); Row row = sheet.createRow(0); Cell cell = row.createCell(1); cell.setCellValue(new Date()); cell.setCellStyle(cs); CreationHelper ch = workbook.getCreationHelper(); cs.setDataFormat(ch.createDataFormat().getFormat("dd MMM yyyy HH:mm:ss")); cs.setAlignment(HorizontalAlignment.RIGHT); FileOutputStream fos = new FileOutputStream("c:/temp/test.xlsx"); workbook.write(fos); fos.close(); workbook.close(); workbook.dispose(); } } I ran the sample code and my output was fine (no slashes) - I have this in the styles.xml - <numFmt numFmtId="164" formatCode="dd MMM yyyy HH:mm:ss"/> I added this passing regression test - r1894016 It seems even with your test-case, it is not possible to reproduce this for us. There are various reasons that can be related, e.g. using different locales, using some unusual version of Java or other differences in your environment compared to more usual setups. Maybe you can try some more to narrow down why the test-case works fine for us, but seems to cause problems for you. If you find something that should be improved/fixed in Apache POI, please re-open this issue with more comments. |