Question

In: Computer Science

Design a program that will receive a valid time in the 12-hour format (e.g. 11:05 PM)...

Design a program that will receive a valid time in the 12-hour format (e.g. 11:05 PM) and convert it to its equivalent 24-hour format (e.g. 2305). Assume 0000 in 24-hour format is 12:00AM in 12-hour format. The program should continue accepting inputs until a sentinel value of 99:99AM is entered.

Ex: If the input is:

09:04AM 10:45AM 11:23AM 99:99AM

IN Java please, Thanks

the output is:

0904 1045 1123

Solutions

Expert Solution

/*

I wrote as simple as I can reach me out if u need any changes

*/

//Convert 12-hour time to 24-hour time format

import java.io.*;
public class Main
{
   public static void main(String[] args) throws IOException {
       BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
       String str=br.readLine();
       while(!str.equals("99:99AM")){
       System.out.println(getConvertedTime(str));
       str=br.readLine();
       }
   }
   public static String getConvertedTime(String str){
   String s=str.substring(str.length()-2);
   String[] hoursAndMinutes=str.substring(0,5).split(":");
   if(s.equals("AM")){
   //if the time is in AM then we can use same hours and minutes except for 12th hour so we need
   //special case for that if it is 12th hour we will use "00" instead of 12 for other we can use same hours
   if(hoursAndMinutes[0].equals("12")) return "00"+hoursAndMinutes[1];
   else return hoursAndMinutes[0]+hoursAndMinutes[1];
   }
   else{
   //if the time is in PM we need to add hours to 12 unless the hour is 12 in case of 12 we need to keep it as it is
   if(hoursAndMinutes[0].equals("12")) return "12"+hoursAndMinutes[1];
   else{
   int hours=Integer.valueOf(hoursAndMinutes[0]);
   hoursAndMinutes[0]=String.valueOf(12+hours);
   return hoursAndMinutes[0]+hoursAndMinutes[1];
   }
   }
  
   }
}


Related Solutions

Q.Write a program that converts between two way of measuring time: 12-hour time (civilian time) and...
Q.Write a program that converts between two way of measuring time: 12-hour time (civilian time) and 24-hour time (military time). We will assume in the context there is no need for seconds, so time12 use only hours (from 1 to 12), minutes, and an "a.m." or "p.m." designation. Our time24 class, which uses hours (from 00 to 23), minutes and seconds. 12-Hours Time 24-Hours Time 12:00 a.m. (midnight) 00:00 12:01 a.m. 00:01 12:00 p.m. 12:00 6:00 p.m. 18:00 11:59 p.m....
In JAVA Write a program that converts a time from 24-hour notation to 12-hour notation. Assume...
In JAVA Write a program that converts a time from 24-hour notation to 12-hour notation. Assume the user will enter the time as a 4-digit number with no colon. Define an exception class called InvalidTimeFormatException. If the user enters an invalid time lime 1065 or 2515, the program should throw and handle an InvalidTimeFormatException. NOTE: Assume the user will enter the time as a 4-digit number with no colon. SAMPLE OUTPUT: Enter time in 24-hour notation: 1614 That is the...
Write a program to convert the time from 24-hour notation to 12-hour notation and vice versa....
Write a program to convert the time from 24-hour notation to 12-hour notation and vice versa. Your program must be menu driven, giving the user the choice of converting the time between the two notations. Furthermore, your program must contain at least the following functions: a function to convert the time from 24-hour notation to 12-hour notation; a function to convert the time from 12-hour notation to 24-hour notation; a function to display the choices; function(s) to get the input;...
Write a program to convert the time from 24-hour notation to 12-hour notation and vice versa....
Write a program to convert the time from 24-hour notation to 12-hour notation and vice versa. Your program must be menu driven, giving the user the choice of converting the time between the two notations. Furthermore, your program must contain at least the following functions: a function to convert the time from 24-hour notation to 12-hour notation; a function to convert the time from 12-hour notation to 24-hour notation; a function to display the choices; function(s) to get the input;...
Stark Company has five employees. Employees paid by the hour receive a $11 per hour pay...
Stark Company has five employees. Employees paid by the hour receive a $11 per hour pay rate for the regular 40-hour workweek plus one and one-half times the hourly rate for each overtime hour beyond the 40 hours per week. Hourly employees are paid every two weeks, but salaried employees are paid monthly on the last biweekly payday of each month. FICA Social Security taxes are 6.2% of the first $118,500 paid to each employee, and FICA Medicare taxes are...
Stark Company has five employees. Employees paid by the hour receive a $11 per hour pay...
Stark Company has five employees. Employees paid by the hour receive a $11 per hour pay rate for the regular 40-hour workweek plus one and one-half times the hourly rate for each overtime hour beyond the 40 hours per week. Hourly employees are paid every two weeks, but salaried employees are paid monthly on the last biweekly payday of each month. FICA Social Security taxes are 6.2% of the first $118,500 paid to each employee, and FICA Medicare taxes are...
Stark Company has five employees. Employees paid by the hour receive a $12 per hour pay...
Stark Company has five employees. Employees paid by the hour receive a $12 per hour pay rate for the regular 40-hour workweek plus one and one-half times the hourly rate for each overtime hour beyond the 40 hours per week. Hourly employees are paid every two weeks, but salaried employees are paid monthly on the last biweekly payday of each month. FICA Social Security taxes are 6.2% of the first $118,500 paid to each employee, and FICA Medicare taxes are...
Lynn delivers a baby girl, 7 lb. 12 oz. vaginally at 6 pm. After one hour...
Lynn delivers a baby girl, 7 lb. 12 oz. vaginally at 6 pm. After one hour your assessment findings reveal: pulse 98, resp. 24, B/P 95/60, temp 99.5, fundus is palpated at U-2, firm & midline; lochia is rubra, heavy and bright red. Perineum is intact without swelling, discoloration or drainage. IV of LR with 20 Units of Pitocin is infusing in the right arm @ 125 mL/hr. She has been assisted to the bathroom to void and is back...
*** In C++ Write a program that converts from 24-hour notation to 12-hour notation. For example,...
*** In C++ Write a program that converts from 24-hour notation to 12-hour notation. For example, it should convert 14:25 to 2:25 P.M. The input is given as two integers. This is what I have so far. The program runs for correct input values, but I cannot figure out how to accommodate for hours > 23 and minutes > 59, or negative values. My code is below: // Writing a program that converts from 24-hour notation to 12-hour notation.// #include...
Chapter 5 Homework Assignment (part 2) PE.05-03.ALGO PE.05-04.ALGO EX.05-04 EX.05-11 EX.05-12 EX.05-14.ALGO EX.05-15.ALGO EX.05-16.ALGO EX.05-17.ALGO EX.05-18.ALGO...
Chapter 5 Homework Assignment (part 2) PE.05-03.ALGO PE.05-04.ALGO EX.05-04 EX.05-11 EX.05-12 EX.05-14.ALGO EX.05-15.ALGO EX.05-16.ALGO EX.05-17.ALGO EX.05-18.ALGO Hide or show questions Progress:7/10 items eBook Show Me How Calculator Print Item Posting a Purchases Journal The purchases journal for Newmark Exterior Cleaners Inc. follows. The accounts payable account has a March 1, 20Y2, balance of $605 for an amount owed to Nicely Co. No payments were made on creditor invoices during March. PURCHASES JOURNAL Page 16 Date Account Credited Post. Ref. Accts.  ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT