Converting yyyy-mm-dd into dd mm yyyy

user1740281 picture user1740281 · Jun 26, 2013 · Viewed 26.2k times · Source

How to convert 2013-06-24 to 24 Jun 2013? I am using the below code.

date1="2013-06-24";
SimpleDateFormat d= new SimpleDateFormat("dd MMM yyyy");

try{
date2 =  d.parse(date1);
}catch (ParseException e1) {
// TODO Auto-generated catch block
  e1.printStackTrace();
}  

But I am getting this error "java.text.ParseException: Unparseable date: "2013-06-24" (at offset 4)"

Answer

duffymo picture duffymo · Jun 26, 2013

You need two DateFormat instances: One to parse the original String, and another to output the one you want.

DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd");
DateFormat outputFormat = new SimpleDateFormat("dd MMM yyyy");
String inputDateStr="2013-06-24";
Date date = inputFormat.parse(inputDateStr);
String outputDateStr = outputFormat.format(date);