导入Excel日期列数据处理

原文地址

导入Excel日期列数据的处理,直接将单元格的内容拼接为日期格式,如果拼接失败则说明不是日期格式的数据。

import cn.hutool.core.date.DateUtil;

public static DateparseDate(String dateStr) {

try{

DateTime parse = DateUtil.parse(dateStr);

        return parse.toJdkDate();

    }catch (Exception e) {

}

String year ="";

    int month =0;

    String yearTag ="";

    try{

yearTag = dateStr.substring(4, 5);

    }catch (Exception e) {

return null;

    }

String[] yearSplit = dateStr.split(yearTag);

    year = yearSplit[0];

    String monthSplit = yearSplit[1];

    try{

boolean haveZero = monthSplit.startsWith("0");

        if (haveZero) {

String tempTestMonth = monthSplit.substring(0, 2);

            month = Integer.parseInt(tempTestMonth);

        }else {

String tempTestMonth = monthSplit.substring(0, 2);

            month = Integer.parseInt(tempTestMonth);

        }

}catch (Exception e) {

try{

String tempTestMonth = monthSplit.substring(0, 1);

            month = Integer.parseInt(tempTestMonth);

        }catch (Exception e1) {

}

}

try{

DateUtil.parse(year +"-" + month, "yyyy-MM");

    }catch (Exception e) {

System.out.println("日期格式错误");

return null;

    }

return DateUtil.parse(year +"-" + month, "yyyy-MM");

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容