使用EasyExcel导入excel中的日期格式数据时获取到的却是一个数字

背景:

在一次处理excel批量数据导入时,需要导入一个订单的发货时间,导入模板中对应的时间那一列使用的是日期格式。那么导入进来DEBUG发现是一个数字,比如2022年7月5日导入进来之后就变成了44745。

原因:

因为excel中的时间是从1900年开始的,而转换成文本类型的数字则代表着1900年之后的N天,知道这个原理之后,就很好解决这个问题了:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
     * 将日期数字转为时间格式
     * daysDuration = 44745
     *
     * @param daysDuration
     * @return
     */
    public static String getTime(String daysDuration) {
        //如果不是数字
        if(!isNumeric(daysDuration)){
            return null;
        }
        //如果是数字 小于0则 返回
        BigDecimal bd = new BigDecimal(daysDuration);
        int days = bd.intValue();//天数
        int mills = (int) Math.round(bd.subtract(new BigDecimal(days)).doubleValue() * 24 * 3600);
 
        //获取时间
        Calendar c = Calendar.getInstance();
        c.set(1900, 0, 1);
        c.add(Calendar.DATE, days);
        int hour = mills / 3600;
        int minute = (mills - hour * 3600) / 60;
        int second = mills - hour * 3600 - minute * 60;
        c.set(Calendar.HOUR_OF_DAY, hour);
        c.set(Calendar.MINUTE, minute);
        c.set(Calendar.SECOND, second);
 
        return dateFormat.format(c.getTime());
    }
 
    /**
     * 校验是否数据含小数点
     *
     * @param str
     * @return
     */
    private static boolean isNumeric(String str){
        Pattern pattern = Pattern.compile("[0-9]+\\.*[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if(!isNum.matches()){
            return false;
        }
        return true;
    }

  

本篇文章如有帮助到您,请给「翎野君」点个赞,感谢您的支持。

首发链接:https://www.cnblogs.com/lingyejun/p/16444826.html

posted @   翎野君  阅读(2931)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 【.NET】调用本地 Deepseek 模型
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
历史上的今天:
2018-07-05 接口API中的敏感数据基于AES进行安全加密后返回
2017-07-05 JVM内存—堆(heap)栈(stack)方法区(method) (转)
点击右上角即可分享
微信分享提示