[SOLVED] How to convert 24 hr format time in to 12 hr Format?

Issue

In my application i want to convert the given CDT formatted 24 hr string in to CDT formatted 12 hr string, How to convert a given 24 hr format string in to 12 hr format string??

Solution

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

final String time = "23:15";

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

here is the javadoc link for SimpleDateFromat.

Answered By – Anantha Sharma

Answer Checked By – Marilyn (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *