Reputation: 7646
I have a problem with String
conversion to Date
Format. Please help me. Below is my code:
String strDate = "23/05/2012"; // Here the format of date is MM/dd/yyyy
Now i want to convert the above String to Date Format like "23 May, 2012".
I am using below code but i am getting value as "Wed May 23 00:00:00 BOT 2012"
String string = "23/05/2012";
Date date = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH).parse(string);
System.out.println(date); // Wed May 23 00:00:00 BOT 2012
How can i get the value as "23 May, 2012". Please help me friends....
Upvotes: 0
Views: 352
Reputation: 1982
import java.text.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws ParseException{
String strDate = "23/02/2012";
Date date = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH).parse(strDate);
String date1 = new SimpleDateFormat("dd MMMM, yyyy").format(date);
System.out.println(date1);
}
}
Upvotes: 1
Reputation: 3012
You must render the date again.
You have the string and you parse it correctly back to a Date
object. Now, you have to render that Date
object the way you want.
You can use SimpleDateFormat
again, changing the pattern.
Your code should then look like
String string = "23/05/2012";
Date date = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH).parse(string);
String newFormat = new SimpleDateFormat("dd MMMM, yyyy").format(date);
System.out.println(newFormat); // 23 May, 2012
Upvotes: 4
Reputation: 9974
Use the method format()
from the class SimpleDateFormat
with the correct pattern.
Simple use:
SimpleDateFormat df = new SimpleDateFormat("dd MMM, yyyy");
System.out.println(df.format(date));
Upvotes: 3