是的,另一个
Java Date帖子:)
我的问题:
简单的标准代码,用于测试插入日期是否为请求的形式(dd / MM / yyyy):
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); try { sdf.parse(strDate); } catch(ParseException e) { // err }
strDate = “10-12-2012” —-> sysout = err (ok)
strDate = “2012-11-10” —-> sysout = err (ok)
strDate = “15/10/2011” —-> sysout = parse (ok)
问题:
strDate = “2012/12/15” —-> sysout = this date that i expect an error
result parsed like “Tue Jun 03 00:00:00 CET 21”
谁知道???
解决方法
只需将
setLenient设置为false.
FROM API:
Specify whether or not date/time parsing is to be lenient. With
lenient parsing,the parser may use heuristics to interpret inputs
that do not precisely match this object’s format. With strict parsing,
inputs must match this object’s format.
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); try { sdf.setLenient(false); System.out.println(sdf.parse("2012/12/15")); } catch(ParseException e) { e.printStackTrace(); }