1
触发场景描述
这是execl 表中转换失败的一列数据
起始时间
2017/08/08
2017/08/08
2019-07-05
2019-07-12
2019-07-12
2019-09-29
2019-10-15
2019-10-15
2019-10-22
2019-10-22
2019-11-21
2019-11-21
2019-11-25
这是我需要单独处理的一列 因为格式不一致 所以想用自定义日期转换器 但发现 读前2条数据时OK
第3条起 全部为 null?
现在只能用纯 String 类型的 model 转 纯String 类型 的 model 接数据 就完全正常... 然后再把数据 自行转换再把数据 复制到DB 实体类 保存触发Bug的代码
//实体类中的属性
@ExcelProperty(value = "起始时间",converter = CustomDateConverter.class)
private LocalDate startDate;
// 自定义转换类
public class CustomDateConverter implements Converter<LocalDate> {
@Override
public Class supportJavaTypeKey() {
return LocalDate.class;
}
@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.STRING;
}
/**
* 这里读的时候会调用
*
* @param cellData
* NotNull
* @param contentProperty
* Nullable
* @param globalConfiguration
* NotNull
* @return
*/
@Override
public LocalDate convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
String dataStringValue = cellData.getStringValue();
//default, ISO_LOCAL_DATE [yyyy-mm-dd]
LocalDate localDate;
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
try {
if (dataStringValue.length() == 10) {
localDate = LocalDate.parse(dataStringValue, dateTimeFormatter);
} else {
localDate = LocalDate.parse(dataStringValue);
}
} catch (Exception e) {
localDate = LocalDate.parse(dataStringValue, dateTimeFormatter);
}
return localDate;
}
}
提示的异常或者没有达到的效果
2020-03-04 18:11:46.252 INFO 6132 --- [nio-8181-exec-1] c.s.salary.listener.EListener : 解析到一条数据:{"startDate":{"year":2017,"month":"AUGUST","monthValue":8,"dayOfMonth":8,"dayOfWeek":"TUESDAY","era":"CE","dayOfYear":220,"leapYear":false,"chronology":{"id":"ISO","calendarType":"iso8601"}}
2020-03-04 18:11:46.637 INFO 6132 --- [nio-8181-exec-1] c.s.salary.listener.EListener : 解析到一条数据:{"startDate":{"year":2017,"month":"AUGUST","monthValue":8,"dayOfMonth":8,"dayOfWeek":"TUESDAY","era":"CE","dayOfYear":220,"leapYear":false,"chronology":{"id":"ISO","calendarType":"iso8601"}}}
2020-03-04 18:11:47.042 ERROR 6132 --- [nio-8181-exec-1] c.s.salary.listener.EListener : 解析失败,但是继续解析下一行:Convert data 43651 to class java.time.LocalDate error
2020-03-04 18:11:47.042 ERROR 6132 --- [nio-8181-exec-1] c.s.salary.listener.EListener : 第3行,第3列解析异常