-
Notifications
You must be signed in to change notification settings - Fork 532
feat: add ZonedDateTime converters #1020
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
33ff35a
5872be7
52bda98
fe786e6
73ee9a5
8eff724
070db89
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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(); | ||
| 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(); | ||
| } | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Empty
So the same
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Empty
@DateTimeFormatvalues are passed through toWorkBookUtil.fillDataFormatas-is.fillDataFormatonly substitutes the default format when the argument isnull— 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 toyyyy-MM-dd HH:mm:ss.Suggest normalizing the empty format to
nullbefore the call:This also keeps the DATE direction consistent with the string converter below, which already falls back when the format is empty.
There was a problem hiding this comment.
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.