Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting yyyy-mm-dd into dd mm yyyy

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)"

like image 889
user1740281 Avatar asked Jun 26 '13 15:06

user1740281


People also ask

How do you convert D mm yyyy to dd mm yyyy?

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.

How do you convert mm/dd/yyyy to dd yyyy in Excel?

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.).

How do I convert MDY to DMY?

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.


2 Answers

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);
like image 148
duffymo Avatar answered Oct 28 '22 18:10

duffymo


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);
like image 39
Priyank Joshi Avatar answered Oct 28 '22 16:10

Priyank Joshi