0
String str2="April 30, 2013";      
simpleDateFormat s= new simpleDateFormat();      
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");    
SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd");   
Date dateObject = null;  

I have string like "April 30,2013", I am getting this as string from form. I want to convert this into date object as 04/30/2013. Is there any way to do it?

I tried with simpleDateFormat() but I didn't found proper output.

halfer
  • 19,824
  • 17
  • 99
  • 186
user2032464
  • 27
  • 1
  • 6
  • Why is dateObject = null – 06needhamt Jun 11 '13 at 17:24
  • Possible duplicate of [Parse String to Date with Different Format in Java](http://stackoverflow.com/questions/882420/parse-string-to-date-with-different-format-in-java) and also: http://stackoverflow.com/questions/4216745/java-string-to-date-conversion – Tom Apr 24 '17 at 15:47

3 Answers3

3

Use MMMMM dd, yyyy to parse and MM/dd/yyyy to format

jmj
  • 237,923
  • 42
  • 401
  • 438
3

Use the following code.

String str2="April 30, 2013";      
SimpleDateFormat input = new SimpleDateFormat("MMMM dd, yyyy");    
SimpleDateFormat output = new SimpleDateFormat("MM/dd/yyyy");   
Date date = input.parse(str2);
System.out.println(output.format(date));
Pradeep Pati
  • 5,779
  • 3
  • 29
  • 43
0

This is how you can convert it String date to Date object:

    SimpleDateFormat dateFormat = new SimpleDateFormat("MMMM d,yyyy");
    SimpleDateFormat yourNewDateFormat = new SimpleDateFormat("MM/dd/yyyy"); 
    Date date = dateFormat.parse("April 30,2013");
    System.out.println(yourNewDateFormat.format(date));
grepit
  • 21,260
  • 6
  • 105
  • 81
  • Doesn't answer the question, very broad answer. – Pradeep Pati Jun 11 '13 at 17:35
  • please add comment it you are going to mark it down , so I can better help – grepit Jun 11 '13 at 17:36
  • @PradeepPati I have modified it please take a look. I hope my changes/answer is specific now. – grepit Jun 11 '13 at 17:42
  • 1
    @PradeepPati take a look now ? any better ? – grepit Jun 11 '13 at 17:50
  • this will give you again string, i want the final output as Date object in the specified format – user2032464 Jun 11 '13 at 17:58
  • @user2032464 the "date" is a date object in above code...the yourNewDateFormat is just using to format to string in a format you asked for. So, you can just use the "date" object if you only need it as a date object some where in your code – grepit Jun 11 '13 at 18:03
  • but here the problem is i am getting date object with value Fri Mar 08 00:00:00 EST 2013 .i have to compare this value with other date which is in the following format 2013-03-08 02:58:27.197. how i ll compare these dates..? – user2032464 Jun 11 '13 at 18:41