本地上传添加时间
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
package com.ruoyi.cms.util.excel;
|
||||
import com.alibaba.excel.converters.Converter;
|
||||
import com.alibaba.excel.enums.CellDataTypeEnum;
|
||||
import com.alibaba.excel.metadata.GlobalConfiguration;
|
||||
import com.alibaba.excel.metadata.data.ReadCellData;
|
||||
import com.alibaba.excel.metadata.data.WriteCellData;
|
||||
import com.alibaba.excel.metadata.property.ExcelContentProperty;
|
||||
import java.time.LocalDate;
|
||||
import java.time.ZoneId;
|
||||
import java.util.Date;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class PostingDateConverter implements Converter<Date> {
|
||||
private static final Pattern MONTH_PATTERN = Pattern.compile("^(\\d{1,2})月$");
|
||||
|
||||
@Override
|
||||
public Class<Date> supportJavaTypeKey() {
|
||||
return Date.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CellDataTypeEnum supportExcelTypeKey() {
|
||||
return CellDataTypeEnum.STRING;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Date convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
|
||||
String value = cellData.getStringValue();
|
||||
if (isBlank(value)) {
|
||||
return null;
|
||||
}
|
||||
String trimStr = value.trim();
|
||||
Matcher matcher = MONTH_PATTERN.matcher(trimStr);
|
||||
if (!matcher.matches()) {
|
||||
return null;
|
||||
}
|
||||
int month = Integer.parseInt(matcher.group(1));
|
||||
if (month < 1 || month > 12) {
|
||||
return null;
|
||||
}
|
||||
int currentYear = LocalDate.now().getYear();
|
||||
LocalDate localDate = LocalDate.of(currentYear, month, 1);
|
||||
return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
|
||||
}
|
||||
|
||||
@Override
|
||||
public WriteCellData<?> convertToExcelData(Date value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
|
||||
if (value == null) {
|
||||
return new WriteCellData<>("");
|
||||
}
|
||||
LocalDate localDate = value.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
|
||||
return new WriteCellData<>(localDate.getMonthValue() + "月");
|
||||
}
|
||||
|
||||
public static boolean isBlank(String str) {
|
||||
if (str == null) {
|
||||
return true;
|
||||
}
|
||||
for (int i = 0; i < str.length(); i++) {
|
||||
if (!Character.isWhitespace(str.charAt(i))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user