2012-07-06 51 views
-2

可能重复的月份:
Improper result of SimpleDateFormat解析日期给出00

下面的代码输出的00/12/2010的日期。我无法弄清楚为什么日期字符串被毫无例外地解析,但是月份被设置为00.为了理智的缘故,我试着对Locale.Enlgish的SimpleDateFormat构造函数使用第二个参数,但这并没有帮助。有什么建议么?

String dateString = "12-OCT-10"; 
    SimpleDateFormat formatFrom = new SimpleDateFormat("dd-MMM-yy"); 
    Date date = formatFrom.parse(dateString); 
    SimpleDateFormat formatTo = new SimpleDateFormat("mm/dd/yyyy"); 
    System.out.println(formatTo.format(date)); 
+2

您的第一站应该是[文档](http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html)。然后[搜索](https://www.google.com/search?q=wrong+month+simpledateformat+site%3Astackoverflow.com)。 :-) – 2012-07-06 18:10:14

+0

不能同意更多。我阅读文档,我知道小写'm'是分钟的,这就是为什么我在第一个SimpleDateFormat中使用大写'M'的原因。我只是第二个愚蠢。愚蠢的错误。真的希望我能删除这篇文章。感谢您的帮助。 – Mario 2012-07-06 18:13:42

回答

12

mm的是分钟,使用MM;详情请参阅the documentation

+0

另请参阅:http://stackoverflow.com/questions/11356109/improper-result-of-simpledateformat/同一问题 – 2012-07-06 18:05:14

+1

啧啧,你得开玩笑吧。我不敢相信我这样做,并浪费了一个小时。感谢您成为一组额外的眼睛。 – Mario 2012-07-06 18:07:08

相关问题