iCodeLikeImDrunk
iCodeLikeImDrunk

Reputation: 17866

Why is my date not displaying properly?

I used SimpleDateFormat:

SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
value.adStartDate = df.parse("2011/11/11 11:11:11");

I was hoping the date would come out like the string I provided, but instead I am getting this:

Fri Nov 11 2011 11:11:11 GMT-0500 (Eastern Standard Time)

This is showing on a form that I created using Javascript...

Is there a way to "force" the output on the form to be like the string?

Basically I want to pass a date with format "yyyy/MM/dd hh:mm:ss" to a form that was generated using Javascript and have the form display it in that same format.

Upvotes: 0

Views: 498

Answers (2)

Alfredo Osorio
Alfredo Osorio

Reputation: 11475

One thing is parsing and another thing is formatting.

Check this example in order to display the formatted string.

@Test
    public void testDateFormat() throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
        Date myDate = df.parse("2011/11/11 11:11:11");
        System.out.println(df.format(myDate));
    }

Output:

2011/11/11 11:11:11

Upvotes: 2

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340973

What do you want to achieve? You have successfully parsed "2011/11/11 11:11:11" String into java.util.Date object. Date.toString() yields the string you see (Fri Nov 11 2011 11:11:11 GMT-0500 (Eastern Standard Time)).

If you now want to format the Date object back to String, use df.format() which does the opposite thing compared to df.parse().

Upvotes: 2

Related Questions