Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@
import org.apache.fesod.sheet.converters.string.StringNumberConverter;
import org.apache.fesod.sheet.converters.string.StringStringConverter;
import org.apache.fesod.sheet.converters.url.UrlImageConverter;
import org.apache.fesod.sheet.converters.zoneddatetime.ZonedDateTimeDateConverter;
import org.apache.fesod.sheet.converters.zoneddatetime.ZonedDateTimeNumberConverter;
import org.apache.fesod.sheet.converters.zoneddatetime.ZonedDateTimeStringConverter;

/**
* Load default handler
Expand Down Expand Up @@ -122,6 +125,8 @@ private static void initAllConverter() {

putAllConverter(new LocalTimeNumberConverter());
putAllConverter(new LocalTimeStringConverter());
putAllConverter(new ZonedDateTimeNumberConverter());
putAllConverter(new ZonedDateTimeStringConverter());

putAllConverter(new DoubleBooleanConverter());
putAllConverter(new DoubleNumberConverter());
Expand Down Expand Up @@ -160,6 +165,7 @@ private static void initDefaultWriteConverter() {
putWriteConverter(new LocalDateTimeDateConverter());
putWriteConverter(new LocalDateDateConverter());
putWriteConverter(new LocalTimeDateConverter());
putWriteConverter(new ZonedDateTimeDateConverter());
putWriteConverter(new DoubleNumberConverter());
putWriteConverter(new FloatNumberConverter());
putWriteConverter(new IntegerNumberConverter());
Expand All @@ -181,6 +187,7 @@ private static void initDefaultWriteConverter() {
putWriteStringConverter(new LocalDateStringConverter());
putWriteStringConverter(new LocalDateTimeStringConverter());
putWriteStringConverter(new LocalTimeStringConverter());
putWriteStringConverter(new ZonedDateTimeStringConverter());
putWriteStringConverter(new DoubleStringConverter());
putWriteStringConverter(new FloatStringConverter());
putWriteStringConverter(new IntegerStringConverter());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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.zoneddatetime;

import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import org.apache.fesod.common.util.StringUtils;
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;

/** ZonedDateTime and date converter. */
public class ZonedDateTimeDateConverter implements Converter<ZonedDateTime> {
@Override
public Class<?> supportJavaTypeKey() {
return ZonedDateTime.class;
}

@Override
public WriteCellData<?> convertToExcelData(
ZonedDateTime value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration)
throws Exception {
LocalDateTime localDateTime = value.toLocalDateTime();
WriteCellData<?> cellData = new WriteCellData<>(localDateTime);
String format = null;
if (contentProperty != null && contentProperty.getDateTimeFormatProperty() != null) {
format = contentProperty.getDateTimeFormatProperty().getFormat();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Empty @DateTimeFormat values are passed through to WorkBookUtil.fillDataFormat as-is. fillDataFormat only substitutes the default format when the argument is null — an empty string ("") is written into the cell style as-is, so @DateTimeFormat(value = "") produces a DATE cell with no format at all instead of falling back to yyyy-MM-dd HH:mm:ss.

Suggest normalizing the empty format to null before the call:

String format = null;
if (contentProperty != null && contentProperty.getDateTimeFormatProperty() != null) {
    format = contentProperty.getDateTimeFormatProperty().getFormat();
    if (StringUtils.isEmpty(format)) {
        format = null;
    }
}
WorkBookUtil.fillDataFormat(cellData, format, DateUtils.defaultDateFormat);

This also keeps the DATE direction consistent with the string converter below, which already falls back when the format is empty.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Fixed in f882ea6: empty DATE formats are normalized to null before fillDataFormat, so the existing yyyy-MM-dd HH:mm:ss default is applied. Added a regression test.

if (StringUtils.isEmpty(format)) {
format = null;
}
}
WorkBookUtil.fillDataFormat(cellData, format, DateUtils.defaultDateFormat);
return cellData;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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.zoneddatetime;

import java.math.BigDecimal;
import java.time.ZoneId;
import java.time.ZonedDateTime;
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;
import org.apache.poi.ss.usermodel.DateUtil;

/** ZonedDateTime and number converter. */
public class ZonedDateTimeNumberConverter implements Converter<ZonedDateTime> {
@Override
public Class<?> supportJavaTypeKey() {
return ZonedDateTime.class;
}

@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.NUMBER;
}

@Override
public ZonedDateTime convertToJavaData(
ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
return DateUtils.getLocalDateTime(
cellData.getNumberValue().doubleValue(),
DateUtils.isDate1904(contentProperty, globalConfiguration))
.atZone(ZoneId.systemDefault());
}

@Override
public WriteCellData<?> convertToExcelData(
ZonedDateTime value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
return new WriteCellData<>(BigDecimal.valueOf(DateUtil.getExcelDate(
value.toLocalDateTime(), DateUtils.isDate1904(contentProperty, globalConfiguration))));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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.zoneddatetime;

import java.time.ZonedDateTime;
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;

/** ZonedDateTime and string converter. */
public class ZonedDateTimeStringConverter implements Converter<ZonedDateTime> {
@Override
public Class<?> supportJavaTypeKey() {
return ZonedDateTime.class;
}

@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.STRING;
}

@Override
public ZonedDateTime convertToJavaData(
ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
return DateUtils.parseZonedDateTime(
cellData.getStringValue(), dateTimeFormat(contentProperty), globalConfiguration.getLocale());
}

@Override
public WriteCellData<?> convertToExcelData(
ZonedDateTime value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
return new WriteCellData<>(
DateUtils.format(value, dateTimeFormat(contentProperty), globalConfiguration.getLocale()));
}

private String dateTimeFormat(ExcelContentProperty contentProperty) {
if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
return null;
}
return contentProperty.getDateTimeFormatProperty().getFormat();
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Empty @DateTimeFormat values are handled inconsistently across the three converters:

  • ZonedDateTimeStringConverter (line 81) falls back to ISO_ZONED_DATE_TIME2020-01-02T03:04:05Z
  • ZonedDateTimeDateConverter (line 54) passes "" through to fillDataFormat → cell ends up with no format at all
  • ZonedDateTimeNumberConverter → the cell is formatted with the workbook default

So the same @DateTimeFormat(value = "") produces three different outputs depending on the cell type. Suggest picking one behavior — falling back to the default yyyy-MM-dd HH:mm:ss on all three keeps it consistent with the other date-time families.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

The DATE defect is fixed in f882ea6. I left the three representations defaults intentionally distinct: STRING uses ISO_ZONED_DATE_TIME to preserve zone and offset text, while DATE and NUMBER use Excel date semantics and the workbook default. This matches the existing converter-family conventions, so a blanket yyyy-MM-dd HH:mm:ss fallback would change intended STRING behavior.

Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
Expand Down Expand Up @@ -164,6 +167,25 @@ public static LocalTime parseLocalTime(String timeString, String timeFormat, Loc
return LocalTime.parse(timeString, getCacheDateTimeFormat(timeFormat, local));
}

/**
* Convert string to a zoned date-time.
*
* @param dateTimeString
* @param dateTimeFormat
* @param local
* @return
*/
public static ZonedDateTime parseZonedDateTime(String dateTimeString, String dateTimeFormat, Locale local) {
DateTimeFormatter formatter = StringUtils.isEmpty(dateTimeFormat)
? DateTimeFormatter.ISO_ZONED_DATE_TIME
: getCacheDateTimeFormat(dateTimeFormat, local);
try {
return ZonedDateTime.parse(dateTimeString, formatter);
} catch (DateTimeParseException e) {
return LocalDateTime.parse(dateTimeString, formatter).atZone(ZoneId.systemDefault());
}
}

/**
* convert string to date
*
Expand Down Expand Up @@ -328,6 +350,24 @@ public static String format(LocalTime time, String timeFormat, Locale local) {
return time.format(getCacheDateTimeFormat(timeFormat, local));
}

/**
* Format a zoned date-time.
*
* @param dateTime
* @param dateTimeFormat
* @param local
* @return format string
*/
public static String format(ZonedDateTime dateTime, String dateTimeFormat, Locale local) {
if (dateTime == null) {
return null;
}
DateTimeFormatter formatter = StringUtils.isEmpty(dateTimeFormat)
? DateTimeFormatter.ISO_ZONED_DATE_TIME
: getCacheDateTimeFormat(dateTimeFormat, local);
return dateTime.format(formatter);
}

/**
* Format date
*
Expand Down Expand Up @@ -375,6 +415,13 @@ public static boolean isDate1904(ExcelContentProperty contentProperty, GlobalCon
return globalUse1904windowing != null && globalUse1904windowing;
}

/**
* Get a cached date-time formatter for the supplied pattern and locale.
*
* @param dateFormat date-time pattern
* @param locale locale used to resolve the pattern
* @return cached formatter
*/
private static DateTimeFormatter getCacheDateTimeFormat(String dateFormat, Locale locale) {
Locale actualLocale = locale == null ? Locale.getDefault(Locale.Category.FORMAT) : locale;
Map<Locale, Map<String, DateTimeFormatter>> localeCache = DATE_TIME_FORMATTER_THREAD_LOCAL.get();
Expand Down
Loading