开发者

How to convert java.lang.string To java.util.date in java [duplicate]

This question already has answers here: 开发者_StackOverflow Closed 10 years ago.

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


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


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