From b939134d5c553a9fe69599a98a80a780b3ed3d7d Mon Sep 17 00:00:00 2001 From: maguawing <294432080@qq.com> Date: Tue, 10 Nov 2026 21:52:53 +0800 Subject: [PATCH 1/2] feat: add MonthDay converter family with tests --- .../converters/DefaultConverterLoader.java | 4 + .../monthday/MonthDayStringConverter.java | 81 ++++++++ .../apache/fesod/sheet/util/DateUtils.java | 36 ++++ .../converter/MonthDayConverterDataTest.java | 117 +++++++++++ .../sheet/converter/MonthDayMixedData.java | 42 ++++ .../converter/MonthDayWithFormatData.java | 40 ++++ .../sheet/converter/MonthDayWriteData.java | 38 ++++ .../monthday/MonthDayStringConverterTest.java | 192 ++++++++++++++++++ .../docs/sheet/help/built-in-converters.md | 7 + .../current/sheet/help/built-in-converters.md | 7 + 10 files changed, 564 insertions(+) create mode 100644 fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/monthday/MonthDayStringConverter.java create mode 100644 fesod-sheet/src/test/java/org/apache/fesod/sheet/converter/MonthDayConverterDataTest.java create mode 100644 fesod-sheet/src/test/java/org/apache/fesod/sheet/converter/MonthDayMixedData.java create mode 100644 fesod-sheet/src/test/java/org/apache/fesod/sheet/converter/MonthDayWithFormatData.java create mode 100644 fesod-sheet/src/test/java/org/apache/fesod/sheet/converter/MonthDayWriteData.java create mode 100644 fesod-sheet/src/test/java/org/apache/fesod/sheet/converters/monthday/MonthDayStringConverterTest.java diff --git a/fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/DefaultConverterLoader.java b/fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/DefaultConverterLoader.java index 95a247442f..dd58ced0a3 100644 --- a/fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/DefaultConverterLoader.java +++ b/fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/DefaultConverterLoader.java @@ -72,6 +72,7 @@ import org.apache.fesod.sheet.converters.longconverter.LongBooleanConverter; import org.apache.fesod.sheet.converters.longconverter.LongNumberConverter; import org.apache.fesod.sheet.converters.longconverter.LongStringConverter; +import org.apache.fesod.sheet.converters.monthday.MonthDayStringConverter; import org.apache.fesod.sheet.converters.offsetdatetime.OffsetDateTimeDateConverter; import org.apache.fesod.sheet.converters.offsetdatetime.OffsetDateTimeNumberConverter; import org.apache.fesod.sheet.converters.offsetdatetime.OffsetDateTimeStringConverter; @@ -137,6 +138,7 @@ private static void initAllConverter() { putAllConverter(new LocalTimeNumberConverter()); putAllConverter(new LocalTimeStringConverter()); + putAllConverter(new MonthDayStringConverter()); putAllConverter(new SqlTimeNumberConverter()); putAllConverter(new SqlTimeStringConverter()); putAllConverter(new TimestampNumberConverter()); @@ -189,6 +191,7 @@ private static void initDefaultWriteConverter() { putWriteConverter(new LocalDateDateConverter()); putWriteConverter(new LocalTimeDateConverter()); putWriteConverter(new InstantStringConverter()); + putWriteConverter(new MonthDayStringConverter()); putWriteConverter(new SqlTimeDateConverter()); putWriteConverter(new TimestampDateConverter()); putWriteConverter(new OffsetDateTimeDateConverter()); @@ -217,6 +220,7 @@ private static void initDefaultWriteConverter() { putWriteStringConverter(new LocalDateTimeStringConverter()); putWriteStringConverter(new LocalTimeStringConverter()); putWriteStringConverter(new InstantStringConverter()); + putWriteStringConverter(new MonthDayStringConverter()); putWriteStringConverter(new SqlTimeStringConverter()); putWriteStringConverter(new TimestampStringConverter()); putWriteStringConverter(new OffsetDateTimeStringConverter()); diff --git a/fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/monthday/MonthDayStringConverter.java b/fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/monthday/MonthDayStringConverter.java new file mode 100644 index 0000000000..dc4b512095 --- /dev/null +++ b/fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/monthday/MonthDayStringConverter.java @@ -0,0 +1,81 @@ +/* + * 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.monthday; + +import java.time.MonthDay; +import java.time.format.DateTimeParseException; +import org.apache.fesod.common.util.StringUtils; +import org.apache.fesod.sheet.converters.Converter; +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.ExcelContentProperty; +import org.apache.fesod.sheet.util.DateUtils; + +/** + * MonthDay and string converter + * + * + */ +public class MonthDayStringConverter implements Converter { + + private static final String LENIENT_FORMAT = "MM-dd"; + + @Override + public Class supportJavaTypeKey() { + return MonthDay.class; + } + + @Override + public CellDataTypeEnum supportExcelTypeKey() { + return CellDataTypeEnum.STRING; + } + + @Override + public MonthDay convertToJavaData( + ReadCellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { + String stringValue = cellData.getStringValue(); + String format = customFormat(contentProperty); + if (format != null) { + return DateUtils.parseMonthDay(stringValue, format, globalConfiguration.getLocale()); + } + try { + return DateUtils.parseMonthDay(stringValue, null, globalConfiguration.getLocale()); + } catch (DateTimeParseException e) { + return DateUtils.parseMonthDay(stringValue, LENIENT_FORMAT, globalConfiguration.getLocale()); + } + } + + @Override + public WriteCellData convertToExcelData( + MonthDay value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { + return new WriteCellData<>( + DateUtils.format(value, customFormat(contentProperty), globalConfiguration.getLocale())); + } + + private String customFormat(ExcelContentProperty contentProperty) { + if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) { + return null; + } + String format = contentProperty.getDateTimeFormatProperty().getFormat(); + return StringUtils.isEmpty(format) ? null : format; + } +} diff --git a/fesod-sheet/src/main/java/org/apache/fesod/sheet/util/DateUtils.java b/fesod-sheet/src/main/java/org/apache/fesod/sheet/util/DateUtils.java index 7ea687fbcb..a7683eb963 100644 --- a/fesod-sheet/src/main/java/org/apache/fesod/sheet/util/DateUtils.java +++ b/fesod-sheet/src/main/java/org/apache/fesod/sheet/util/DateUtils.java @@ -32,6 +32,7 @@ import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; +import java.time.MonthDay; import java.time.OffsetDateTime; import java.time.Year; import java.time.format.DateTimeFormatter; @@ -97,6 +98,8 @@ public class DateUtils { public static String defaultYearFormat = "yyyy"; + public static String defaultMonthDayFormat = "--MM-dd"; + public static final String DEFAULT_LOCAL_TIME_FORMAT = TIME_FORMAT_8; public static final int SECONDS_PER_MINUTE = 60; @@ -170,6 +173,21 @@ public static LocalTime parseLocalTime(String timeString, String timeFormat, Loc return LocalTime.parse(timeString, getCacheDateTimeFormat(timeFormat, local)); } + /** + * convert string to month day + * + * @param monthDayString month day string, e.g. --10-31 + * @param monthDayFormat month day format, empty means ISO representation of a month-day + * @param local local + * @return + */ + public static MonthDay parseMonthDay(String monthDayString, String monthDayFormat, Locale local) { + if (StringUtils.isEmpty(monthDayFormat)) { + monthDayFormat = defaultMonthDayFormat; + } + return MonthDay.parse(monthDayString, getCacheDateTimeFormat(monthDayFormat, local)); + } + /** * convert string to date, requiring the string to carry an explicit offset * @@ -353,6 +371,24 @@ public static String format(LocalTime time, String timeFormat, Locale local) { return time.format(getCacheDateTimeFormat(timeFormat, local)); } + /** + * Format month day + * + * @param monthDay MonthDay + * @param monthDayFormat month day format, empty means ISO representation of a month-day + * @param local local + * @return format string + */ + public static String format(MonthDay monthDay, String monthDayFormat, Locale local) { + if (monthDay == null) { + return null; + } + if (StringUtils.isEmpty(monthDayFormat)) { + monthDayFormat = defaultMonthDayFormat; + } + return monthDay.format(getCacheDateTimeFormat(monthDayFormat, local)); + } + /** * convert string to year * diff --git a/fesod-sheet/src/test/java/org/apache/fesod/sheet/converter/MonthDayConverterDataTest.java b/fesod-sheet/src/test/java/org/apache/fesod/sheet/converter/MonthDayConverterDataTest.java new file mode 100644 index 0000000000..6c00df8dcc --- /dev/null +++ b/fesod-sheet/src/test/java/org/apache/fesod/sheet/converter/MonthDayConverterDataTest.java @@ -0,0 +1,117 @@ +/* + * 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.converter; + +import java.time.LocalDate; +import java.time.MonthDay; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import org.apache.fesod.sheet.testkit.Tags; +import org.apache.fesod.sheet.testkit.base.AbstractExcelTest; +import org.apache.fesod.sheet.testkit.enums.ExcelFormat; +import org.apache.fesod.sheet.testkit.helpers.RoundTripHelper; +import org.apache.fesod.sheet.testkit.params.ExcelFormatSource; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.params.ParameterizedTest; + +/** + * Round-trip tests for {@link org.apache.fesod.sheet.converters.monthday.MonthDayStringConverter} over all Excel + * formats, covering the default ISO format, a user-configured {@code DateTimeFormat}, null values, boundary values and + * coexistence with other date converters. + */ +@Tag(Tags.ROUND_TRIP) +public class MonthDayConverterDataTest extends AbstractExcelTest { + + @ParameterizedTest + @ExcelFormatSource + void readAndWriteMonthDay(ExcelFormat format) throws Exception { + MonthDayWriteData data = new MonthDayWriteData(); + data.setBirthday(MonthDay.of(10, 31)); + + List result = RoundTripHelper.writeAndRead( + createTempFile(format), MonthDayWriteData.class, Collections.singletonList(data)); + + Assertions.assertEquals(1, result.size()); + Assertions.assertEquals(MonthDay.of(10, 31), result.get(0).getBirthday()); + } + + @ParameterizedTest + @ExcelFormatSource + void readAndWriteMonthDayWithDateTimeFormat(ExcelFormat format) throws Exception { + MonthDayWithFormatData data = new MonthDayWithFormatData(); + data.setBirthday(MonthDay.of(2, 29)); + + List result = RoundTripHelper.writeAndRead( + createTempFile(format), MonthDayWithFormatData.class, Collections.singletonList(data)); + + Assertions.assertEquals(1, result.size()); + Assertions.assertEquals(MonthDay.of(2, 29), result.get(0).getBirthday()); + } + + @ParameterizedTest + @ExcelFormatSource + void readAndWriteNullMonthDay(ExcelFormat format) throws Exception { + MonthDayMixedData data = new MonthDayMixedData(); + data.setDate(LocalDate.of(2024, 3, 15)); + + List result = RoundTripHelper.writeAndRead( + createTempFile(format), MonthDayMixedData.class, Collections.singletonList(data)); + + Assertions.assertEquals(1, result.size()); + Assertions.assertNull(result.get(0).getBirthday()); + Assertions.assertEquals(LocalDate.of(2024, 3, 15), result.get(0).getDate()); + } + + @ParameterizedTest + @ExcelFormatSource + void readAndWriteBoundaryMonthDays(ExcelFormat format) throws Exception { + List data = new ArrayList<>(); + for (String value : new String[] {"--01-01", "--02-29", "--12-31"}) { + MonthDayWriteData row = new MonthDayWriteData(); + row.setBirthday(MonthDay.parse(value)); + data.add(row); + } + + List result = + RoundTripHelper.writeAndRead(createTempFile(format), MonthDayWriteData.class, data); + + Assertions.assertEquals(3, result.size()); + Assertions.assertEquals(MonthDay.of(1, 1), result.get(0).getBirthday()); + Assertions.assertEquals(MonthDay.of(2, 29), result.get(1).getBirthday()); + Assertions.assertEquals(MonthDay.of(12, 31), result.get(2).getBirthday()); + } + + @ParameterizedTest + @ExcelFormatSource + void readAndWriteMonthDayAlongsideLocalDate(ExcelFormat format) throws Exception { + MonthDayMixedData data = new MonthDayMixedData(); + data.setBirthday(MonthDay.of(3, 15)); + data.setDate(LocalDate.of(2024, 3, 15)); + + List result = RoundTripHelper.writeAndRead( + createTempFile(format), MonthDayMixedData.class, Collections.singletonList(data)); + + Assertions.assertEquals(1, result.size()); + Assertions.assertEquals(MonthDay.of(3, 15), result.get(0).getBirthday()); + Assertions.assertEquals(LocalDate.of(2024, 3, 15), result.get(0).getDate()); + } +} diff --git a/fesod-sheet/src/test/java/org/apache/fesod/sheet/converter/MonthDayMixedData.java b/fesod-sheet/src/test/java/org/apache/fesod/sheet/converter/MonthDayMixedData.java new file mode 100644 index 0000000000..863f916979 --- /dev/null +++ b/fesod-sheet/src/test/java/org/apache/fesod/sheet/converter/MonthDayMixedData.java @@ -0,0 +1,42 @@ +/* + * 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.converter; + +import java.time.LocalDate; +import java.time.MonthDay; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; +import org.apache.fesod.sheet.annotation.ExcelProperty; + +/** + * Data model for testing that {@link MonthDay} conversion coexists with other date converters in the same row. + */ +@Getter +@Setter +@EqualsAndHashCode +public class MonthDayMixedData { + + @ExcelProperty("Birthday") + private MonthDay birthday; + + @ExcelProperty("Date") + private LocalDate date; +} diff --git a/fesod-sheet/src/test/java/org/apache/fesod/sheet/converter/MonthDayWithFormatData.java b/fesod-sheet/src/test/java/org/apache/fesod/sheet/converter/MonthDayWithFormatData.java new file mode 100644 index 0000000000..623ee7921a --- /dev/null +++ b/fesod-sheet/src/test/java/org/apache/fesod/sheet/converter/MonthDayWithFormatData.java @@ -0,0 +1,40 @@ +/* + * 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.converter; + +import java.time.MonthDay; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; +import org.apache.fesod.sheet.annotation.ExcelProperty; +import org.apache.fesod.sheet.annotation.format.DateTimeFormat; + +/** + * Data model for testing {@link MonthDay} conversion with a user-configured {@link DateTimeFormat}. + */ +@Getter +@Setter +@EqualsAndHashCode +public class MonthDayWithFormatData { + + @ExcelProperty("Birthday") + @DateTimeFormat("MM/dd") + private MonthDay birthday; +} diff --git a/fesod-sheet/src/test/java/org/apache/fesod/sheet/converter/MonthDayWriteData.java b/fesod-sheet/src/test/java/org/apache/fesod/sheet/converter/MonthDayWriteData.java new file mode 100644 index 0000000000..04c953d402 --- /dev/null +++ b/fesod-sheet/src/test/java/org/apache/fesod/sheet/converter/MonthDayWriteData.java @@ -0,0 +1,38 @@ +/* + * 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.converter; + +import java.time.MonthDay; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; +import org.apache.fesod.sheet.annotation.ExcelProperty; + +/** + * Data model for testing {@link MonthDay} conversion during Excel write and read operations. + */ +@Getter +@Setter +@EqualsAndHashCode +public class MonthDayWriteData { + + @ExcelProperty("Birthday") + private MonthDay birthday; +} diff --git a/fesod-sheet/src/test/java/org/apache/fesod/sheet/converters/monthday/MonthDayStringConverterTest.java b/fesod-sheet/src/test/java/org/apache/fesod/sheet/converters/monthday/MonthDayStringConverterTest.java new file mode 100644 index 0000000000..36f3322bf2 --- /dev/null +++ b/fesod-sheet/src/test/java/org/apache/fesod/sheet/converters/monthday/MonthDayStringConverterTest.java @@ -0,0 +1,192 @@ +/* + * 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.monthday; + +import java.time.MonthDay; +import java.time.format.DateTimeParseException; +import java.util.Locale; +import org.apache.fesod.sheet.converters.ConverterKeyBuild; +import org.apache.fesod.sheet.converters.DefaultConverterLoader; +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.ValueSource; + +/** + * Tests {@link MonthDayStringConverter}. + */ +@Tag(Tags.UNIT) +class MonthDayStringConverterTest { + + private static final GlobalConfiguration GLOBAL_CONFIGURATION = new GlobalConfiguration(); + private final MonthDayStringConverter converter = new MonthDayStringConverter(); + + @AfterEach + void tearDown() { + DateUtils.removeThreadLocalCache(); + } + + @ParameterizedTest + @ValueSource(strings = {"--10-31", "10-31"}) + void convertToJavaDataAcceptsValidValues(String value) { + MonthDay parsed = converter.convertToJavaData(new ReadCellData<>(value), null, GLOBAL_CONFIGURATION); + + Assertions.assertEquals(MonthDay.of(10, 31), parsed); + } + + @ParameterizedTest + @ValueSource(strings = {"2024-10-31", "13-01", "--02-30", "abc"}) + void convertToJavaDataRejectsInvalidValues(String value) { + Assertions.assertThrows( + DateTimeParseException.class, + () -> converter.convertToJavaData(new ReadCellData<>(value), null, GLOBAL_CONFIGURATION)); + } + + @Test + void convertToJavaDataSupportsLeapDay() { + MonthDay parsed = converter.convertToJavaData(new ReadCellData<>("--02-29"), null, GLOBAL_CONFIGURATION); + + Assertions.assertEquals(MonthDay.of(2, 29), parsed); + } + + @Test + void convertToExcelDataWritesIsoMonthDay() { + WriteCellData cellData = converter.convertToExcelData(MonthDay.of(10, 31), null, GLOBAL_CONFIGURATION); + + Assertions.assertEquals("--10-31", cellData.getStringValue()); + } + + @Test + void convertToExcelDataUsesConfiguredPattern() { + WriteCellData cellData = + converter.convertToExcelData(MonthDay.of(10, 31), propertyWithFormat("MM/dd"), GLOBAL_CONFIGURATION); + + Assertions.assertEquals("10/31", cellData.getStringValue()); + } + + @Test + void convertToJavaDataReadsConfiguredPattern() { + MonthDay parsed = converter.convertToJavaData( + new ReadCellData<>("10/31"), propertyWithFormat("MM/dd"), GLOBAL_CONFIGURATION); + + Assertions.assertEquals(MonthDay.of(10, 31), parsed); + } + + @Test + void convertToJavaDataRejectsTextNotMatchingConfiguredPattern() { + ExcelContentProperty property = propertyWithFormat("MM/dd"); + + Assertions.assertThrows( + DateTimeParseException.class, + () -> converter.convertToJavaData(new ReadCellData<>("--10-31"), property, GLOBAL_CONFIGURATION)); + } + + @Test + void blankOrNullPatternFallsBackToIso() { + for (String blankFormat : new String[] {"", null}) { + ExcelContentProperty property = propertyWithFormat(blankFormat); + + Assertions.assertEquals( + "--10-31", + converter + .convertToExcelData(MonthDay.of(10, 31), property, GLOBAL_CONFIGURATION) + .getStringValue()); + Assertions.assertEquals( + MonthDay.of(10, 31), + converter.convertToJavaData(new ReadCellData<>("--10-31"), property, GLOBAL_CONFIGURATION)); + } + } + + @Test + void configuredPatternHonoursGlobalLocale() { + ExcelContentProperty property = propertyWithFormat("MMMM d"); + GlobalConfiguration usConfiguration = new GlobalConfiguration(); + usConfiguration.setLocale(Locale.US); + GlobalConfiguration frenchConfiguration = new GlobalConfiguration(); + frenchConfiguration.setLocale(Locale.FRENCH); + + Assertions.assertEquals( + "March 15", + converter + .convertToExcelData(MonthDay.of(3, 15), property, usConfiguration) + .getStringValue()); + Assertions.assertEquals( + "mars 15", + converter + .convertToExcelData(MonthDay.of(3, 15), property, frenchConfiguration) + .getStringValue()); + Assertions.assertEquals( + MonthDay.of(3, 15), + converter.convertToJavaData(new ReadCellData<>("March 15"), property, usConfiguration)); + Assertions.assertEquals( + MonthDay.of(3, 15), + converter.convertToJavaData(new ReadCellData<>("mars 15"), property, frenchConfiguration)); + } + + @ParameterizedTest + @ValueSource(strings = {"--01-01", "--02-29", "--12-31"}) + void writeAndReadRoundTripsBoundaryValues(String isoText) { + WriteCellData written = converter.convertToExcelData(MonthDay.parse(isoText), null, GLOBAL_CONFIGURATION); + + Assertions.assertEquals( + MonthDay.parse(isoText), + converter.convertToJavaData(new ReadCellData<>(written.getStringValue()), null, GLOBAL_CONFIGURATION)); + } + + @Test + void converterIsRegisteredForBothReadAndWrite() { + Assertions.assertEquals( + MonthDayStringConverter.class, + DefaultConverterLoader.loadAllConverter() + .get(ConverterKeyBuild.buildKey(MonthDay.class, CellDataTypeEnum.STRING)) + .getClass()); + Assertions.assertEquals( + MonthDayStringConverter.class, + DefaultConverterLoader.loadDefaultWriteConverter() + .get(ConverterKeyBuild.buildKey(MonthDay.class)) + .getClass()); + Assertions.assertEquals( + MonthDayStringConverter.class, + DefaultConverterLoader.loadDefaultWriteConverter() + .get(ConverterKeyBuild.buildKey(MonthDay.class, CellDataTypeEnum.STRING)) + .getClass()); + Assertions.assertEquals( + 1, + DefaultConverterLoader.loadAllConverter().entrySet().stream() + .filter(entry -> entry.getKey().getClazz() == MonthDay.class) + .count()); + } + + private static ExcelContentProperty propertyWithFormat(String format) { + ExcelContentProperty property = new ExcelContentProperty(); + property.setDateTimeFormatProperty(new DateTimeFormatProperty(format, false)); + return property; + } +} diff --git a/website/docs/sheet/help/built-in-converters.md b/website/docs/sheet/help/built-in-converters.md index 8cef3b7a44..404ed11930 100644 --- a/website/docs/sheet/help/built-in-converters.md +++ b/website/docs/sheet/help/built-in-converters.md @@ -383,6 +383,13 @@ This section lists the converters provided in the project. 🟡 NEXT +MonthDay +STRING +MonthDayStringConverter +✅ Read
✅ Write +🟡 NEXT + + byte[] IMAGE (Drawing) ByteArrayImageConverter diff --git a/website/i18n/zh-cn/docusaurus-plugin-content-docs/current/sheet/help/built-in-converters.md b/website/i18n/zh-cn/docusaurus-plugin-content-docs/current/sheet/help/built-in-converters.md index 9b20864f44..39358398ab 100644 --- a/website/i18n/zh-cn/docusaurus-plugin-content-docs/current/sheet/help/built-in-converters.md +++ b/website/i18n/zh-cn/docusaurus-plugin-content-docs/current/sheet/help/built-in-converters.md @@ -383,6 +383,13 @@ title: '内置转换器' 🟡 待发布 +MonthDay +STRING +MonthDayStringConverter +✅ 读取
✅ 写入 +🟡 待发布 + + byte[] IMAGE (Drawing) ByteArrayImageConverter From 0bee6bc1f578b0e602f299568b46b530fcf7b2b0 Mon Sep 17 00:00:00 2001 From: maguawing <294432080@qq.com> Date: Mon, 16 Nov 2026 09:14:55 +0800 Subject: [PATCH 2/2] fix: address review feedback on MonthDay converter --- .../monthday/MonthDayStringConverter.java | 21 +++------------ .../apache/fesod/sheet/util/DateUtils.java | 6 ++++- .../DefaultConverterLoaderTest.java | 17 ++++++++++++ .../monthday/MonthDayStringConverterTest.java | 27 ------------------- 4 files changed, 25 insertions(+), 46 deletions(-) diff --git a/fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/monthday/MonthDayStringConverter.java b/fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/monthday/MonthDayStringConverter.java index dc4b512095..c985ede36b 100644 --- a/fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/monthday/MonthDayStringConverter.java +++ b/fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/monthday/MonthDayStringConverter.java @@ -20,8 +20,6 @@ package org.apache.fesod.sheet.converters.monthday; import java.time.MonthDay; -import java.time.format.DateTimeParseException; -import org.apache.fesod.common.util.StringUtils; import org.apache.fesod.sheet.converters.Converter; import org.apache.fesod.sheet.enums.CellDataTypeEnum; import org.apache.fesod.sheet.metadata.GlobalConfiguration; @@ -32,13 +30,9 @@ /** * MonthDay and string converter - * - * */ public class MonthDayStringConverter implements Converter { - private static final String LENIENT_FORMAT = "MM-dd"; - @Override public Class supportJavaTypeKey() { return MonthDay.class; @@ -52,16 +46,8 @@ public CellDataTypeEnum supportExcelTypeKey() { @Override public MonthDay convertToJavaData( ReadCellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { - String stringValue = cellData.getStringValue(); - String format = customFormat(contentProperty); - if (format != null) { - return DateUtils.parseMonthDay(stringValue, format, globalConfiguration.getLocale()); - } - try { - return DateUtils.parseMonthDay(stringValue, null, globalConfiguration.getLocale()); - } catch (DateTimeParseException e) { - return DateUtils.parseMonthDay(stringValue, LENIENT_FORMAT, globalConfiguration.getLocale()); - } + return DateUtils.parseMonthDay( + cellData.getStringValue(), customFormat(contentProperty), globalConfiguration.getLocale()); } @Override @@ -75,7 +61,6 @@ private String customFormat(ExcelContentProperty contentProperty) { if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) { return null; } - String format = contentProperty.getDateTimeFormatProperty().getFormat(); - return StringUtils.isEmpty(format) ? null : format; + return contentProperty.getDateTimeFormatProperty().getFormat(); } } diff --git a/fesod-sheet/src/main/java/org/apache/fesod/sheet/util/DateUtils.java b/fesod-sheet/src/main/java/org/apache/fesod/sheet/util/DateUtils.java index e713c6795a..f03931174d 100644 --- a/fesod-sheet/src/main/java/org/apache/fesod/sheet/util/DateUtils.java +++ b/fesod-sheet/src/main/java/org/apache/fesod/sheet/util/DateUtils.java @@ -92,7 +92,11 @@ public class DateUtils { public static String defaultYearFormat = "yyyy"; - public static String defaultMonthDayFormat = "--MM-dd"; + /** + * The optional {@code --} prefix reads both the ISO form ({@code --10-31}) and the bare form ({@code 10-31}), + * so no lenient fallback pattern is needed when parsing. + */ + public static String defaultMonthDayFormat = "[--]MM-dd"; public static final String DEFAULT_LOCAL_TIME_FORMAT = TIME_FORMAT_8; diff --git a/fesod-sheet/src/test/java/org/apache/fesod/sheet/converters/DefaultConverterLoaderTest.java b/fesod-sheet/src/test/java/org/apache/fesod/sheet/converters/DefaultConverterLoaderTest.java index bd5f19d417..1f78999c73 100644 --- a/fesod-sheet/src/test/java/org/apache/fesod/sheet/converters/DefaultConverterLoaderTest.java +++ b/fesod-sheet/src/test/java/org/apache/fesod/sheet/converters/DefaultConverterLoaderTest.java @@ -23,6 +23,7 @@ import java.sql.Timestamp; import java.time.Instant; import java.time.LocalTime; +import java.time.MonthDay; import java.util.Map; import java.util.UUID; import org.apache.fesod.sheet.converters.ConverterKeyBuild.ConverterKey; @@ -30,6 +31,7 @@ import org.apache.fesod.sheet.converters.localtime.LocalTimeDateConverter; import org.apache.fesod.sheet.converters.localtime.LocalTimeNumberConverter; import org.apache.fesod.sheet.converters.localtime.LocalTimeStringConverter; +import org.apache.fesod.sheet.converters.monthday.MonthDayStringConverter; import org.apache.fesod.sheet.converters.sqltime.SqlTimeDateConverter; import org.apache.fesod.sheet.converters.sqltime.SqlTimeNumberConverter; import org.apache.fesod.sheet.converters.sqltime.SqlTimeStringConverter; @@ -79,6 +81,21 @@ void loadConvertersRegistersLocalTimeFamily() { writeConverter.get(ConverterKeyBuild.buildKey(LocalTime.class, CellDataTypeEnum.STRING))); } + @Test + void loadConvertersRegistersMonthDayStringConverter() { + Map> allConverter = DefaultConverterLoader.loadAllConverter(); + Assertions.assertInstanceOf( + MonthDayStringConverter.class, + allConverter.get(ConverterKeyBuild.buildKey(MonthDay.class, CellDataTypeEnum.STRING))); + + Map> writeConverter = DefaultConverterLoader.loadDefaultWriteConverter(); + Assertions.assertInstanceOf( + MonthDayStringConverter.class, writeConverter.get(ConverterKeyBuild.buildKey(MonthDay.class))); + Assertions.assertInstanceOf( + MonthDayStringConverter.class, + writeConverter.get(ConverterKeyBuild.buildKey(MonthDay.class, CellDataTypeEnum.STRING))); + } + @Test void loadConvertersRegistersInstantStringConverter() { Map> allConverter = DefaultConverterLoader.loadAllConverter(); diff --git a/fesod-sheet/src/test/java/org/apache/fesod/sheet/converters/monthday/MonthDayStringConverterTest.java b/fesod-sheet/src/test/java/org/apache/fesod/sheet/converters/monthday/MonthDayStringConverterTest.java index 36f3322bf2..8500592ac0 100644 --- a/fesod-sheet/src/test/java/org/apache/fesod/sheet/converters/monthday/MonthDayStringConverterTest.java +++ b/fesod-sheet/src/test/java/org/apache/fesod/sheet/converters/monthday/MonthDayStringConverterTest.java @@ -22,9 +22,6 @@ import java.time.MonthDay; import java.time.format.DateTimeParseException; import java.util.Locale; -import org.apache.fesod.sheet.converters.ConverterKeyBuild; -import org.apache.fesod.sheet.converters.DefaultConverterLoader; -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; @@ -160,30 +157,6 @@ void writeAndReadRoundTripsBoundaryValues(String isoText) { converter.convertToJavaData(new ReadCellData<>(written.getStringValue()), null, GLOBAL_CONFIGURATION)); } - @Test - void converterIsRegisteredForBothReadAndWrite() { - Assertions.assertEquals( - MonthDayStringConverter.class, - DefaultConverterLoader.loadAllConverter() - .get(ConverterKeyBuild.buildKey(MonthDay.class, CellDataTypeEnum.STRING)) - .getClass()); - Assertions.assertEquals( - MonthDayStringConverter.class, - DefaultConverterLoader.loadDefaultWriteConverter() - .get(ConverterKeyBuild.buildKey(MonthDay.class)) - .getClass()); - Assertions.assertEquals( - MonthDayStringConverter.class, - DefaultConverterLoader.loadDefaultWriteConverter() - .get(ConverterKeyBuild.buildKey(MonthDay.class, CellDataTypeEnum.STRING)) - .getClass()); - Assertions.assertEquals( - 1, - DefaultConverterLoader.loadAllConverter().entrySet().stream() - .filter(entry -> entry.getKey().getClazz() == MonthDay.class) - .count()); - } - private static ExcelContentProperty propertyWithFormat(String format) { ExcelContentProperty property = new ExcelContentProperty(); property.setDateTimeFormatProperty(new DateTimeFormatProperty(format, false));