Question Detail

How to convert 24 hr format time in to 12 hr Format?

6 years ago Views 1733 Visit Post Reply

Do I want to convert the given CDT formatted 24 hr string into CDT formatted 12 hr, How to convert a 24 hr format string into 12 hr format string?


Thread Reply

Anonymous

- 6 years ago

you can try using a SimpleDateFormat object to convert the time formats.

final String time = "22:15:12";

try {
    final SimpleDateFormat dateformatObj = new SimpleDateFormat("H:mm");
    final Date dateObj = dateformatObj.parse(time);
    System.out.println(dateObj);
    System.out.println(new SimpleDateFormat("K:mm:ss").format(dateObj));
} catch (final ParseException e) {
    e.printStackTrace();
}