Repository navigation
Description
Search before asking
- I searched in the issues and found nothing similar.
Fesod version
main (69147b7)
JDK version
17
Operating system
Windows 11 (the behavior is OS-independent)
Steps To Reproduce
Write a value with the built-in OffsetDateTimeDateConverter, then read it back with a plain OffsetDateTime field:
public class DateConverterModel {
@ExcelProperty(value = "odt", converter = OffsetDateTimeDateConverter.class)
private OffsetDateTime value;
public OffsetDateTime getValue() { return value; }
public void setValue(OffsetDateTime value) { this.value = value; }
}
public class PlainModel {
@ExcelProperty("odt")
private OffsetDateTime value;
public OffsetDateTime getValue() { return value; }
public void setValue(OffsetDateTime value) { this.value = value; }
}
DateConverterModel model = new DateConverterModel();
model.setValue(OffsetDateTime.of(2024, 1, 2, 3, 4, 5, 0, ZoneOffset.ofHours(-5)));
File xlsx = new File("out.xlsx");
FesodSheet.write(xlsx, DateConverterModel.class).sheet().doWrite(Collections.singletonList(model));
System.out.println(FesodSheet.read(xlsx).head(PlainModel.class).sheet().doReadSync().get(0).getValue());
// on a UTC+8 machine: 2024-01-02T03:04:05+08:00
File csv = new File("out.csv");
FesodSheet.write(csv, DateConverterModel.class).sheet().doWrite(Collections.singletonList(model));
System.out.println(Files.readAllLines(csv.toPath()));
// [odt, 2024-01-02 03:04:05]
System.out.println(FesodSheet.read(csv).head(PlainModel.class).sheet().doReadSync().get(0).getValue());
// ExcelDataConvertException: ... to class java.time.OffsetDateTime error
Current Behavior
- XLSX/XLS: the value reads back as
2024-01-02T03:04:05+08:00, not-05:00. The wall-clock digits are unchanged, but the instant is different, and nothing warns about it. - CSV: the file contains
2024-01-02 03:04:05with no offset. Reading it back intoOffsetDateTimethrowsExcelDataConvertException, because the CSV string has no offset and the string converter defaults toISO_OFFSET_DATE_TIME. - The default
Stringconverter round-trips correctly; the problem is specific to the explicit DATE converter.
Expected Behavior
An Excel date cell cannot carry an offset, so dropping it may be the intended contract. In that case please make the loss explicit, and keep the CSV path consistent with the rest of the library:
- document in the converter javadoc and on the built-in converters page that the DATE converter keeps only the local date-time and discards the offset, and that the String converter should be used when the offset matters; and
- make the CSV output readable back, e.g. accept the local date-time and attach the system zone (as
OffsetDateTimeNumberConverteralready does for XLSX), or write an ISO string that carries the offset.
Anything else?
Root cause: OffsetDateTimeDateConverter.convertToExcelData uses value.toLocalDateTime() and DateUtils.defaultDateFormat (yyyy-MM-dd HH:mm:ss). The XLSX read path attaches ZoneId.systemDefault() in OffsetDateTimeNumberConverter; the CSV read path uses OffsetDateTimeStringConverter, whose default is ISO_OFFSET_DATE_TIME.
ZonedDateTimeDateConverter in #1294 follows the same pattern, so it will have the same limitation. #1243 also touches these files, and I am happy to coordinate or rebase on it.
Are you willing to submit a PR?
- I'm willing to submit a PR!
Activity
fix: read local date-time text in ZonedDateTimeStringConverter (apach…
fix: preserve OffsetDateTime offset in CSV output (apache#1334)
fix: preserve ZonedDateTime zone in CSV output (apache#1334)