easyExcel 读取日期为数字的解决方案
生活随笔
收集整理的這篇文章主要介紹了
easyExcel 读取日期为数字的解决方案
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
在導(dǎo)入的模板文件中設(shè)置了日期格式 yyyy/mm/dd,java 接受實(shí)體類對(duì)應(yīng)字段為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在解析時(shí) 該字段類型為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, 導(dǎo)致日期數(shù)差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")));} }總結(jié)
以上是生活随笔為你收集整理的easyExcel 读取日期为数字的解决方案的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 网络游戏知识产权保护白皮书
- 下一篇: 2020上半年中国教育行业投融资报告