参见英文答案 >
Parsing a string to date format in java defaults date to 1 and month to January2个
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,YYYY,EEE",Locale.US); System.out.println(sdf.format(new Date())); System.out.println(sdf.format(sdf.parse("Apr 27,2018,Fri")));
Java不会按预期解析日期并输出:
Apr 27,Fri Jan 05,Fri // I can not understand why java parse the month of April as January
解决方法
你的问题是你使用YYYY代表周年,而你想要代表年份的yyyy,请参阅所有选项
here.
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy,Locale.US);