|
原帖由 java307 于 2008-9-10 21:48 发表 ![]()
parse不但过了,而且是错误的过了
try
{
String newValue = "1997-02-29";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(newValue);
System.out.println(date.toString());
}
catch(Exception ex)
{
ex.printStackTrace();
}
打印结果是: Sat Mar 01 00:00:00 CST 1997 1997年闰年,没29号,于是它分析出来一个3月1号,有意思
如果输入1997-45-56
则结果是: Thu Oct 26 00:00:00 CST 2000 它把(45-12)和(56-30)的部分算成把日期向后面增加,真有意思,不知道这个函数这么设计
有什么意义?
兄弟,API再看一遍吧,说的很清楚:
* By default, parsing is lenient: If the input is not in the form used
* by this object's format method but can still be parsed as a date, then
* the parse succeeds. Clients may insist on strict adherence to the
* format by calling setLenient(false)
你加上sdf.setLenient(false);不就行了 |
|