Search code examples
springapache-poixssfpoi-hssf

Date representation in csv file is to be changed


When i convert xls/xlsx to csv than in that csv file date is coming as Fri Nov 11 00:00:00 IST 1988. But i need yyyy-mm-dd format. What can i do in the following code: [code]

            row = (HSSFRow) rowIterator.next();

            // For each row, iterate through each columns
            Iterator<Cell> cellIterator = row.cellIterator();
            while (cellIterator.hasNext()) {

                cell = (HSSFCell) cellIterator.next();
                    if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC)
                    {
                        if(HSSFDateUtil.isCellDateFormatted(cell))
                        {

                            data.append(cell.getDateCellValue() + "|");

                        }else
                            data.append(cell.getNumericCellValue() + "|");
                    }else if(cell.getCellType() == Cell.CELL_TYPE_BOOLEAN)
                    {
                        data.append(cell.getBooleanCellValue() + "|");
                    } else if(cell.getCellType() == Cell.CELL_TYPE_STRING)
                    {
                        data.append(cell.getStringCellValue() + "|");
                    }  
                    else if(cell.getCellType() == Cell.CELL_TYPE_BLANK)
                    {
                        data.append("" + "|");
                    }
                    else
                    {
                        data.append(cell + ",");

                    }
            }data.append("\r\n");

Solution

  • For date format try this if condition on your date formatted cell.

    if(HSSFDateUtil.isCellDateFormatted(cell))
          {
         // data.append(cell.getDateCellValue() + "|");
          SimpleDateFormat DtFormat = new SimpleDateFormat("yyyy-mm-dd");
          Date date=Test.getRow(RowNum).getCell(CellNum).getDateCellValue();
          data.append(date);
          }