代碼
String MonthYear = null; SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm/yyyy"); String dateformat = "2012-11-17T00:00:00.000-05:00" MonthYear = simpleDateFormat.format(dateformat); System.out.println(MonthYear);
解決方法:(這個錯誤的原因就是說,String類型無法直接parse為一個date類型,所以需要先把String字符串,格式化為一個datea類型,最后再格式化為你想要的格式)
DateFormat outputFormat = new SimpleDateFormat("yyyy/MM"); DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.US); Date date = null; String inputText = "2012-11-17"; try { date = inputFormat.parse(inputText); }catch (Exception e){ e.printStackTrace(); } String outputText = outputFormat.format(date); System.out.println(outputText);
https://blog.csdn.net/S_body/article/details/80453023