【发布时间】:2015-05-22 13:34:03
【问题描述】:
也许这个问题很傻,但我没有找到任何让我满意的答案。
我有一个 SimpleDataFormat,例如:
sdf = new SimpleDateFormat("dd/MM/yyyy");
如果我尝试解析如下日期:10/10/15
那么日期结果是 15 年,而不是 2015 年。
为什么parse 在这种情况下有效?我期待一个ParseException
如何强制用户以 4 位格式输入年份? (请不要String.length())
我正在使用setLenient(false)
【问题讨论】:
-
嗯,看来你确实是一年了
15是不是一个有效的日期? -
@gtgaxiola 不,我在问为什么用两位数的解析工作。我希望它失败,我想强制使用四位数年份的日期
-
知道了。那么
Date一直是“不确定的”。如果我可以引用加勒比海盗的话,格式“更像是你所说的'指南'而不是实际规则”。 -
来自Javadoc of SimpleDateFormat
For parsing, if the number of pattern letters is more than 2, the year is interpreted literally, regardless of the number of digits. So using the pattern "MM/dd/yyyy", "01/11/12" parses to Jan 11, 12 A.D. -
@gtgaxiola 谢谢 :) 我想我会使用字符串长度。
标签: java date simpledateformat