0

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

1
  • Just use another SimpleDateFormat and see @UwePlonus' answer. Also, beware in case this matters to you: SimpleDateFormat is not thread safe. Commented Jun 6, 2013 at 10:53

3 Answers 3

4

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
Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.