Mixed Excel date columns often contain true date values, text strings, and empty cells. In Axyra Sheets, cell.dateValue() handles only numeric cell values, so text dates return null and can trigger a NullPointerException during validation.
The problem
This issue affects operators cleaning older Excel workbooks before sorting, filtering, or loading data into another system. A date column may contain proper Excel date values alongside text such as 2026/01/01, 2026-01-01, or 2026 01 01.
The problem came up in the Stack Overflow community while iterating through sheet.usedRange() in Axyra Sheets. The workbook contained mixed cell types, and calling cell.dateValue() for every date column did not produce a LocalDate for text cells.
Excel stores many real dates as numbers with a date format. Axyra exposes those values as CellValue.Number, and cell.dateValue() can convert them to LocalDate. A text date is different. Axyra exposes it as CellValue.Text, so cell.dateValue() returns null.
Blank cells also need explicit handling. An empty date may mean that the date is unknown, not that the workbook is broken. Treating every null as a valid date causes errors later when fixDate expects a non-null LocalDate.
The solution
Read the cell's CellValue first, then choose the conversion method based on its type:
CellValue.Number: callcell.dateValue().CellValue.Text: trim the text and pass it to a date parser.CellValue.Blank: returnnulland let the validation process decide whether an unknown date is acceptable.- Any other value: raise an
IllegalStateException, because the cell contains an unexpected type.
The key is to separate date reading from date validation. The readDate method should determine what date, if any, the cell represents. The fixDate method should then check date limits, correct known problems, or preserve a valid date.
A practical implementation looks like this:
LocalDate readDate(Cell cell) {
CellValue value = cell.value();
if (value instanceof CellValue.Number) {
return cell.dateValue();
}
if (value instanceof CellValue.Text textValue) {
return parseDateText(textValue.value().trim());
}
if (value instanceof CellValue.Blank) {
return null;
}
throw new IllegalStateException("Not a supported date cell: " + value);
}
CellValue.Number covers Excel's normal numeric date handling. CellValue.Text requires text-to-date conversion. CellValue.Blank provides predictable null handling for empty cells. The exception protects the process from silently accepting an unsupported value.
The text parser should accept only the formats your business actually uses. For example, it may convert 2026/01/01, 2026-01-01, and 2026 01 01 into the same LocalDate. It should reject ambiguous or malformed values rather than guessing.
How to apply it
Identify the date columns before iterating through the worksheet. This is safer than trying to detect dates in every cell.
Obtain the cells from the used range and iterate through the expected rows.
for (int row = firstRow; row <= lastRow; row++) { Cell cell = sheet.cell(row, dateColumn); LocalDate date = readDate(cell); LocalDate normalized = fixDate(date); if (normalized != null) { cell.setDate(normalized); } }Inspect the cell value before converting it.
cell.value()tells you whether the cell is numeric, text, blank, or another type.Convert numeric values with
cell.dateValue(). This preserves Axyra's Excel date handling for cells that already contain usable date values.Convert text values with
parseDateText. Trim spaces first, then parse the supported separators and date patterns.Return
nullforCellValue.Blank. UpdatefixDateso it handles an unknown date before applying validation rules.LocalDate fixDate(LocalDate date) { if (date == null) { return null; } if (date.isBefore(minAllowedDate)) { return minAllowedDate; } if (date.isAfter(maxAllowedDate)) { return maxAllowedDate; } return date; }Write only valid normalized dates back to the workbook with
cell.setDate(). Do not write a null value unless Axyra's workbook rules specifically require it.Record rejected text values and unexpected cell types. A row number, column name, original value, and reason make spreadsheet data cleaning easier to audit.
Test the process with at least four cases: a numeric Excel date, a recognized text date, a blank cell, and an invalid text value.
After normalization, sort and filter the table again. Consistent date values prevent text dates from being sorted alphabetically ahead of real Excel dates.
One alternative is to convert every date column to text first and parse all values through one route. That can work, but checking CellValue is more robust because it preserves valid numeric dates and distinguishes blanks from malformed text.
Implementation angle
This pattern is a good boundary for an automation workflow. Axyra can normalize date columns before the workbook is synchronized with Zoho Creator, Make, or n8n, while rejected values can be sent to an exception list for review. A language model such as Claude can help generate and test date parsing rules, but the allowed formats and date boundaries should remain explicit in code.
Book an AI implementation engagement with Creator Scripts at creatorscripts.com.
Frequently asked questions
Why does cell.dateValue() return null for some Excel dates?
Excel dates may be stored as numbers or as text. In Axyra Sheets, cell.dateValue() converts numeric date values, but text dates need to be parsed separately.
How do I handle blank cells in an Excel date column?
Check whether the cell is blank before validating its date. Return null for an empty cell, then make sure your validation method handles that value without calling date methods on it.
How can I convert text dates in Excel with Java?
Check the cell value type first, then trim and parse text using the date formats your business accepts. Reject malformed or ambiguous dates instead of guessing, and keep a record of values that could not be parsed.