You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@fesod.apache.org by "leehaut (via GitHub)" <gi...@apache.org> on 2026/08/23 04:01:47 UTC

[PR] feat: add java.time.LocalTime converters [fesod]

leehaut opened a new pull request, #1032:
URL: https://github.com/apache/fesod/pull/1032

   ## Purpose of the pull request
   
   Related: #1017
   
   Add first-class converter support for `java.time.LocalTime`, following the existing `LocalDate` / `LocalDateTime` pattern.
   
   ## What's changed?
   
   Excel has no native time-only cell type. This PR treats `LocalTime` as the time-of-day counterpart of `LocalDate`:
   
   - Writing preserves the wall-clock time, attaches `DateUtils.EPOCH` (`1970-01-01`) as the date component, and applies `HH:mm:ss` so Excel shows time only.
   - Reading drops any date component with `toLocalTime()`. An Excel-native fraction (`0.5` = `12:00:00`) and a full datetime serial that happens to be `12:00:00` both become `LocalTime.of(12, 0, 0)`.
   
   Converter family under `org.apache.fesod.sheet.converters.localtime`:
   
   - `LocalTimeDateConverter` — default write path (`DATE` cell, format `HH:mm:ss`)
   - `LocalTimeNumberConverter` — bidirectional Excel numeric serial, including `use1904windowing`
   - `LocalTimeStringConverter` — bidirectional `STRING` cell; default `HH:mm:ss`, also auto-detects `HH:mm`, respects `@DateTimeFormat` and `Locale`
   
   `DateUtils` additions (mirroring `parseLocalDate` / `format(LocalDate)`):
   
   - `DEFAULT_LOCAL_TIME_FORMAT` / `TIME_FORMAT_8` / `TIME_FORMAT_5`
   - `parseLocalTime(...)` / `format(LocalTime, ...)` / `switchTimeFormat(...)` / `getLocalTime(...)`
   
   Registration in `DefaultConverterLoader` matches `LocalDateTime`:
   
   - Number and String converters in `initAllConverter()`
   - Date converter as the default write converter
   - String converter for string-formatted writes
   
   Scope is limited to `java.time.LocalTime` (JDK 8). Converter lookup is keyed by the declared Java class, so existing `Date` converters do not cover `LocalTime` fields.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@fesod.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fesod.apache.org
For additional commands, e-mail: commits-help@fesod.apache.org


Re: [PR] feat: add java.time.LocalTime converters [fesod]

Posted by "delei (via GitHub)" <gi...@apache.org>.
delei merged PR #1032:
URL: https://github.com/apache/fesod/pull/1032


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@fesod.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fesod.apache.org
For additional commands, e-mail: commits-help@fesod.apache.org


Re: [PR] feat: add java.time.LocalTime converters [fesod]

Posted by "bengbengbalabalabeng (via GitHub)" <gi...@apache.org>.
bengbengbalabalabeng commented on code in PR #1032:
URL: https://github.com/apache/fesod/pull/1032#discussion_r3841983151


##########
fesod-sheet/src/test/java/org/apache/fesod/sheet/util/DateUtilsTest.java:
##########
@@ -19,12 +19,15 @@
 
 package org.apache.fesod.sheet.util;
 
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;

Review Comment:
   Avoid using wildcard imports.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@fesod.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fesod.apache.org
For additional commands, e-mail: commits-help@fesod.apache.org


Re: [PR] feat: add java.time.LocalTime converters [fesod]

Posted by "bengbengbalabalabeng (via GitHub)" <gi...@apache.org>.
bengbengbalabalabeng commented on PR #1032:
URL: https://github.com/apache/fesod/pull/1032#issuecomment-5384431687

   The newly added files in this PR are implemented from scratch and are not derived from Alibaba's EasyExcel. Therefore, no EasyExcel-related license header is required for these files.
   
   Please refer to: https://github.com/apache/fesod/blob/main/fesod-sheet/src/main/java/org/apache/fesod/sheet/FesodSheet.java


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@fesod.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fesod.apache.org
For additional commands, e-mail: commits-help@fesod.apache.org


Re: [PR] feat: add java.time.LocalTime converters [fesod]

Posted by "bengbengbalabalabeng (via GitHub)" <gi...@apache.org>.
bengbengbalabalabeng commented on code in PR #1032:
URL: https://github.com/apache/fesod/pull/1032#discussion_r3841935132


