关于EasyExcel导入数据时表格日期格式识别为数字问题

参考官方地址

自定义日期转字符串转换器

/*** 自定义excel日期转换器** @author li* @date 2024-05-29*/
public class CustomStringDateConverter implements Converter<String> {@Overridepublic Class<?> supportJavaTypeKey() {return String.class;}@Overridepublic CellDataTypeEnum supportExcelTypeKey() {return CellDataTypeEnum.STRING;}/*** 读调用*/@Overridepublic String convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {Date date = null;//单元格格式为 文本类型if (CellDataTypeEnum.STRING.equals(cellData.getType())) {date = DateTimeUtil.parseMatched(cellData.getStringValue());if(date==null){throw new DmeoException("日期数据解析失败,请检查导入模板数据");}}//单元格格式为标准日期类型try {if (CellDataTypeEnum.NUMBER.equals(cellData.getType())) {if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {date = DateUtils.getJavaDate(cellData.getNumberValue().doubleValue(),globalConfiguration.getUse1904windowing());} else {date = DateUtils.getJavaDate(cellData.getNumberValue().doubleValue(),contentProperty.getDateTimeFormatProperty().getUse1904windowing());}}} catch (Exception e) {throw new DmeoException("日期数据解析失败,请检查导入模板数据");}return DateTimeUtil.format(date, FORMAT_SHORT);}/*** 写调用*/@Overridepublic WriteCellData<?> convertToExcelData(WriteConverterContext<String> context) {return new WriteCellData<>(context.getValue());}}
   @ExcelProperty(value = "日期",converter = CustomStringDateConverter.class)

控制层异常捕获,需要使用 e.getCause().getMessage()才能取到自己抛出的异常信息

