0

I'm reading an XLS sheet through Apache POI and I'm having trouble figuring out why this code is not reporting the year of dates correctly.

The Excel sheet lists data like this:

enter image description here

I added out.println so I can monitor the output which appears like this:

enter image description here

As you see above, the code strips out the "20" from 2019 which displays correctly when outputting the contents of cell. Any idea why, and any idea how I can fix it? It seems to work fine if I use cell.iterator, but when I changed the code to account for nulls it no longer works.

for (int r = 1; r < rows; r++) {
    row = (XSSFRow) sheet.getRow(r);
    if (row != null) {
        for (int c = 0; c < cols; c++) {
            cell = row.getCell((short) c);
            out.println("cell = " + cell);
            String x;
            DataFormatter dataFormatter = new DataFormatter();
            x = dataFormatter.formatCellValue(cell);
            out.println("X + " + x + "<br/>");
        }
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
spas2k
  • 499
  • 1
  • 6
  • 15
  • Duplicate: https://stackoverflow.com/a/17282121/600135 – kol Aug 03 '21 at 15:32
  • 1
    You need to customize the `DataFormatter`, as already answered [POI DataFormatter returns 2 digits year instead of 4 digits year for date cells](https://stackoverflow.com/questions/53228302/poi-dataformatter-returns-2-digits-year-instead-of-4-digits-year-for-date-cells/53236531#53236531) – hc_dev Aug 03 '21 at 15:34
  • 1
    Does this answer your question? [POI DataFormatter returns 2 digits year instead of 4 digits year for date cells](https://stackoverflow.com/questions/53228302/poi-dataformatter-returns-2-digits-year-instead-of-4-digits-year-for-date-cells) – hc_dev Aug 03 '21 at 15:34

0 Answers0