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)"
There is a formula that can quickly convert dd/mm/yyyy to mm/dd/yyyy date format. Select a blank cell next to the dates you want to convert, type this formula =DATE(VALUE(RIGHT(A9,4)), VALUE(MID(A9,4,2)), VALUE(LEFT(A9,2))), and drag fill handle over the cells which need to use this formula.
Select the data, one column at at time. Select > Data > Text to Columns , select Delimited > Next , select Tab > Next , select Date and choose from the pull-down menu the format in which the data is present in the text (e.g. DMY or MDY etc.).
Select a blank cell next to your date, for instance. I1, and type this formula =TEXT(G1, "yyyy-mm-dd"), and press Enter key, then drag AutoFill handle over the cells needed this formula.
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);
See the first problem is that you are using different delimiters for String and Date. So either you do "2013-06-24" to "2013 06 24" in String or do new SimpleDateFormat("dd MMM yyyy") to new SimpleDateFormat("dd-MMM-yyyy").
And second problem is that you cannot directly change format like this, in String you are having year-month-date format, so first make a Date object with same format than change it to your desired format as below :
date1="2013-06-24";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date dt = format.parse(date1);
SimpleDateFormat your_format = new SimpleDateFormat("dd-MMM-yyyy");
date2 = your_format.format(dt);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With