X1633: Convert military time (24hr) to AM/PM

Write the method String convertTime(String time) in Java that implements the following logic: Given a time in 24hr format, often refered to as military time, return a string of time using AM/PM designation. For example, 06:00 is 6:00AM and 23:59 is 11:59PM. The input time is always 2 digits for the hour with a leading 0 if the hour is less than 10, followed by a ":" and 2 digits for the minutes.

The result should have only one digit for the hour if it is less than 10, followed by ":" followed by the minutes and finishing with either AM or PM depending if the original time was larger than 12:00.

You may consult the Java String class or the Integer class documentation to help you solve this problem.

Examples:

convertTime("06:00") -> "6:00AM"
convertTime("13:15") -> "1:15PM"

Your Answer:

Reset

Practice a different Java exercise

Feedback

Your feedback will appear here when you check your answer.