java SimpleDateFormat模式与参数不同

SimpleDateFormat模式是“yyyyMM”,arg是yyyy-MM,但没有exception和错误的结果。 为什么? THX ~~

SimpleDateFormat format = new SimpleDateFormat("yyyyMM"); System.out.println(format.format(format.parse("2011-07"))); 

结果是201105

使用setLenient(false) ; 然后它会抛出你期望的exception。

请参阅setLenient()文档 。

parse的输入是错误的,试试

的System.out.println(format.format(format.parse( “201107”)));

在2011 – 07年,它将月份解释为负7个月,结果是201005(12-7 = 5)。