Anandv
Anandv

Reputation: 107

how to read Date from Excel file

I was trying to read date form an excel using this code

case 2:
   if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC) { 
                        if ((cell.getDateCellValue()) != null) {

                            postAssumpContractPojo.setAssumptionDate(cell.getDateCellValue());

                        }
                    }
   Break;

But the first Condition is getting Flase ( if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC)) And in excel date is in this format (2014-10-14) Please Help ...

Upvotes: 0

Views: 2412

Answers (1)

Shishir Kumar
Shishir Kumar

Reputation: 8191

Check out the method in POI's DateUtil class for dealing with dates in Excelsheets, DateUtil.isCellDateFormatted()

You can get the date as shown below.

if (DateUtil.isCellDateFormatted(cell))
{
   try {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      cellValue = sdf.format(cell.getDateCellValue());

    } catch (ParseException e) {
            e.printStackTrace();
    }
}

Upvotes: 2

Related Questions