Accoridng to SimpleDateFormat
,
if i will use hh
for hour, then i will get time in AM/PM, so I am trying this,
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd yyyy hh:mm:ss Z");
System.out.println(sdf.parse("Jan 30 2014 09:07:32 GMT").toString());
but the output of above parsed date is still in 24 hours format instead of 12 hours AM/PM.
if I am using hh
instead of HH
why my output is incorrect ?
I have also tried KK
already but didn't work either.
but the output of above parsed date is still in 24 hours format instead of 12 hours AM/PM.
You've parsed the value using hh
, but the output is just what you get from calling Date.toString()
:
Converts this Date object to a String of the form:
dow mon dd hh:mm:ss zzz yyyy
where:
... hh is the hour of the day (00 through 23), as two decimal digits.
The fact that you parsed using a 12-hour time of day is irrelevant to the output of Date.toString()
. A Date
doesn't have any clue about format, or time zone, or calendar - it's just an instant in time. (Internally, a number of milliseconds since the Unix epoch.)
If you need to convert a Date
to a String
in a particular format, you should use DateFormat.format
to do so.
I would also strongly discourage you from using a parsing format which uses hh
but without an AM/PM designator - do you really only want to be able to represent values which occur in the morning?
Are you really sure that your input data is in 12-hour format? It seems unlikely to me - in particular, when the hour is zero-padded, that usually indicates that it's in a 24-hour format. The lack of an AM/PM specifier suggests that again.
See more on this question at Stackoverflow