日期在Java中将dd-MMM-yyyy转换为dd-MM-yyyy

在Java中将23-Mar-2011转换为23-03-2011的最简单方法是什么?


感谢大家。 这似乎解决了这个问题:

try { Calendar cal = Calendar.getInstance(); cal.setTime(new SimpleDateFormat("dd-MMM-yyyy").parse("24-Nov-2002")); SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy"); System.out.println(sdf.format(cal.getTime())); } catch (ParseException e) { e.printStackTrace(); } 

你看过SimpleDateFormat吗?

试试这个

 String strDate="23-Mar-2011"; SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy"); try { Date varDate=dateFormat.parse(strDate); dateFormat=new SimpleDateFormat("dd-MM-yyyy"); System.out.println("Date :"+dateFormat.format(varDate)); }catch (Exception e) { // TODO: handle exception e.printStackTrace(); } 

这是一个更简单的代码版本:

 DateFormat shortFormat = new SimpleDateFormat("dd-MM-yyyy",Locale.ENGLISH); DateFormat mediumFormat = new SimpleDateFormat("dd-MMM-yyyy",Locale.ENGLISH); String s = "23-Mar-2011"; String shortDate = shortFormat.format(mediumFormat.parse(s)); System.out.println(shortDate); 

SimpleDateFormat format1 = new SimpleDateFormat(“dd-MMM-yyyy”);

SimpleDateFormat format2 = new SimpleDateFormat(“dd-MM-yyyy”);

日期日期= format1.parse(“24-Nov-2002”);

的System.out.println(format2.format(日期));