Convert dd-MMM-yyyy date to dd-MM-yyyy in java

What is the easiest way to convert 23-Mar-2011 to 23-03-2011 in Java?


Thanks to everyone. This seems to solve the problem:

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(); } 
+6
java date
source share
4 answers

Have you watched SimpleDateFormat ?

+6
source share

try it

 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(); } 
+6
source share

Here is a simpler version of your code:

 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); 
+1
source share

SimpleDateFormat format1 = new SimpleDateFormat ("dd-MMM-yyyy");

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

Date date = format1.parse ("24-November-2002");

System.out.println (format2.format (date));

0
source share

All Articles