2011-03-23 8 views
6

¿Cuál es la forma más sencilla de convertir 23-Mar-2011 a 23-03-2011 en Java?Fecha de conversión dd-MMM-aaaa a dd-MM-aaaa en java


Gracias a todos. Esto parece resolver el problema:

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(); } 
+0

Gracias a todos encontró el answer.'try { \t \t \t Calendario cal = Calendar.getInstance(); \t \t \t cal.setTime (nuevo SimpleDateFormat ("dd-MMM-yyyy"). Parse ("24-Nov-2002")); \t \t \t SimpleDateFormat sdf = new SimpleDateFormat ("dd-MM-yyyy"); \t \t \t System.out.println (sdf.format (cal.getTime())); \t \t} catch (ParseException e) { \t \t \t e.printStackTrace(); \t \t} ' –

Respuesta

6

¿Has mirado SimpleDateFormat?

+0

Sí, lo hice. Pero obteniendo una java.text.ParseException –

+0

por favor publique su código aquí –

+0

Gracias por su ayuda. –

1

Aquí es una versión más simple de su código:

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); 
0

SimpleDateFormat formato1 = new SimpleDateFormat ("dd-mmm-aaaa");

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

Fecha de fecha = format1.parse ("24-Nov-2002");

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

6

Prueba este

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(); 
}