SimpleDateFormatparsing时忽略月份

以下代码将parsingdate定义为“Wed Jan 13 00:00:00 EST 2010”,而不是“Wed Jun 13 00:00:00 EST 2010”。 任何想法非常赞赏。

SimpleDateFormat sf = new SimpleDateFormat("yyyy-mm-dd'T'HH:mm:ss"); String str = "2010-06-13T00:00:00"; Date date = sf.parse(str); System.out.println(" Date " + date.toString()); 

尝试:

 "yyyy-MM-dd'T'HH:mm:ss" 

MM表示月份。 mm是指分钟。 有关支持的date和时间模式的更多详细信息,请参阅SimpleDateFormat的文档。

问题在于,你使用“mm”作为月份,“mm”代表分钟。 以下是所有可用的date格式,请在这里阅读更多文档。

 Symbol Meaning Kind Example D day in year Number 189 E day of week Text E/EE/EEE:Tue, EEEE:Tuesday, EEEEE:T F day of week in month Number 2 (2nd Wed in July) G era designator Text AD H hour in day (0-23) Number 0 K hour in am/pm (0-11) Number 0 L stand-alone month Text L:1 LL:01 LLL:Jan LLLL:January LLLLL:J M month in year Text M:1 MM:01 MMM:Jan MMMM:January MMMMM:J S fractional seconds Number 978 W week in month Number 2 Z time zone (RFC 822) Time Zone Z/ZZ/ZZZ:-0800 ZZZZ:GMT-08:00 ZZZZZ:-08:00 a am/pm marker Text PM c stand-alone day of week Text c/cc/ccc:Tue, cccc:Tuesday, ccccc:T d day in month Number 10 h hour in am/pm (1-12) Number 12 k hour in day (1-24) Number 24 m minute in hour Number 30 s second in minute Number 55 w week in year Number 27 G era designator Text AD y year Number yy:10 y/yyy/yyyy:2010 z time zone Time Zone z/zz/zzz:PST zzzz:Pacific Standard 

示例如果date是06 07 2016

 SimpleDateFormat sdf= new SimpleDateFormat("dd MM yyyy"); 

您可以在这些格式之间使用逗号,句号,斜杠或连字符。

  Ex: dd-mm-yyyy, it will display like(06-07-2016) dd/mm/yyyy,it will display like(06/07/2016) dd.mm.yyyy,it will display like(06.07.2016) dd,mm,yyyy ,it will display like(06,07,2016) MM - will display number of the Month. MMM - will display Month Three character only(Ex: Jul) MMMM - will display full month(Ex: July) yyyy - will display full year(2016) yy - will display last two digits(16) hh - will display hours mm -will display minutes ss - will display seconds a - will display AM or PM Ex: if time is 12:09:10 PM means (hh:mm:ss a) EEE- will display short week name(Ex: Wed) EEEE- will display full week name(Ex: Wednesday)