您的格式必须符合您的输入
对于
2016-03-20
格式应为(仅使用第二个
SimpleDateFormat对象)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");完整答案
SimpleDateFormat month_date = new SimpleDateFormat("MMM yyyy", Locale.ENGLISH);SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");String actualDate = "2016-03-20";Date date = sdf.parse(actualDate);String month_name = month_date.format(date);System.out.println("Month :" + month_name); //Mar 2016使用
java.timejava-8
String actualDate = "2016-03-20";DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.ENGLISH);DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("MMM yyyy", Locale.ENGLISH);LocalDate ld = LocalDate.parse(actualDate, dtf);String month_name = dtf2.format(ld);System.out.println(month_name); // Mar 2016


