Data Structures & Algorithms Handson solutions cognizant (1)

Time Conversion Data Structures and Algorithm Cognizant Handson Solution

In this post we are going to cover the Data Structures & Algorithms Time Conversion handson solution asked in Cognizant.

Question of this handson is available here.

Steps to upload answer in GenC Platform:

  1. Click on this link.
  2. Insert the below code in Hacker Rank code editor.
  3. Compile & Execute the Code.
  4. Take screenshot of successful execution.
  5. And upload the screenshot in the platform.
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) {
    // Write your code here
        String hour = s.substring(0,2);
        String rest = s.substring(3,8);
        String amOrPm = s.substring(8);

        String newHour = hour;
        if(amOrPm.equals("AM") && Integer.valueOf(hour) == 12)
            newHour = "00";
                
        else if(amOrPm.equals("PM") && Integer.valueOf(hour) != 12)
            newHour = String.valueOf(12 + Integer.valueOf(hour));     
            
        return newHour + ":" + rest;
    }

}

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        String s = bufferedReader.readLine();

        String result = Result.timeConversion(s);

        bufferedWriter.write(result);
        bufferedWriter.newLine();

        bufferedReader.close();
        bufferedWriter.close();
    }
}

Similar Posts