diff --git a/fesod-sheet/src/main/java/org/apache/fesod/sheet/metadata/data/WriteCellData.java b/fesod-sheet/src/main/java/org/apache/fesod/sheet/metadata/data/WriteCellData.java index cf3511360..14f3831ba 100644 --- a/fesod-sheet/src/main/java/org/apache/fesod/sheet/metadata/data/WriteCellData.java +++ b/fesod-sheet/src/main/java/org/apache/fesod/sheet/metadata/data/WriteCellData.java @@ -26,6 +26,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; @@ -169,7 +170,11 @@ public WriteCellData(Date dateValue) { throw new IllegalArgumentException("DateValue can not be null"); } setType(CellDataTypeEnum.DATE); - this.dateValue = LocalDateTime.ofInstant(dateValue.toInstant(), ZoneId.systemDefault()); + if (dateValue instanceof java.sql.Date || dateValue instanceof java.sql.Time) { + this.dateValue = LocalDateTime.ofInstant(Instant.ofEpochMilli(dateValue.getTime()), ZoneId.systemDefault()); + } else { + this.dateValue = LocalDateTime.ofInstant(dateValue.toInstant(), ZoneId.systemDefault()); + } } /** diff --git a/fesod-sheet/src/test/java/org/apache/fesod/sheet/metadata/data/WriteCellDataTest.java b/fesod-sheet/src/test/java/org/apache/fesod/sheet/metadata/data/WriteCellDataTest.java new file mode 100644 index 000000000..11831805a --- /dev/null +++ b/fesod-sheet/src/test/java/org/apache/fesod/sheet/metadata/data/WriteCellDataTest.java @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.fesod.sheet.metadata.data; + +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import org.apache.fesod.sheet.enums.CellDataTypeEnum; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class WriteCellDataTest { + + @Test + void constructorShouldSupportSqlDate() { + WriteCellData cellData = new WriteCellData<>(java.sql.Date.valueOf("2026-05-07")); + + Assertions.assertEquals(CellDataTypeEnum.DATE, cellData.getType()); + Assertions.assertEquals( + LocalDate.of(2026, 5, 7), cellData.getDateValue().toLocalDate()); + } + + @Test + void constructorShouldSupportSqlTime() { + WriteCellData cellData = new WriteCellData<>(java.sql.Time.valueOf("12:34:56")); + + Assertions.assertEquals(CellDataTypeEnum.DATE, cellData.getType()); + Assertions.assertEquals( + LocalTime.of(12, 34, 56), cellData.getDateValue().toLocalTime()); + } + + @Test + void constructorShouldKeepSupportingSqlTimestampNanos() { + WriteCellData cellData = new WriteCellData<>(java.sql.Timestamp.valueOf("2026-05-07 12:34:56.789123456")); + + Assertions.assertEquals(CellDataTypeEnum.DATE, cellData.getType()); + Assertions.assertEquals(LocalDateTime.of(2026, 5, 7, 12, 34, 56, 789_123_456), cellData.getDateValue()); + } +}