SimpleDateFormat返回NULL JDK7的日期,但与JDK6工作正常

问题描述:

我有这个示例Java代码,我想解析一个字符串到日期,基于SimpleDateFormat上设置的模式。当我在JDK6中运行这个代码时,它的工作正常。但在JDK7中,解析调用返回NULL。任何想法已经被JDK7改变了。这是一个已知的问题或任何解决方法?SimpleDateFormat返回NULL JDK7的日期,但与JDK6工作正常

SimpleDateFormat _theSimpleDateFormatHelper = new SimpleDateFormat(); 
    _theSimpleDateFormatHelper.setLenient(false); 
    _theSimpleDateFormatHelper.applyPattern("yyyy-MM-dd hh:mm:ss"); 

    ParsePosition parsePos = new ParsePosition(0); 
    Object formattedObj = _theSimpleDateFormatHelper.parse("1989-09-21 00:00:00", parsePos); 

下面的代码工作正常:

SimpleDateFormat _theSimpleDateFormatHelper = new SimpleDateFormat(); 
//_theSimpleDateFormatHelper.setLenient(false); <-- In lenient mode, the parsing succeeds 
_theSimpleDateFormatHelper.applyPattern("yyyy-MM-dd hh:mm:ss"); 

ParsePosition parsePos = new ParsePosition(0); 
Object formattedObj = _theSimpleDateFormatHelper.parse("1989-09-21 00:00:00", parsePos); 

它不工作的原因是因为该格式是严格模式不正确。在此页面http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html中,您可以看到h范围是1-12。

如果使用H相反,范围是0-23,这也能发挥作用:

SimpleDateFormat _theSimpleDateFormatHelper = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
_theSimpleDateFormatHelper.setLenient(false); 
Object formattedObj = _theSimpleDateFormatHelper.parse("1989-09-21 00:00:00");