Ask Question
3 August, 19:05

Design a program that will receive a valid time in the 24-hour format (e. g. 2305) and convert to its equivalent 12-hour format (e. g. 11:05 PM). Assume 0000 in 24-hour format is 12:00AM in 12-hour format. The program should continue accepting inputs until a sentinel value of 9999 is entered. Hint: Use System. out. printf statements with "d" (Links to an external site.) to match output format. Ex: If the input is: g

+3
Answers (1)
  1. 3 August, 21:37
    0
    import java. io.*;

    public class Main

    {

    public static void main (String[] args) throws IOException {

    BufferedReader bufferObject=new BufferedReader (new InputStreamReader (System. in));

    String stringObject=bufferObject. readLine ();

    while (! stringObject. equals ("99:99AM")) {

    System. out. println (convertedTime (stringObject));

    stringObject=bufferObject. readLine ();

    }

    }

    public static String convertedTime (String stringObject) {

    String s=stringObject. substring (stringObject. length () - 2);

    String[] timeObject=stringObject. substring (0,5). split (":");

    if (s. equals ("AM")) {

    if (timeObject[0]. equals ("12")) return "00"+timeObject[1];

    else return timeObject[0]+timeObject[1];

    }

    else{

    if (timeObject[0]. equals ("12")) return "12"+timeObject[1];

    else{

    int hours=Integer. valueOf (timeObject[0]);

    timeObject[0]=String. valueOf (12+hours);

    return timeObject[0]+timeObject[1];

    }

    }

    }

    }

    Explanation:

    Inside the main method run a while loop until stringObject is not equal to the string "99:99AM". Call the convertedTime method and display the results. Use the same hours and minutes except for 12th hour If the time is in AM. Use "00" instead of 12, if it is 12th hour. Add hours to 12, if the time is in PM and don't change anything in case of 12.
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Design a program that will receive a valid time in the 24-hour format (e. g. 2305) and convert to its equivalent 12-hour format (e. g. ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers