poi导入excel表格数据到数据库的时候,对出生日期的校验

时间:2023-03-09 00:14:44
poi导入excel表格数据到数据库的时候,对出生日期的校验

出生日期格式为8位数字的字符串 如:yyyyMMdd 规则:yyyy大于1900并小于当前时间,月、日 按日期规则校验

       //解决读过来的字符串显示为科学计数法问题
BigDecimal bd = new BigDecimal(strValues[6]);//strValues[6]为第6列读取的数据
String s = bd.toPlainString();
//设置出生日期格式
SimpleDateFormat sd = new SimpleDateFormat("yyyyMMdd");
//获取当前系统时间
Integer time = Integer.valueOf(sd.format(new Date()));
//setLenient用于设置Calendar是否宽松解析字符串,如果为false,则严格解析;默认为true,宽松解析
sd.setLenient(false);
try {
//转成指定的格式
sd.parse(bd.toPlainString());
} catch (ParseException e) {
throw new BaseException("出生日期格式不正确");
}
if ((1900 > Integer.valueOf(s.substring(0,4))) || (Integer.valueOf(s) > time)){
throw new BaseException("出生日期格式不正确");
}
accountInfoVO.setBirthDate(s);//出生日期(放数据)