我有一个简单的例子:
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/YY");
System.out.println( sdf.format(new Date()));
try {
System.out.println( sdf.parse("10/18/14") );
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
在控制台中,我收到了 14年10月18日 Sun Dec 29 00:00:00 EST 2013
任何解释为什么sdf.parse将于2013年10月29日返回而不是2014年10月18日
答案 0 :(得分:4)
您的格式String
为YY
,应为yy
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy");
根据SimpleDateFormat
Javadoc(部分说明),
Letter Date or Time Component Presentation Examples G Era designator Text AD y Year Year 1996; 96 Y Week year Year 2009; 09
目前您已获得周年。
当我进行上述更改时,我得到(预期的)
10/18/14
Sat Oct 18 00:00:00 EDT 2014
答案 1 :(得分:0)
同样的事情发生在这里:SimpleDateFormat appears to give an incorrect result 试着先看看是否有人有同样的问题,为你的问题找到答案会更容易,更快。