开发者

How to create a Date with specific format in Java?

I have the following code to handle date

SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss a"); 

String s = formatter.format(formatter.parse("10/11/2011 4:24:00 PM");
System.out.prinln(s);
Date d = (Date)formatter.parseObject(s);
System.out.println(d);

this first print line output is : 10/10/2011 00:00:00 AM

and the second is : Mon Oct 10 00:00:00 GMT+02:00 2011

How I can make the second statement print out the same as the firs开发者_如何学运维t one?

edit

the question is how to create date object with this format ?


You can't. The toString() method of objects is used mainly for debugging, and not for presenting to users.

Always use a formatter. In your case: String formatted = formatter.format(d)

The Date object does not have a representation format. It only has the data. The formatter has the format.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