Java Program to Convert a time in 12-hour AM/PM format , to military (24-hour) time - HackerRank

 


// Program by Akash Tripathi (@proakash256)
 
// To Convert a time in 12-hour AM/PM format ,
// to military (24-hour) time.
 
// 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.
  
import java.io.*;
import java.util.*;
class Time_Format{
    public static String timeConversion(String s) {
    String s1 = "";
    String h = "";
    h = h + s.charAt(0) + s.charAt(1);
    s1 = s1 + s.charAt(8) + s.charAt(9);
    if((s1.equals("PM") == true) && (h.equals("12") == false))
    {
        int a = Integer.parseInt(h);
        a = a + 12;
        h = String.valueOf(a);
    }
    else if((s1.equals("AM") == true) && (h.equals("12") == true))
    {
        h = "00";
    }
    s1 = "";
    s1 = s1 + h;
    s1 = s1 + s.substring(2 , 8);
    return s1;
    }
    public static void main(String[] argsthrows IOException {
        BufferedReader bufferedReader =
new BufferedReader(new InputStreamReader(System.in));
        String s = bufferedReader.readLine();
        String result = timeConversion(s);
        System.out.println(result);
        bufferedReader.close();
    }
}

Comments