I am going crazy trying to parse an Excel from Java using Apache POI and I ami finding the following problem parsing a specific column containin dates.
This is the column into my Excel file:
As you can see this column contains date fields but some cells of this column contains number values some other celles contains text values (I can see it using the TYPE() Excel function). I really don't know how it is possible because all the cell contains date fields. Some idea?
Anyway in my code I am trying to handle this strange situation in this way:
if(currentRow.getCell(20) != null && !currentRow.getCell(20).toString().trim().isEmpty()) {
if(currentRow.getCell(20).getCellType().toString().equals("STRING")) {
System.out.println("Data sottoscrizione contratto è STRING: " + currentRow.getCell(20).toString());
}
else if(currentRow.getCell(20).getCellType().toString().equals("NUMERIC")) {
System.out.println("Data sottoscrizione contratto è NUMERIC");
String dateAsString = currentRow.getCell(20).toString();
System.out.println("DATA SOTTOSCRIZIONE CONTRATTO: " + currentRow.getCell(20).toString());
}
}
In this way I can handle both the case trying to convert to a date.
And here my problem. When it found an Excel numeric value in enter into the if NUMERIC case
and printing the cell value by:
System.out.println("DATA SOTTOSCRIZIONE CONTRATTO: " + currentRow.getCell(20).toString());
I obtain printed the value 16-ott-2017 related to the date value 16/10/2017
And here some doubts: Why am I obtaining in this format instead something like 16/10/2017.
16-ott-2017 should be the italian formattation of the date. How can I convert it into a propper Date object?
Buon giorno!
You are currently using the
toString()
method of the cell, which will not be very accurate in returning numeric values or even dates. It might work sometimes, but it won't do always.Use the methods that get you a real value, like
Cell.getNumericCellValue()
,Cell.getDateCellValue()
(outdated because it returns ajava.util.Date
) orCell.getLocalDateTimeCellValue()
. If your cell just contains text, like"16-ott-2020"
, use thegetStringCellValue()
and convert the value returned to aLocalDate
(orLocalDateTime
depends on if time of day matters for you).Here's an example of the conversion (to a
LocalDate
):The output of that code is