EasyExcel => FastExcel ,导入支持多种时间格式
InfoExcelDTO
/**
* 合作开始日期*
*/
@ExcelProperty(index = 22,converter = ExcelDateConverter.class)
private Date cooperationDate;
ExcelDateConverter
package com.vipsoft.base.util;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import cn.idev.excel.converters.Converter;
import cn.idev.excel.enums.CellDataTypeEnum;
import cn.idev.excel.metadata.GlobalConfiguration;
import cn.idev.excel.metadata.data.ReadCellData;
import cn.idev.excel.metadata.property.ExcelContentProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 日期格式转换器
*/
public class ExcelDateConverter implements Converter<Date> {
private static final Logger log = LoggerFactory.getLogger(ExcelDateConverter.class);
// 定义所有要尝试的日期格式
SimpleDateFormat[] formats = {
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"),
new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"),
new SimpleDateFormat("yyyy/MM/dd"),
new SimpleDateFormat("yyyy-MM-dd"),
new SimpleDateFormat("yyyy/MM"),
new SimpleDateFormat("yyyy/MM"),
new SimpleDateFormat("yyyyMMdd")
};
@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) throws Exception {
String cellValue = "";
if (cellData.getType().equals(CellDataTypeEnum.NUMBER)) {
long cellIntValue = cellData.getNumberValue().longValue();
if (cellIntValue > 19900100) {
try {
// 1. 第一种解析,传入的是数字形式的日期,形如yyyyMMdd
SimpleDateFormat originalFormat = new SimpleDateFormat("yyyyMMdd");
return originalFormat.parse(String.valueOf(cellIntValue));
} catch (Exception e) {
log.warn("exception when parse numerical time with format yyyyMMdd");
cellValue=String.valueOf(cellIntValue);
}
}
// 2. 第二种解析, excel是从1900年开始计算,最终通过计算与1900年间隔的天数计算目标日期
LocalDate localDate = LocalDate.of(1900, 1, 1);
//excel 有些奇怪的bug, 导致日期数差2
localDate = localDate.plusDays(cellIntValue - 2);
// 转换为ZonedDateTime(如果需要时区信息)
ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
return Date.from(zonedDateTime.toInstant());
} else if (cellData.getType().equals(CellDataTypeEnum.STRING)) {
// 3. 第三种解析
Date date = null;
cellValue = cellData.getStringValue();
for (SimpleDateFormat format : formats) {
try {
date = format.parse(cellValue);
if (date != null) {
// 这一步是将日期格式化为Java期望的格式
return date;
}
} catch (Exception e) {
// 如果有异常,捕捉异常后继续解析
log.error(e.getMessage(), e);
}
}
}
// 如果有异常,捕捉异常后继续解析
throw new UnsupportedOperationException("The current operation is not supported by the current converter." + cellValue);
}
@Override
public WriteCellData<?> convertToExcelData(Date value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dateValue = sdf.format(value);
return new WriteCellData<>(dateValue);
}
}
本文来自博客园,作者:VipSoft 转载请注明原文链接:https://www.cnblogs.com/vipsoft/p/18594558
分类:
JAVA
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 本地部署 DeepSeek:小白也能轻松搞定!
· 基于DeepSeek R1 满血版大模型的个人知识库,回答都源自对你专属文件的深度学习。
· 如何给本地部署的DeepSeek投喂数据,让他更懂你
· 在缓慢中沉淀,在挑战中重生!2024个人总结!
· 大人,时代变了! 赶快把自有业务的本地AI“模型”训练起来!
2022-12-09 Exception: HOUR_OF_DAY: 0 -> 1
2022-12-09 大数据 - DWD&DIM 行为数据
2021-12-09 Win10家庭/企业/教育版均可升级到专业版