package com.qianwen.core.excel.extend.converters;
|
|
import com.alibaba.excel.converters.Converter;
|
import com.alibaba.excel.enums.CellDataTypeEnum;
|
import com.alibaba.excel.metadata.CellData;
|
import com.alibaba.excel.metadata.GlobalConfiguration;
|
import com.alibaba.excel.metadata.property.ExcelContentProperty;
|
import java.text.ParseException;
|
import java.time.LocalDateTime;
|
import java.time.format.DateTimeFormatter;
|
|
/* loaded from: blade-starter-excel-9.3.0.0-SNAPSHOT.jar:org/springblade/core/excel/extend/converters/LocalDateTimeStringConverter.class */
|
public enum LocalDateTimeStringConverter implements Converter<LocalDateTime> {
|
INSTANCE;
|
|
private static final String MINUS = "-";
|
|
public Class supportJavaTypeKey() {
|
return LocalDateTime.class;
|
}
|
|
public CellDataTypeEnum supportExcelTypeKey() {
|
return CellDataTypeEnum.STRING;
|
}
|
|
public LocalDateTime convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws ParseException {
|
String pattern, stringValue = cellData.getStringValue();
|
if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
|
pattern = switchDateFormat(stringValue);
|
} else {
|
pattern = contentProperty.getDateTimeFormatProperty().getFormat();
|
}
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
|
return LocalDateTime.parse(cellData.getStringValue(), formatter);
|
}
|
|
public CellData<String> convertToExcelData(LocalDateTime value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
|
String pattern;
|
if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
|
pattern = "yyyy-MM-dd HH:mm:ss";
|
} else {
|
pattern = contentProperty.getDateTimeFormatProperty().getFormat();
|
}
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
|
return new CellData<>(value.format(formatter));
|
}
|
|
private static String switchDateFormat(String dateString) {
|
int length = dateString.length();
|
switch (length) {
|
case 19:
|
if (dateString.contains("-"))
|
return "yyyy-MM-dd HH:mm:ss";
|
return "yyyy/MM/dd HH:mm:ss";
|
case 17:
|
return "yyyyMMdd HH:mm:ss";
|
case 14:
|
return "yyyyMMddHHmmss";
|
case 10:
|
return "yyyy-MM-dd";
|
}
|
throw new IllegalArgumentException("can not find date format for:" + dateString);
|
}
|
}
|