Skip to content
Open
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.apache.fesod.sheet.metadata.data;

import java.math.BigDecimal;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
Expand Down Expand Up @@ -163,7 +164,8 @@ public WriteCellData(Date dateValue) {
throw new IllegalArgumentException("DateValue can not be null");
}
setType(CellDataTypeEnum.DATE);
this.dateValue = LocalDateTime.ofInstant(dateValue.toInstant(), ZoneId.systemDefault());
// Use getTime() which works for both java.util.Date and java.sql.Date
this.dateValue = LocalDateTime.ofInstant(Instant.ofEpochMilli(dateValue.getTime()), ZoneId.systemDefault());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the Date#getTime() incurs a loss of precision, as it only provides millisecond accuracy. Furthermore, might we consider supporting java.sql.Timestamp? I believe the unit test examples could be improved.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the feedback! I'll look into this and explore supporting java.sql.Timestamp as well. Will improve the test examples too.

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,19 @@ public void doAfterAllAnalysed(AnalysisContext context) {
Assertions.assertEquals(1, list.size());
CellDataReadData cellDataData = list.get(0);

Assertions.assertEquals("2020年01月01日", cellDataData.getDate().getData());
Assertions.assertEquals("2020-01-01", cellDataData.getDate().getData());
Assertions.assertEquals("2020-01-01", cellDataData.getSqlDate().getData());

Assertions.assertEquals(2L, (long) cellDataData.getInteger1().getData());
Assertions.assertEquals(2L, (long) cellDataData.getInteger2());

if (context.readWorkbookHolder().getExcelType() != ExcelTypeEnum.CSV) {
Assertions.assertEquals(
"B2+C2", cellDataData.getFormulaValue().getFormulaData().getFormulaValue());
} else {
Assertions.assertNull(cellDataData.getFormulaValue().getData());
}

log.debug("First row:{}", JSON.toJSONString(list.get(0)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,22 @@ private void readAndWrite(File file) throws Exception {
private List<CellDataWriteData> data() throws Exception {
List<CellDataWriteData> list = new ArrayList<>();
CellDataWriteData cellDataData = new CellDataWriteData();

cellDataData.setDate(new WriteCellData<>(DateUtils.parseDate("2020-01-01 01:01:01")));
cellDataData.setSqlDate(new WriteCellData<>(java.sql.Date.valueOf("2020-01-01")));

WriteCellData<Integer> integer1 = new WriteCellData<>();
integer1.setType(CellDataTypeEnum.NUMBER);
integer1.setNumberValue(BigDecimal.valueOf(2L));
cellDataData.setInteger1(integer1);
cellDataData.setInteger2(2);

WriteCellData<?> formulaValue = new WriteCellData<>();
FormulaData formulaData = new FormulaData();
formulaValue.setFormulaData(formulaData);
formulaData.setFormulaValue("B2+C2");
cellDataData.setFormulaValue(formulaValue);

list.add(cellDataData);
return list;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@
@Setter
@EqualsAndHashCode
public class CellDataReadData {
@DateTimeFormat("yyyy年MM月dd日")
@DateTimeFormat("yyyy-MM-dd")
private ReadCellData<String> date;

@DateTimeFormat("yyyy-MM-dd")
private ReadCellData<String> sqlDate;

private ReadCellData<Integer> integer1;
private Integer integer2;
private ReadCellData<?> formulaValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@
@Setter
@EqualsAndHashCode
public class CellDataWriteData {
@DateTimeFormat("yyyy年MM月dd日")
@DateTimeFormat("yyyy-MM-dd")
private WriteCellData<Date> date;

@DateTimeFormat("yyyy-MM-dd")
private WriteCellData<Date> sqlDate;

private WriteCellData<Integer> integer1;
private Integer integer2;
private WriteCellData<?> formulaValue;
Expand Down
Loading