I have a spreadsheet with the date 20160831.
Up until now I was using:
if (Cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
if (DateUtil.isCellDateFormatted(cell)) {
// parse the date (irrelevant to this question as ^^ is false)
} else {
Cell.setCellType(Cell.CELL_TYPE_STRING);
// do some work with Cell.getStringCellValue();
}
}
However, the string returned from Cell.getStringCellValue(), in this case, is not "20160831", but, instead, "20160930".
I have printed cell.getNumericCellValue(); which returns 2.016093E7. This is causing me to lose my "1" in "31" upon conversion to String.
I have attempted use DataFormatter in hopes of displaying "data exactly as it appears in Excel". (https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/DataFormatter.html#formatCellValue(org.apache.poi.ss.usermodel.Cell))
I have attempted to use cell.getCellDateValue() regardless, to worse effect.
I have attempted to use DecimalFormat to the same effect.
I have attempted to use NumberToTextConverter to the same effect.
I always lose my "1" in "31". I find this to be the most troubling issue at hand, although I am also getting back the incorrect month value in this date (any insight on that would also be most helpful).
Aside from manually exporting the spreadsheet to a csv file before parsing, I cannot find a way to resolve this issue. Meanwhile, the multitude of other files are working perfectly fine with the code above, as DateUtils typically returns true.
Edit:
I'm adding the full block (which isn't much more than what I originally posted, as well as a screen shot of the spreadsheet, and a copy of the printouts.
private Transitional parseValueForTransitionalWithPropertyAndVector(Cell cell,
Transitional transitional, Property property, Vector vector) {
if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
if (DateUtil.isCellDateFormatted(cell)) {
logger.log(Level.INFO,
"parseValueForTransitionalWithPropertyAndVector() | cellDeterminedToHoldASerialiazedDate");
Calendar calendar = new GregorianCalendar();
Date date = null;
try {
date = cell.getDateCellValue();
calendar.setTime(date);
if (property.getPropTypeKey() == TransitionalHelper.PROP_TYPE_KEY.PAYSTART) {
transitional.setPayPeriodStart(calendar);
logger.log(Level.INFO,
"parseValueForTransitionalWithPropertyAndVector() payStart[MM/DD/YYYY]: "
+ calendar.get(Calendar.MONTH) + "/" + calendar.get(Calendar.DAY_OF_MONTH) + "/"
+ calendar.get(Calendar.YEAR));
} else if (property.getPropTypeKey() == TransitionalHelper.PROP_TYPE_KEY.PAYEND) {
transitional.setPayPeriodEnd(calendar);
logger.log(Level.INFO,
"parseValueForTransitionalWithPropertyAndVector() payEnd[MM/DD/YYYY]: "
+ calendar.get(Calendar.MONTH) + "/" + calendar.get(Calendar.DAY_OF_MONTH) + "/"
+ calendar.get(Calendar.YEAR));
} else if (property.getPropTypeKey() == TransitionalHelper.PROP_TYPE_KEY.PAYDATE) {
transitional.setPayDate(calendar);
logger.log(Level.INFO,
"parseValueForTransitionalWithPropertyAndVector() payDate[MM/DD/YYYY]: "
+ calendar.get(Calendar.MONTH) + "/" + calendar.get(Calendar.DAY_OF_MONTH) + "/"
+ calendar.get(Calendar.YEAR));
}
} catch (IllegalStateException e) {
e.printStackTrace();
}
} else {
// Added to debug >>
System.out.println("cell.getNumericCellValue(): " + cell.getNumericCellValue());
DataFormatter formatter = new DataFormatter();
System.out.println("DataFormatter output: " + formatter.formatCellValue(cell));
System.out.println("DecimalFormat output: " + new DecimalFormat("0").format( cell.getNumericCellValue()));
System.out.println("NumberToTextConverter output: " + NumberToTextConverter.toText(cell.getNumericCellValue()));
cell.setCellType(Cell.CELL_TYPE_STRING);
System.out.println("cell.getStringCellValue(): " + cell.getStringCellValue());
// <<
transitional = parseValueForTransitionalWithPropertyAndVector(cell.getStringCellValue(),
transitional, property, vector);
}
} else {
cell.setCellType(Cell.CELL_TYPE_STRING);
transitional = parseValueForTransitionalWithPropertyAndVector(cell.getStringCellValue(),
transitional, property, vector);
}
return transitional;
}`
For clarification:
A Transitional is the object the parsed values are being attached to. A Property defines how the value in the cell is parsed. A Vector is a map that shows where the value was found.
The output:
cell.getNumericCellValue(): 2.016093E7
DataFormatter output: 20160930
DecimalFormat output: 20160930
NumberToTextConverter output: 20160930
cell.getStringCellValue(): 20160930
A screenshot of the relevant cell from the spreadsheet (I cannot upload the entire spreadsheet):