       catch (ExcelDataConvertException e) {String msg = "第" + e.getRowIndex() + "行,第" + e.getColumnIndex() + "列"+e.getCause().getMessage()+"," +"解析字符数据为:"+ e.getCellData().getStringValue();} 

官方日期工具类

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Pattern;import com.alibaba.excel.util.BooleanUtils;
import com.alibaba.excel.util.MapUtils;
import org.apache.poi.ss.usermodel.DateUtil;
import org.springframework.util.StringUtils;/*** 官方日期工具类***/
public class DateUtils {/*** Is a cache of dates*/private static final ThreadLocal<Map<Short, Boolean>> DATE_THREAD_LOCAL =new ThreadLocal<>();/*** Is a cache of dates*/private static final ThreadLocal<Map<String, SimpleDateFormat>> DATE_FORMAT_THREAD_LOCAL =new ThreadLocal<>();/*** The following patterns are used in {@link #isADateFormat(Short, String)}*/private static final Pattern date_ptrn1 = Pattern.compile("^\\[\\$\\-.*?\\]");private static final Pattern date_ptrn2 = Pattern.compile("^\\[[a-zA-Z]+\\]");private static final Pattern date_ptrn3a = Pattern.compile("[yYmMdDhHsS]");// add "\u5e74 \u6708 \u65e5" for Chinese/Japanese date format:2017 \u5e74 2 \u6708 7 \u65e5private static final Pattern date_ptrn3b =Pattern.compile("^[\\[\\]yYmMdDhHsS\\-T/\u5e74\u6708\u65e5,. :\"\\\\]+0*[ampAMP/]*$");// elapsed time patterns: [h],[m] and [s]private static final Pattern date_ptrn4 = Pattern.compile("^\\[([hH]+|[mM]+|[sS]+)\\]");// for format which start with "[DBNum1]" or "[DBNum2]" or "[DBNum3]" could be a Chinese dateprivate static final Pattern date_ptrn5 = Pattern.compile("^\\[DBNum(1|2|3)\\]");// for format which start with "年" or "月" or "日" or "时" or "分" or "秒" could be a Chinese dateprivate static final Pattern date_ptrn6 = Pattern.compile("(年|月|日|时|分|秒)+");public static final String DATE_FORMAT_10 = "yyyy-MM-dd";public static final String DATE_FORMAT_14 = "yyyyMMddHHmmss";public static final String DATE_FORMAT_16 = "yyyy-MM-dd HH:mm";public static final String DATE_FORMAT_16_FORWARD_SLASH = "yyyy/MM/dd HH:mm";public static final String DATE_FORMAT_17 = "yyyyMMdd HH:mm:ss";public static final String DATE_FORMAT_19 = "yyyy-MM-dd HH:mm:ss";public static final String DATE_FORMAT_19_FORWARD_SLASH = "yyyy/MM/dd HH:mm:ss";private static final String MINUS = "-";public static String defaultDateFormat = DATE_FORMAT_19;public static String defaultLocalDateFormat = DATE_FORMAT_10;private DateUtils() {}/*** convert string to date** @param dateString* @param dateFormat* @return* @throws ParseException*/public static Date parseDate(String dateString, String dateFormat) throws ParseException {if (StringUtils.isEmpty(dateFormat)) {dateFormat = switchDateFormat(dateString);}return getCacheDateFormat(dateFormat).parse(dateString);}/*** convert string to date** @param dateString* @param dateFormat* @param local* @return*/public static LocalDateTime parseLocalDateTime(String dateString, String dateFormat, Locale local) {if (StringUtils.isEmpty(dateFormat)) {dateFormat = switchDateFormat(dateString);}if (local == null) {return LocalDateTime.parse(dateString, DateTimeFormatter.ofPattern(dateFormat));} else {return LocalDateTime.parse(dateString, DateTimeFormatter.ofPattern(dateFormat, local));}}/*** convert string to date** @param dateString* @param dateFormat* @param local* @return*/public static LocalDate parseLocalDate(String dateString, String dateFormat, Locale local) {if (StringUtils.isEmpty(dateFormat)) {dateFormat = switchDateFormat(dateString);}if (local == null) {return LocalDate.parse(dateString, DateTimeFormatter.ofPattern(dateFormat));} else {return LocalDate.parse(dateString, DateTimeFormatter.ofPattern(dateFormat, local));}}/*** convert string to date** @param dateString* @return* @throws ParseException*/public static Date parseDate(String dateString) throws ParseException {return parseDate(dateString, switchDateFormat(dateString));}/*** switch date format** @param dateString* @return*/public static String switchDateFormat(String dateString) {int length = dateString.length();switch (length) {case 19:if (dateString.contains(MINUS)) {return DATE_FORMAT_19;} else {return DATE_FORMAT_19_FORWARD_SLASH;}case 16:if (dateString.contains(MINUS)) {return DATE_FORMAT_16;} else {return DATE_FORMAT_16_FORWARD_SLASH;}case 17:return DATE_FORMAT_17;case 14:return DATE_FORMAT_14;case 10:return DATE_FORMAT_10;default:throw new IllegalArgumentException("can not find date format for:" + dateString);}}/*** Format date* <p>* yyyy-MM-dd HH:mm:ss** @param date* @return*/public static String format(Date date) {return format(date, null);}/*** Format date** @param date* @param dateFormat* @return*/public static String format(Date date, String dateFormat) {if (date == null) {return null;}if (StringUtils.isEmpty(dateFormat)) {dateFormat = defaultDateFormat;}return getCacheDateFormat(dateFormat).format(date);}/*** Format date** @param date* @param dateFormat* @return*/public static String format(LocalDateTime date, String dateFormat, Locale local) {if (date == null) {return null;}if (StringUtils.isEmpty(dateFormat)) {dateFormat = defaultDateFormat;}if (local == null) {return date.format(DateTimeFormatter.ofPattern(dateFormat));} else {return date.format(DateTimeFormatter.ofPattern(dateFormat, local));}}/*** Format date** @param date* @param dateFormat* @return*/public static String format(LocalDate date, String dateFormat) {return format(date, dateFormat, null);}/*** Format date** @param date* @param dateFormat* @return*/public static String format(LocalDate date, String dateFormat, Locale local) {if (date == null) {return null;}if (StringUtils.isEmpty(dateFormat)) {dateFormat = defaultLocalDateFormat;}if (local == null) {return date.format(DateTimeFormatter.ofPattern(dateFormat));} else {return date.format(DateTimeFormatter.ofPattern(dateFormat, local));}}/*** Format date** @param date* @param dateFormat* @return*/public static String format(LocalDateTime date, String dateFormat) {return format(date, dateFormat, null);}/*** Format date** @param date* @param dateFormat* @return*/public static String format(BigDecimal date, Boolean use1904windowing, String dateFormat) {if (date == null) {return null;}LocalDateTime localDateTime = DateUtil.getLocalDateTime(date.doubleValue(),BooleanUtils.isTrue(use1904windowing), true);return format(localDateTime, dateFormat);}private static DateFormat getCacheDateFormat(String dateFormat) {Map<String, SimpleDateFormat> dateFormatMap = DATE_FORMAT_THREAD_LOCAL.get();if (dateFormatMap == null) {dateFormatMap = new HashMap<String, SimpleDateFormat>();DATE_FORMAT_THREAD_LOCAL.set(dateFormatMap);} else {SimpleDateFormat dateFormatCached = dateFormatMap.get(dateFormat);if (dateFormatCached != null) {return dateFormatCached;}}SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);dateFormatMap.put(dateFormat, simpleDateFormat);return simpleDateFormat;}/*** Given an Excel date with either 1900 or 1904 date windowing,* converts it to a java.util.Date.** Excel Dates and Times are stored without any timezone* information. If you know (through other means) that your file* uses a different TimeZone to the system default, you can use* this version of the getJavaDate() method to handle it.** @param date             The Excel date.* @param use1904windowing true if date uses 1904 windowing,*                         or false if using 1900 date windowing.* @return Java representation of the date, or null if date is not a valid Excel date*/public static Date getJavaDate(double date, boolean use1904windowing) {//To calculate the Date, in the use of `org.apache.poi.ss.usermodel.DateUtil.getJavaDate(double, boolean,// java.util.TimeZone, boolean), Date when similar `2023-01-01 00:00:00.500`, returns the`2023-01-01// 00:00:01`, but excel in fact shows the `2023-01-01 00:00:00`.// `org.apache.poi.ss.usermodel.DateUtil.getLocalDateTime(double, boolean, boolean)` There is no problem.return Date.from(getLocalDateTime(date, use1904windowing).atZone(ZoneId.systemDefault()).toInstant());}/*** Given an Excel date with either 1900 or 1904 date windowing,* converts it to a java.time.LocalDateTime.** Excel Dates and Times are stored without any timezone* information. If you know (through other means) that your file* uses a different TimeZone to the system default, you can use* this version of the getJavaDate() method to handle it.** @param date             The Excel date.* @param use1904windowing true if date uses 1904 windowing,*                         or false if using 1900 date windowing.* @return Java representation of the date, or null if date is not a valid Excel date*/public static LocalDateTime getLocalDateTime(double date, boolean use1904windowing) {return DateUtil.getLocalDateTime(date, use1904windowing, true);}/*** Given an Excel date with either 1900 or 1904 date windowing,* converts it to a java.time.LocalDate.** Excel Dates and Times are stored without any timezone* information. If you know (through other means) that your file* uses a different TimeZone to the system default, you can use* this version of the getJavaDate() method to handle it.** @param date             The Excel date.* @param use1904windowing true if date uses 1904 windowing,*                         or false if using 1900 date windowing.* @return Java representation of the date, or null if date is not a valid Excel date*/public static LocalDate getLocalDate(double date, boolean use1904windowing) {LocalDateTime localDateTime = getLocalDateTime(date, use1904windowing);return localDateTime == null ? null : localDateTime.toLocalDate();}/*** Determine if it is a date format.** @param formatIndex* @param formatString* @return*/public static boolean isADateFormat(Short formatIndex, String formatString) {if (formatIndex == null) {return false;}Map<Short, Boolean> isDateCache = DATE_THREAD_LOCAL.get();if (isDateCache == null) {isDateCache = MapUtils.newHashMap();DATE_THREAD_LOCAL.set(isDateCache);} else {Boolean isDatecachedDataList = isDateCache.get(formatIndex);if (isDatecachedDataList != null) {return isDatecachedDataList;}}boolean isDate = isADateFormatUncached(formatIndex, formatString);isDateCache.put(formatIndex, isDate);return isDate;}/*** Determine if it is a date format.** @param formatIndex* @param formatString* @return*/public static boolean isADateFormatUncached(Short formatIndex, String formatString) {// First up, is this an internal date format?if (isInternalDateFormat(formatIndex)) {return true;}if (StringUtils.isEmpty(formatString)) {return false;}String fs = formatString;final int length = fs.length();StringBuilder sb = new StringBuilder(length);for (int i = 0; i < length; i++) {char c = fs.charAt(i);if (i < length - 1) {char nc = fs.charAt(i + 1);if (c == '\\') {switch (nc) {case '-':case ',':case '.':case ' ':case '\\':// skip current '\' and continue to the next charcontinue;}} else if (c == ';' && nc == '@') {i++;// skip ";@" dupletscontinue;}}sb.append(c);}fs = sb.toString();// short-circuit if it indicates elapsed time: [h], [m] or [s]if (date_ptrn4.matcher(fs).matches()) {return true;}// If it starts with [DBNum1] or [DBNum2] or [DBNum3]// then it could be a Chinese datefs = date_ptrn5.matcher(fs).replaceAll("");// If it starts with [$-...], then could be a date, but// who knows what that starting bit is all aboutfs = date_ptrn1.matcher(fs).replaceAll("");// If it starts with something like [Black] or [Yellow],// then it could be a datefs = date_ptrn2.matcher(fs).replaceAll("");// You're allowed something like dd/mm/yy;[red]dd/mm/yy// which would place dates before 1900/1904 in red// For now, only consider the first onefinal int separatorIndex = fs.indexOf(';');if (0 < separatorIndex && separatorIndex < fs.length() - 1) {fs = fs.substring(0, separatorIndex);}// Ensure it has some date letters in it// (Avoids false positives on the rest of pattern 3)if (!date_ptrn3a.matcher(fs).find()) {return false;}// If we get here, check it's only made up, in any case, of:// y m d h s - \ / , . : [ ] T// optionally followed by AM/PMboolean result = date_ptrn3b.matcher(fs).matches();if (result) {return true;}result = date_ptrn6.matcher(fs).find();return result;}/*** Given a format ID this will check whether the format represents an internal excel date format or not.** @see #isADateFormat(Short, String)*/public static boolean isInternalDateFormat(short format) {switch (format) {// Internal Date Formats as described on page 427 in// Microsoft Excel Dev's Kit...// 14-22case 0x0e:case 0x0f:case 0x10:case 0x11:case 0x12:case 0x13:case 0x14:case 0x15:case 0x16:// 27-36case 0x1b:case 0x1c:case 0x1d:case 0x1e:case 0x1f:case 0x20:case 0x21:case 0x22:case 0x23:case 0x24:// 45-47case 0x2d:case 0x2e:case 0x2f:// 50-58case 0x32:case 0x33:case 0x34:case 0x35:case 0x36:case 0x37:case 0x38:case 0x39:case 0x3a:return true;}return false;}public static void removeThreadLocalCache() {DATE_THREAD_LOCAL.remove();DATE_FORMAT_THREAD_LOCAL.remove();}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/18919.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

SQL常用语句(三)-- Now()返回当前本地日期和时间的日期/时间函数,以及时间的加减

#获取当前时间&#xff1a; SELECT NOW() AS newTime; #2019-05-20 14:28:41 #当前时间减30秒&#xff1a; SELECT (NOW() - INTERVAL 30 SECOND) AS newTime; #2019-05-20 14:29:31 #当前时间加30秒&#xff1a; SELECT (NOW() INTERVAL 30 SECOND) AS newTime; …

三十五岁零基础转行成为AI大模型开发者怎么样呢?

以下从3个方面帮大家分析&#xff1a; 35岁转行会不会太晚&#xff1f;零基础学习AI大模型开发能不能学会&#xff1f;AI大模型开发行业前景如何&#xff0c;学完后能不能找到好工作&#xff1f; 一、35岁转行会不会太晚&#xff1f; 35岁正处于人生的黄金时期&#xff0c;拥…

今日选题.

诱导读者点开文章的9引真经&#xff08;二&#xff09; 标题重要么&#xff1f;新媒体、博客文通常在手机上阅读。首先所有的内容不同于纸媒&#xff0c;手机只展现标题&#xff0c;而内容都是折叠。其次读者能像看内容一样看4、5条或者7、8条标题&#xff08;区别于不同的主流…

如何在Web页面中集成AI图像识别功能

前言 在信息时代&#xff0c;Web 页面成为我们与世界交互的重要窗口。AI 技术的迅猛发展&#xff0c;特别是图像识别技术&#xff0c;为 Web 页面带来了革命性的变化。通过在 Web 页面上实现图像识别&#xff0c;我们即将迈入一个更加智能与便捷的时代。这种技术不仅使网页能够…

2024广东省赛 C.DFS序

题目 #include <bits/stdc.h> using namespace std; #define int long long #define pb push_back #define fi first #define se second #define lson p << 1 #define rson p << 1 | 1 #define ll long long const int maxn 1e6 5, inf 1e9, maxm 4e4 …

代码助手之-百度Comate智能体验

简介 越来越多的厂商提供了智能代码助手&#xff0c;百度也不例外。Baidu Comate&#xff08;智能代码助手&#xff09;是基于文心大模型&#xff0c;Comate取自Coding Mate&#xff0c;寓意大家的AI编码伙伴。Comate融合了百度内部多年积累的编程现场大数据和外部开源代码和知…

如何顺利通过软考中级系统集成项目管理工程师?

中级资格的软考专业包括"信息系统"&#xff0c;属于软考的中级级别。熟悉软考的人都知道&#xff0c;软考分为初级、中级和高级三个级别&#xff0c;涵盖计算机软件、计算机网络、计算机应用技术、信息系统和信息服务五个专业&#xff0c;共设立了27个资格。本文将详…

全程曝光 计算机领域顶会投稿后会经历哪些关键环节?

会议之眼 快讯 亲爱的计算机领域大牛们&#xff0c;当你挥洒汗水&#xff0c;精心打磨一篇科研论文&#xff0c;终于怀着激动的心情投稿至顶会——&#xff08;如&#xff08;ACM MM 、ACL、AAAI&#xff09;时&#xff0c;你是否想知道接下来这篇论文会经历怎样的旅程&#x…

产业园物业满意度调研如何抽样

本文由群狼调研&#xff08;长沙商圈选址调查&#xff09;出品&#xff0c;欢迎转载&#xff0c;请注明出处。在进行产业园物业满意度调研时&#xff0c;可以采用以下抽样方法&#xff1a; 群体抽样&#xff1a;从产业园内的不同群体中随机选择样本。例如&#xff0c;可以根据不…

Mybatis进阶——动态SQL(1)

目录 一、 <if> 标签 二、<trim> 标签 三、<where> 标签 四、<set> 标签 五、<foreach> 标签 六、<include> 标签 动态SQL 是Mybatis的强大特性之一&#xff0c;能够完成不同条件下的不同SQL拼接&#xff0c;可以参考官方文档&#…

pyQt处理任务等待动画

写了一个显示Qt正在处理内容的等待动画&#xff0c;任务另开一个线程执行&#xff0c;执行完后自动关闭动画 from PyQt5 import QtCore, QtWidgets from PyQt5.QtWidgets import QApplication, QMessageBox, QDialog, QVBoxLayout from PyQt5.QtCore import pyqtSignal, QTime…

理解消息队列:队列与主题的区别

理解消息队列:队列与主题的区别 如果你研究过多种消息队列产品,可能会发现每种消息队列都有自己的一套消息模型,像队列(Queue)、主题(Topic)或分区(Partition)这些名词概念在不同的消息队列模型中含义各异。这是因为没有统一的标准。尽管曾有国际组织尝试制定过消息相…

springboot 作为客户端接收服务端的 tcp 长连接数据,并实现自定义结束符,解决 粘包 半包 问题

博主最近的项目对接了部分硬件设备&#xff0c;其中有的设备只支持tcp长连接方式传输数据&#xff0c;博主项目系统平台作为客户端发起tcp请求到设备&#xff0c;设备接收到请求后作为服务端保持连接并持续发送数据到系统平台。 1.依赖引入 连接使用了netty&#xff0c;如果项…

CPU占用率很高,相应很慢排查思路

获取线程状态 通过top -c命令可以动态显示进程及其占用资源的排行榜 可以看到&#xff0c;CPU占用率100%的PID是80972&#xff0c;定位到该进程之后&#xff0c;我们再从线程的dump日志中去定位. 使用top -H -p 80972命令查找到该进程中消耗CPU最多的线程&#xff0c;从下面的…

Apose.Words 常用对象详解

系列文章目录 文章目录 系列文章目录前言一、基础对象1. moveToBookmark 前言 本文介绍 Apose.Words 的常用对象的含义及使用方法。 一、基础对象 1. moveToBookmark 将指针移动到书签位置。 moveToBookmark(String bookmarkName, boolean isStart, boolean isAfter) book…

国产可视化爬虫助力AI大模型训练:精准爬取汉语词典

大语言模型&#xff0c;可以生成流畅对话的会话聊天机器人、通畅起草文章的内容生成器。在炫酷技术的背后&#xff0c;数据、算力、算法&#xff0c;被视作生成式AI的三个核心要素。由此可见&#xff0c;高质量的训练数据对于AI算法的准确性至关重要。 如何获得高质量的训练数…

【方法】如何禁止查看压缩包里的内容?

使用压缩文件&#xff0c;可以让文件更方便存储和传输&#xff0c;那对于重要的文件&#xff0c;如何防止随意查看压缩包的内容呢&#xff1f;我们可以试试以下两个方法。 方法1&#xff1a; 最常见的便是给压缩包设置“打开密码”&#xff0c;这样只有通过密码才能查看文件内…

外汇天眼:PayPoint投资100万英镑,深化与Aperidata开放银行合作

PayPoint今日宣布对Aperidata Ltd进行100万英镑的投资&#xff0c;Aperidata是一家创新的消费者和商业信用报告及开放银行平台。 此交易将使PayPoint集团在两家公司之间现有的商业合作基础上更进一步&#xff0c;为包括政府、地方当局、慈善机构和住房协会在内的多个领域的客户…

uniapp tabBar app页面滚动闪屏的问题

我在做app的时候&#xff0c;调试tabBar页面滚动时莫名其妙的闪屏&#xff0c;其他页面不闪屏&#xff0c;可能跟新建的项目样式有关。 修改方法如下。 在pages.json中 "tabBar": {"selectedColor": "#204AFF","color": "#ccc…

PaddleOCR2.7+Qt5

章节一&#xff1a;Windows 下的 PIP 安装 官网安装教程地址 按照里面的教程去安装 如果使用cuda版本的还要安装tensorrt&#xff0c;不然后面运行demo程序的程序会报如下错。 下载TensorRT 8版本&#xff0c;tensorrt下载地址 章节二&#xff1a;编译源码 进入官网源码地址 下…