You need two formats: one to parse, and one to format. You need to parse from String
to Date
with one DateFormat
, then format that Date
into a String
with the other format.
Currently, your single SimpleDateFormat
is half way between - you've got HH
which is 24-hour, but you've also got aa
which is for am/pm. You want HH
without the aa
for input, and hh
with the aa
for output. (It's almost never appropriate to have both HH
and aa
.)
TimeZone utc = TimeZone.getTimeZone("etc/UTC");
DateFormat inputFormat = new SimpleDateFormat("dd MMM, yyyy HH:mm",
Locale.US);
inputFormat.setTimeZone(utc);
DateFormat outputFormat = new SimpleDateFormat("dd MMM, yyyy hh:mm aa",
Locale.US);
outputFormat.setTimeZone(utc);
Date date = inputFormat.parse(input);
String output = outputFormat.format(date);
Note that I'm setting the locale to US so it can always parse "Nov", and the time zone to UTC so you don't need to worry about certain times being skipped or ambiguous.