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 @@ -49,11 +49,7 @@ public WriteCellData<?> convertToExcelData(
Date value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration)
throws Exception {
WriteCellData<?> cellData = new WriteCellData<>(value);
String format = null;
if (contentProperty != null && contentProperty.getDateTimeFormatProperty() != null) {
format = contentProperty.getDateTimeFormatProperty().getFormat();
}
WorkBookUtil.fillDataFormat(cellData, format, DateUtils.defaultDateFormat);
WorkBookUtil.fillDataFormat(cellData, DateUtils.resolveDateTimeFormat(contentProperty, globalConfiguration));
return cellData;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,7 @@ public Date convertToJavaData(
@Override
public WriteCellData<?> convertToExcelData(
Date value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
return new WriteCellData<>(DateUtils.format(value, null));
} else {
return new WriteCellData<>(DateUtils.format(
value, contentProperty.getDateTimeFormatProperty().getFormat()));
}
return new WriteCellData<>(
DateUtils.format(value, DateUtils.resolveDateTimeFormat(contentProperty, globalConfiguration)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,7 @@ public WriteCellData<?> convertToExcelData(
throws Exception {
LocalDateTime localDateTime = value == null ? null : value.atTime(0, 0);
WriteCellData<?> cellData = new WriteCellData<>(localDateTime);
String format = null;
if (contentProperty != null && contentProperty.getDateTimeFormatProperty() != null) {
format = contentProperty.getDateTimeFormatProperty().getFormat();
}
WorkBookUtil.fillDataFormat(cellData, format, DateUtils.defaultLocalDateFormat);
WorkBookUtil.fillDataFormat(cellData, DateUtils.resolveLocalDateFormat(contentProperty, globalConfiguration));
return cellData;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,9 @@ public LocalDate convertToJavaData(
@Override
public WriteCellData<?> convertToExcelData(
LocalDate value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
return new WriteCellData<>(DateUtils.format(value, null, globalConfiguration.getLocale()));
} else {
return new WriteCellData<>(DateUtils.format(
value, contentProperty.getDateTimeFormatProperty().getFormat(), globalConfiguration.getLocale()));
}
return new WriteCellData<>(DateUtils.format(
value,
DateUtils.resolveLocalDateFormat(contentProperty, globalConfiguration),
globalConfiguration.getLocale()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,7 @@ public WriteCellData<?> convertToExcelData(
LocalDateTime value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration)
throws Exception {
WriteCellData<?> cellData = new WriteCellData<>(value);
String format = null;
if (contentProperty != null && contentProperty.getDateTimeFormatProperty() != null) {
format = contentProperty.getDateTimeFormatProperty().getFormat();
}
WorkBookUtil.fillDataFormat(cellData, format, DateUtils.defaultDateFormat);
WorkBookUtil.fillDataFormat(cellData, DateUtils.resolveDateTimeFormat(contentProperty, globalConfiguration));
return cellData;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,9 @@ public LocalDateTime convertToJavaData(
@Override
public WriteCellData<?> convertToExcelData(
LocalDateTime value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
return new WriteCellData<>(DateUtils.format(value, null, globalConfiguration.getLocale()));
} else {
return new WriteCellData<>(DateUtils.format(
value, contentProperty.getDateTimeFormatProperty().getFormat(), globalConfiguration.getLocale()));
}
return new WriteCellData<>(DateUtils.format(
value,
DateUtils.resolveDateTimeFormat(contentProperty, globalConfiguration),
globalConfiguration.getLocale()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import java.time.LocalDateTime;
import java.time.OffsetDateTime;
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;
Expand All @@ -42,14 +41,7 @@ public WriteCellData<?> convertToExcelData(
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);
WorkBookUtil.fillDataFormat(cellData, DateUtils.resolveDateTimeFormat(contentProperty, globalConfiguration));
return cellData;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public String convertToJavaData(
return DateUtils.format(
cellData.getNumberValue(),
DateUtils.isDate1904(contentProperty, globalConfiguration),
contentProperty.getDateTimeFormatProperty().getFormat());
DateUtils.resolveDateTimeFormat(contentProperty, globalConfiguration));
}
// If there are "NumberFormat", read as number
if (contentProperty != null && contentProperty.getNumberFormatProperty() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,17 @@ public CellDataTypeEnum supportExcelTypeKey() {
public Year convertToJavaData(
ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
return DateUtils.parseYear(
cellData.getStringValue(), getYearFormat(contentProperty), globalConfiguration.getLocale());
cellData.getStringValue(),
DateUtils.resolveYearFormat(contentProperty, globalConfiguration),
globalConfiguration.getLocale());
}

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

private static String getYearFormat(ExcelContentProperty contentProperty) {
if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
return null;
}
return contentProperty.getDateTimeFormatProperty().getFormat();
return new WriteCellData<>(DateUtils.format(
value,
DateUtils.resolveYearFormat(contentProperty, globalConfiguration),
globalConfiguration.getLocale()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.apache.fesod.common.util.StringUtils;
import org.apache.fesod.sheet.converters.Converter;
import org.apache.fesod.sheet.converters.ConverterKeyBuild;
import org.apache.fesod.sheet.util.DateUtils;

/**
* Write/read holder
Expand Down Expand Up @@ -125,6 +127,37 @@ public AbstractHolder(BasicParameter basicParameter, AbstractHolder prentAbstrac
} else {
globalConfiguration.setFiledCacheLocation(basicParameter.getFiledCacheLocation());
}

// Date patterns resolve eagerly: a workbook snapshots the process-wide default once so every cell in it uses
// one consistent value, while sheet/table holders inherit the workbook's snapshot. A blank value is treated as
// "not set" here rather than stored as-is: the resolvers read an empty pattern as a fallback signal, so
// keeping it in the snapshot would let a later global change leak into an in-flight workbook.
if (StringUtils.isBlank(basicParameter.getDefaultDateFormat())) {
globalConfiguration.setDefaultDateFormat(
prentAbstractHolder != null
? prentAbstractHolder.getGlobalConfiguration().getDefaultDateFormat()
: DateUtils.getDefaultDateFormat());
} else {
globalConfiguration.setDefaultDateFormat(basicParameter.getDefaultDateFormat());
}

if (StringUtils.isBlank(basicParameter.getDefaultLocalDateFormat())) {
globalConfiguration.setDefaultLocalDateFormat(
prentAbstractHolder != null
? prentAbstractHolder.getGlobalConfiguration().getDefaultLocalDateFormat()
: DateUtils.getDefaultLocalDateFormat());
} else {
globalConfiguration.setDefaultLocalDateFormat(basicParameter.getDefaultLocalDateFormat());
}

if (StringUtils.isBlank(basicParameter.getDefaultYearFormat())) {
globalConfiguration.setDefaultYearFormat(
prentAbstractHolder != null
? prentAbstractHolder.getGlobalConfiguration().getDefaultYearFormat()
: DateUtils.getDefaultYearFormat());
} else {
globalConfiguration.setDefaultYearFormat(basicParameter.getDefaultYearFormat());
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.Objects;
import java.util.function.Consumer;
import org.apache.fesod.common.util.ListUtils;
import org.apache.fesod.common.util.StringUtils;
import org.apache.fesod.sheet.converters.Converter;
import org.apache.fesod.sheet.enums.CacheLocationEnum;

Expand Down Expand Up @@ -184,6 +185,72 @@ public T autoStrip(Boolean autoStrip) {
return self();
}

/**
* Default pattern for {@code Date}, {@code LocalDateTime} and {@code OffsetDateTime} fields without a
* {@code @DateTimeFormat} pattern.
* <p>
* default is the process-wide default ({@link org.apache.fesod.sheet.util.DateUtils#getDefaultDateFormat()}),
* {@code yyyy-MM-dd HH:mm:ss} unless changed.
* <p>
* Passing {@code null} leaves the pattern unset, so the read/write context inherits the process-wide default. An
* empty or whitespace-only pattern is rejected rather than silently treated as "not set".
*
* @param defaultDateFormat the pattern, or {@code null} to inherit the process-wide default
* @return
* @throws IllegalArgumentException if the pattern is empty or whitespace-only
*/
public T defaultDateFormat(String defaultDateFormat) {
parameter().setDefaultDateFormat(requireNonBlankPattern(defaultDateFormat, "defaultDateFormat"));
return self();
}

/**
* Default pattern for {@code LocalDate} fields without a {@code @DateTimeFormat} pattern.
* <p>
* default is the process-wide default ({@link org.apache.fesod.sheet.util.DateUtils#getDefaultLocalDateFormat()}),
* {@code yyyy-MM-dd} unless changed.
* <p>
* Passing {@code null} leaves the pattern unset, so the read/write context inherits the process-wide default. An
* empty or whitespace-only pattern is rejected rather than silently treated as "not set".
*
* @param defaultLocalDateFormat the pattern, or {@code null} to inherit the process-wide default
* @return
* @throws IllegalArgumentException if the pattern is empty or whitespace-only
*/
public T defaultLocalDateFormat(String defaultLocalDateFormat) {
parameter().setDefaultLocalDateFormat(requireNonBlankPattern(defaultLocalDateFormat, "defaultLocalDateFormat"));
return self();
}

/**
* Default pattern for {@code Year} fields without a {@code @DateTimeFormat} pattern.
* <p>
* default is the process-wide default ({@link org.apache.fesod.sheet.util.DateUtils#getDefaultYearFormat()}),
* {@code yyyy} unless changed.
* <p>
* Passing {@code null} leaves the pattern unset, so the read/write context inherits the process-wide default. An
* empty or whitespace-only pattern is rejected rather than silently treated as "not set".
*
* @param defaultYearFormat the pattern, or {@code null} to inherit the process-wide default
* @return
* @throws IllegalArgumentException if the pattern is empty or whitespace-only
*/
public T defaultYearFormat(String defaultYearFormat) {
parameter().setDefaultYearFormat(requireNonBlankPattern(defaultYearFormat, "defaultYearFormat"));
return self();
}

/**
* Rejects an empty or whitespace-only pattern while keeping {@code null} as "not set", which is how the sibling
* builder parameters behave: the holder then falls back to the parent or process-wide default.
*/
private static String requireNonBlankPattern(String format, String name) {
if (format != null && StringUtils.isBlank(format)) {
throw new IllegalArgumentException(name + " can not be blank");
}
return format;
}

@SuppressWarnings("unchecked")
protected T self() {
return (T) this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,21 @@ public class BasicParameter {
* Automatic strip includes sheet name and content
*/
private Boolean autoStrip;
/**
* Default pattern for {@code Date}, {@code LocalDateTime} and {@code OffsetDateTime} fields without a
* {@code @DateTimeFormat} pattern; {@code null} falls back to the process-wide default
* ({@link org.apache.fesod.sheet.util.DateUtils#setDefaultDateFormat(String)}).
*/
private String defaultDateFormat;
/**
* Default pattern for {@code LocalDate} fields without a {@code @DateTimeFormat} pattern; {@code null} falls back
* to the process-wide default
* ({@link org.apache.fesod.sheet.util.DateUtils#setDefaultLocalDateFormat(String)}).
*/
private String defaultLocalDateFormat;
/**
* Default pattern for {@code Year} fields without a {@code @DateTimeFormat} pattern; {@code null} falls back to
* the process-wide default ({@link org.apache.fesod.sheet.util.DateUtils#setDefaultYearFormat(String)}).
*/
private String defaultYearFormat;
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,28 @@ public class GlobalConfiguration {
*/
private Boolean autoStrip;

/**
* Per-call default pattern for {@code Date}, {@code LocalDateTime} and {@code OffsetDateTime} fields without a
* {@code @DateTimeFormat} pattern; {@code null} falls back to the process-wide default
* ({@link org.apache.fesod.sheet.util.DateUtils#getDefaultDateFormat()}, snapshotted when the context is created).
*/
private String defaultDateFormat;

/**
* Per-call default pattern for {@code LocalDate} fields without a {@code @DateTimeFormat} pattern; {@code null}
* falls back to the process-wide default
* ({@link org.apache.fesod.sheet.util.DateUtils#getDefaultLocalDateFormat()}, snapshotted when the context is
* created).
*/
private String defaultLocalDateFormat;

/**
* Per-call default pattern for {@code Year} fields without a {@code @DateTimeFormat} pattern; {@code null} falls
* back to the process-wide default
* ({@link org.apache.fesod.sheet.util.DateUtils#getDefaultYearFormat()}, snapshotted when the context is created).
*/
private String defaultYearFormat;

public GlobalConfiguration() {
this.autoTrim = Boolean.TRUE;
this.autoStrip = Boolean.FALSE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,10 @@ private String buildCellValue(CsvCell csvCell) {
}
// date
if (dataFormat == null) {
dataFormatString = DateUtils.defaultDateFormat;
String defaultDateFormat = csvWorkbook.getDefaultDateFormat();
dataFormatString = !StringUtils.isEmpty(defaultDateFormat)
? defaultDateFormat
: DateUtils.getDefaultDateFormat();
dataFormat = csvWorkbook.createDataFormat().getFormat(dataFormatString);
}
if (dataFormatString == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ public class CsvWorkbook implements Workbook {
*/
private Locale locale;

/**
* Per-call default pattern for date values without an explicit cell style format; {@code null} falls back to the
* process-wide default ({@link org.apache.fesod.sheet.util.DateUtils#getDefaultDateFormat()}).
*/
private String defaultDateFormat;

/**
* Whether to use scientific Format.
* <p>
Expand Down
Loading
Loading