5

Possible Duplicate:
how to parse date in java?

I am getting "Mon Aug 01 00:00:00 IST 2011" (java.lang.String) as string and i need to convert it to "01-08-2011" Date(java.Util.Date) ,how to do this

Community
  • 1
  • 1
n92
  • 7,424
  • 27
  • 93
  • 129

3 Answers3

3

use the SimpleDateFormat class

SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");

to print the date. This will print the current date in predefined formatted using SimpleDateFormat

System.out.println("Current Date " + sdf.format(new Date());

String mydate = "01-08-2011";

to convert string into date

Date parseDate = sdf.parse(mydate);
Pratik
  • 30,639
  • 18
  • 84
  • 159
  • 2
    Vinay is getting `Mon Aug 01 00:00:00 IST 2011` not `01-08-2011`, use pattern: `"EEE MMM dd HH:mm:ss zzz yyyy"` – oliholz Aug 08 '11 at 08:00
0

Please take a look at (Simple)DateFormat's parse method.

Mot
  • 28,248
  • 23
  • 84
  • 121
0

Look at the SimpleDateFormat class in the javadocs.

Constructs a SimpleDateFormat using the given pattern and the default date format symbols for the default locale

SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
System.out.println("Date " + sdf.format(new Date());
Saurabh Gokhale
  • 53,625
  • 36
  • 139
  • 164
  • I think "E M d k:m:s z y" would be the correct date format. See http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html – Friek Aug 08 '11 at 07:57