简单日期格式化程序中的月份总是返回1月

我使用简单的日期格式将字符串转换为日期,但日期的月份总是返回JANUARY这里是我的代码:

public static void main(String args[]){ String date = "2017-12-29"; SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd"); SimpleDateFormat dateFormatter = new SimpleDateFormat("MMMM dd, YYYY"); try { Date startEntryDate = formatter.parse(date); System.out.println(dateFormatter.format(startEntryDate)); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } 

如前所述,你必须使用MM而不是mm ,但是?
为什么不使用java.time库:

 String month = LocalDate.parse("2017-12-29").getMonth().name(); 

尝试使用

new SimpleDateFormat("yyyy-MM-dd");

mm表示分钟。 MM – 月。

 Letter Date or Time Component Presentation Examples G Era designator Text AD y Year Year 1996; 96 M Month in year Month July; Jul; 07 w Week in year Number 27 W Week in month Number 2 D Day in year Number 189 d Day in month Number 10 F Day of week in month Number 2 E Day in week Text Tuesday; Tue a Am/pm marker Text PM H Hour in day (0-23) Number 0 k Hour in day (1-24) Number 24 K Hour in am/pm (0-11) Number 0 h Hour in am/pm (1-12) Number 12 m Minute in hour Number 30 s Second in minute Number 55 S Millisecond Number 978 z Time zone General time zone Pacific Standard Time; PST; GMT-08:00 Z Time zone RFC 822 time zone -0800 

尝试这个:

 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat dateFormatter = new SimpleDateFormat("MMMM dd, yyyy"); 
  Try this: new SimpleDateFormat("MM/dd/yyyy") MM is "month" (not mm) dd is "day" (not DD) It's all in the javadoc for SimpleDateFormat FYI, the reason your format is still a valid date format is that: mm is "minutes" DD is "day in year" Also, you don't need the cast to Date... it already is a Date (or it explodes): public static void main(String[] args) throws ParseException { System.out.println(new SimpleDateFormat("MM/dd/yyyy").parse("08/16/2011")); } Output: 

2011年8月16日星期二00:00:00

尝试这个:

  String date = "2017-8-28"; SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat dateFormatter = new SimpleDateFormat("MMMM dd, YYYY"); try { Date startEntryDate = formatter.parse(date); System.out.println(dateFormatter.format(startEntryDate)); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); }