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

1

3 Answers 3

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

1 Comment

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

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

Comments

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

1 Comment

I think "E M d k:m:s z y" would be the correct date format. See download.oracle.com/javase/1.4.2/docs/api/java/text/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.