字符串转时间格式-严格验证时间格式

来源:互联网 发布:seo资源 编辑:程序博客网 时间:2024/06/07 13:56

dateFormat.setLenient(false);

前台经常会用到字符串作为时间使用,那在进行Format时,设置这个属性false就会严格校验时间类型合法性


// 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期,//比如2017/02/29会被接受,并转换成2004/03/01,实际201702没有29号SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd"); dateFormat.setLenient(false);  try {      dateFormat.parse(startym);   }catch (ParseException e) {       e.printStackTrace();    throw new AppException("开始年月日格式不正确!<br>检查时间格式:"+startym);   }try {         dateFormat.parse(endym);  }catch (ParseException e) {     e.printStackTrace();   throw new AppException("结束年月日格式不正确!<br>检查时间格式:"+endym);}

阅读全文
0 0
原创粉丝点击