DateFormat setLenient
之前都沒注意過有這么個方法,否則判斷日期格式就太不靠譜了。
又是晦澀的單詞:setLenient(false),嚴(yán)格匹配日期格式
又是晦澀的單詞:setLenient(false),嚴(yán)格匹配日期格式
SimpleDateFormat df = new SimpleDateFormat("MMddyyyy");
/*
* 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.
*/
df.setLenient(false);
try {
Date a = df.parse("23232011");
System.out.print("date: " + a);
} catch (java.text.ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*
* 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.
*/
df.setLenient(false);
try {
Date a = df.parse("23232011");
System.out.print("date: " + a);
} catch (java.text.ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
posted on 2011-07-18 17:23 cerulean 閱讀(3210) 評論(0) 編輯 收藏 所屬分類: Java