user3505931 user3505931 - 1 year ago 115
Java Question

Converting XX:XX AM/PM to 24 Hour Clock

I have searched google and I cannot find out how you can take a string: xx:xx AM/PM (ex. 3:30 PM) and change it so that it is now in 24 hours.

So for example the previous time would be "15:30". I have looked into simply using if then statements to manipulate the string, however it seems very tedious. Is there any easy way to do this?

Input: 3:30 PM
Expected Output: 15:30

Answer Source


  String time = "3:30 PM";

    SimpleDateFormat date12Format = new SimpleDateFormat("hh:mm a");

    SimpleDateFormat date24Format = new SimpleDateFormat("HH:mm");



Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download