当前位置:
首页 >
easyExcel 读取日期为数字的解决方案
发布时间:2025/3/15
79
豆豆
生活随笔
收集整理的这篇文章主要介绍了
easyExcel 读取日期为数字的解决方案
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
在导入的模板文件中设置了日期格式 yyyy/mm/dd,java 接受实体类对应字段为Date
/*** 使用日期*/@ExcelProperty(value = "使用日期",converter = DateConverter.class)@DateTimeFormat("yyyy/MM/dd")@JsonFormat(pattern = "yyyy/MM/dd",timezone = "GMT+8",shape = JsonFormat.Shape.STRING)@JsonSerialize(using = DateSerializer.class)private Date usingTime;easyExcel在解析时 该字段类型为Number. 值是从1900年1月1日到这一天的日期,所以添加解析器
public class DateConverter implements Converter<Date> {@Overridepublic Class supportJavaTypeKey() {return Date.class;}@Overridepublic CellDataTypeEnum supportExcelTypeKey() {return CellDataTypeEnum.STRING;}@Overridepublic Date convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) {if (cellData.getType().equals(CellDataTypeEnum.NUMBER)) {LocalDate localDate = LocalDate.of(1900, 1, 1);//excel 有些奇怪的bug, 导致日期数差2localDate = localDate.plusDays(cellData.getNumberValue().longValue() - 2);return Date.valueOf(localDate);} else if (cellData.getType().equals(CellDataTypeEnum.STRING)) {return Date.valueOf(LocalDate.parse(cellData.getStringValue(), DateTimeFormatter.ofPattern("yyyy/MM/dd")));} else {return null;} }@Overridepublic CellData convertToExcelData(Date date, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) {return new CellData<>(date.toLocalDate().format(DateTimeFormatter.ofPattern("yyyy/MM/dd")));} }总结
以上是生活随笔为你收集整理的easyExcel 读取日期为数字的解决方案的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 网络游戏知识产权保护白皮书
- 下一篇: 2020上半年中国教育行业投融资报告