导入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");

}

你可能感兴趣的:(导入Excel日期列数据处理)