Given a time in 12-hour AM/PM format, convert it to military (24-hour) time
2021-09-01T07:47:09 - Vicky Chhetri
Read Time:2 Minute, 2 Second
Given a time in 12-hour AM/PM format, convert it to military (24-hour) time.
Note: – 12:00:00AM on a 12-hour clock is 00:00:00 on a 24-hour clock.
– 12:00:00PM on a 12-hour clock is 12:00:00 on a 24-hour clock.
Example
- Return ’12:01:00′.
- Return ’00:01:00′.
Function Description
Complete the timeConversion function in the editor below. It should return a new string representing the input time in 24 hour format.
timeConversion has the following parameter(s):
- string s: a time in hour format
Returns
- string: the time in hour format
Input Format
A single string that represents a time in 12-hour clock format
Constraints
- All input times are valid
Sample Input
07:05:45PM
Sample Output
19:05:45
Java solution
2021
package Timeprj;
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result {
/*
* Complete the 'timeConversion' function below.
*
* The function is expected to return a STRING.
* The function accepts STRING s as parameter.
*/
public static String timeConversion(String s) {
char[] newtime= new char[10];
String st2= new String();
String st3= new String();
int size=s.length();
for(int i=0;i<size-2;i++) {
newtime[i]=s.charAt(i);
st3+=s.charAt(i);
}
if(s.charAt(8)=='P') {
int charPos1=Integer.parseInt(String.valueOf(newtime[0]));
int charPos2=Integer.parseInt(String.valueOf(newtime[1]));
if(charPos1==0) {
charPos2 +=12;
st2=charPos2+st3.substring(2, 8);
}
if(charPos1==1 & charPos2!=2) {
charPos2=Integer.parseInt(st3.substring(0, 2));
charPos2 +=12;
st2=charPos2+st3.substring(2, 8);
}
if( (charPos1==1) & (charPos2==2) ) {
st2 =st3;
}
}
else {
int charPos1=Integer.parseInt(String.valueOf(newtime[0]));
int charPos2=Integer.parseInt(String.valueOf(newtime[1]));
if(charPos1==0) {
st2 =st3;
}
if( (charPos1==1) & (charPos2==2) ) {
st2="00"+st3.substring(2, 8);
}
}
return st2;
}
}
public class Solution {
public static void main(String[] args) {
try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(System.out));
String s = bufferedReader.readLine();
String result = Result.timeConversion(s);
bufferedWriter.write(result);
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}catch(Exception e) {
e.printStackTrace();
}
}
}