##########
fesod-sheet/src/test/java/org/apache/fesod/sheet/converters/localtime/LocalTimeStringConverterTest.java:
##########
@@ -0,0 +1,170 @@
+/*
+ * 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.converters.localtime;
+
+import java.time.LocalTime;
+import java.time.format.DateTimeParseException;
+import java.util.Locale;
+import java.util.stream.Stream;
+import org.apache.fesod.sheet.enums.CellDataTypeEnum;
+import org.apache.fesod.sheet.metadata.GlobalConfiguration;
+import org.apache.fesod.sheet.metadata.data.ReadCellData;
+import org.apache.fesod.sheet.metadata.data.WriteCellData;
+import org.apache.fesod.sheet.metadata.property.DateTimeFormatProperty;
+import org.apache.fesod.sheet.metadata.property.ExcelContentProperty;
+import org.apache.fesod.sheet.testkit.Tags;
+import org.apache.fesod.sheet.util.DateUtils;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
+import org.junit.jupiter.params.provider.MethodSource;
+
+/**
+ * Tests {@link LocalTimeStringConverter}.
+ */
+@Tag(Tags.UNIT)
+class LocalTimeStringConverterTest {
+
+    private static final GlobalConfiguration GLOBAL_CONFIGURATION = new GlobalConfiguration();
+    private final LocalTimeStringConverter converter = new LocalTimeStringConverter();
+
+    @AfterEach
+    void tearDown() {
+        DateUtils.removeThreadLocalCache();
+    }
+
+    @Test
+    void supportKeys() {
+        Assertions.assertEquals(LocalTime.class, converter.supportJavaTypeKey());
+        Assertions.assertEquals(CellDataTypeEnum.STRING, converter.supportExcelTypeKey());
+    }
+
+    @ParameterizedTest
+    @CsvSource({"00:00:00", "01:01:01", "12:00:00", "23:59:59"})
+    void convertRoundTripWithDefaultFormat(String value) {
+        LocalTime time = LocalTime.parse(value);
+
+        LocalTime actual = converter.convertToJavaData(new ReadCellData<>(value), null, GLOBAL_CONFIGURATION);
+        WriteCellData<?> written = converter.convertToExcelData(time, null, GLOBAL_CONFIGURATION);
+
+        Assertions.assertEquals(time, actual);
+        Assertions.assertEquals(CellDataTypeEnum.STRING, written.getType());
+        Assertions.assertEquals(value, written.getStringValue());
+    }
+
+    @Test
+    void convertToJavaDataAutoDetectsHourMinute() {
+        LocalTime actual = converter.convertToJavaData(new ReadCellData<>("12:30"), null, GLOBAL_CONFIGURATION);
+
+        Assertions.assertEquals(LocalTime.of(12, 30), actual);
+    }
+
+    @Test
+    void convertUsesCustomFormatFromContentProperty() {
+        ExcelContentProperty contentProperty = contentProperty(DateUtils.TIME_FORMAT_5, Boolean.FALSE);
+
+        LocalTime actual =
+                converter.convertToJavaData(new ReadCellData<>("12:30"), contentProperty, GLOBAL_CONFIGURATION);
+        WriteCellData<?> written = converter.convertToExcelData(LocalTime.NOON, contentProperty, GLOBAL_CONFIGURATION);
+
+        Assertions.assertEquals(LocalTime.of(12, 30), actual);
+        Assertions.assertEquals("12:00", written.getStringValue());
+    }
+
+    @Test
+    void convertFallsBackToDefaultWhenContentPropertyHasNoDateTimeFormat() {
+        ExcelContentProperty contentProperty = new ExcelContentProperty();
+
+        LocalTime actual =
+                converter.convertToJavaData(new ReadCellData<>("01:01:01"), contentProperty, GLOBAL_CONFIGURATION);
+        WriteCellData<?> written =
+                converter.convertToExcelData(LocalTime.of(1, 1, 1), contentProperty, GLOBAL_CONFIGURATION);
+
+        Assertions.assertEquals(LocalTime.of(1, 1, 1), actual);
+        Assertions.assertEquals("01:01:01", written.getStringValue());
+    }
+
+    @Test
+    void convertToJavaDataUsesSwitchTimeFormatWhenConfiguredFormatIsEmpty() {
+        ExcelContentProperty contentProperty = contentProperty("", Boolean.FALSE);
+
+        LocalTime withSeconds =
+                converter.convertToJavaData(new ReadCellData<>("12:30:45"), contentProperty, GLOBAL_CONFIGURATION);
+        LocalTime withoutSeconds =
+                converter.convertToJavaData(new ReadCellData<>("12:30"), contentProperty, GLOBAL_CONFIGURATION);
+
+        Assertions.assertEquals(LocalTime.of(12, 30, 45), withSeconds);
+        Assertions.assertEquals(LocalTime.of(12, 30), withoutSeconds);
+    }
+
+    @Test
+    void convertToExcelDataUsesDefaultFormatWhenConfiguredFormatIsEmpty() {
+        ExcelContentProperty contentProperty = contentProperty("", Boolean.FALSE);
+
+        WriteCellData<?> written =
+                converter.convertToExcelData(LocalTime.of(12, 30, 45), contentProperty, GLOBAL_CONFIGURATION);
+
+        Assertions.assertEquals("12:30:45", written.getStringValue());
+    }
+
+    @ParameterizedTest
+    @MethodSource("locales")
+    void convertToExcelDataKeepsNumericTimeAcrossLocales(Locale locale) {

Review Comment:
   The `HH:mm:ss` pattern contains no locale-sensitive symbols, so its output is consistent across different Locales and cannot reflect conversion differences. If you want to test locale behavior, you can introduce the locale-sensitive field `a` (the AM/PM marker) and switch to `hh:mm:ss a`:
   
   - Locale.US → 12:30:45 PM
   - Locale.CHINA → 12:30:45 下午



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@fesod.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fesod.apache.org
For additional commands, e-mail: commits-help@fesod.apache.org


Re: [PR] feat: add java.time.LocalTime converters [fesod]

Posted by "leehaut (via GitHub)" <gi...@apache.org>.
leehaut commented on PR #1032:
URL: https://github.com/apache/fesod/pull/1032#issuecomment-5384648979

   > The newly added files in this PR are implemented from scratch and are not derived from Alibaba's EasyExcel. Therefore, no EasyExcel-related license header is required for these files.
   > 
   > Please refer to: https://github.com/apache/fesod/blob/main/fesod-sheet/src/main/java/org/apache/fesod/sheet/FesodSheet.java
   
   Thanks for the catch. Removed the EasyExcel-derived license header from the newly added LocalTime converter files; they now keep the Apache 2.0 header only, consistent with `FesodSheet.java`.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@fesod.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fesod.apache.org
For additional commands, e-mail: commits-help@fesod.apache.org


Re: [PR] feat: add java.time.LocalTime converters [fesod]

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #1032:
URL: https://github.com/apache/fesod/pull/1032#discussion_r3838267762


##########
fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/localtime/LocalTimeDateConverter.java:
##########
@@ -0,0 +1,53 @@
+/*
+ * 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.converters.localtime;
+
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import org.apache.fesod.sheet.converters.Converter;
+import org.apache.fesod.sheet.metadata.GlobalConfiguration;
+import org.apache.fesod.sheet.metadata.data.WriteCellData;
+import org.apache.fesod.sheet.metadata.property.ExcelContentProperty;
+import org.apache.fesod.sheet.util.DateUtils;
+import org.apache.fesod.sheet.util.WorkBookUtil;
+
+/**
+ * LocalTime and date converter
+ */
+public class LocalTimeDateConverter implements Converter<LocalTime> {
+    @Override
+    public Class<?> supportJavaTypeKey() {
+        return LocalTime.class;
+    }
+
+    @Override
+    public WriteCellData<?> convertToExcelData(
+            LocalTime value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration)
+            throws Exception {
+        LocalDateTime localDateTime = value == null ? null : value.atDate(DateUtils.EPOCH);
+        WriteCellData<?> cellData = new WriteCellData<>(localDateTime);
+        String format = null;
+        if (contentProperty != null && contentProperty.getDateTimeFormatProperty() != null) {
+            format = contentProperty.getDateTimeFormatProperty().getFormat();
+        }
+        WorkBookUtil.fillDataFormat(cellData, format, DateUtils.DEFAULT_LOCAL_TIME_FORMAT);

Review Comment:
   When a field uses `@DateTimeFormat` without a `value` (for example, only to set `use1904windowing`), `DateTimeFormat.value()` is the empty string. Passing that through `fillDataFormat` does not select the LocalTime default; `StyleUtil` treats the blank format as `General`, so Excel displays the underlying serial instead of `HH:mm:ss`. Normalize an empty format to `null` before applying the default.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@fesod.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fesod.apache.org
For additional commands, e-mail: commits-help@fesod.apache.org