Sean
Sean

Reputation: 1123

What is wrong with my date format?

I am trying to get the date in this format "time":"05:09pm 08/02/2011". What I have so far is

Calendar c = Calendar.getInstance();
                SimpleDateFormat sdf = new SimpleDateFormat(
                        "HH:mmaa MM/dd/yyyy");

                holder.put("time", sdf.format(c.getTime()));

and this is what comes out.

                "time":"21:28PM 08\/26\/2011"

Why is this happening and what can I do to fix it? Thanks

Upvotes: 1

Views: 453

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1500385

I suspect you want a format of

hh:mmaa MM/dd/yyyy

As hh is the 12-hour format in the range 01-12. I find that's typically how humans represent 12-hour values. Of course if you want 00-11, use KK instead as suggested by MByD.

(Quite what the rationale is for the capitalized form being the 24-hour version of HH, but the lower case form being the 24-hour version of kk, I don't know...)

If you were worried by the backslashes in the output, I suspect that's just JSON-escaping. I'm surprised it's necessary for forward-slashes, but it shouldn't do any harm.

Upvotes: 2

MByD
MByD

Reputation: 137312

Try changing HH:mmaa MM/dd/yyyy to KK:mmaa MM/dd/yyyy. (KK is hour in am/pm, 0-11)

Upvotes: 1

Related Questions