1

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 first one?

edit

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

3
  • This is already the third similar question today: Data->String->Date?? Commented Oct 15, 2011 at 13:08
  • 2
    The question "how to create date object with this format" is nonsensical. The output you want is created by formatting a Date object using a DateFormat object (per Bozho's answer). Commented Oct 15, 2011 at 13:11
  • 1
    You must distinguish between the object itself (the date) and the correspondig formatting. The latter is in fact only a view that may vary depending on the environment (e.g. user language in a web application). Commented Oct 15, 2011 at 13:12

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.