알고리즘 단련장/해커랭크

[해커랭크] Time Conversion

snapcoder 2025. 8. 6. 23:10
728x90
반응형
SMALL

[해커랭크] Time Conversion

 

Given a time in -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  function with the following parameter(s):

  • : a time in  hour format

Returns

  • : the time in  hour format

Input Format

A single string  that represents a time in -hour clock format (i.e.:  or ).

Constraints

  • All input times are valid

Sample Input 0

07:05:45PM

Sample Output 0

19:05:45

 

 

 

 

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
   
        int index = s.indexOf("M")-1;
        int hh = Integer.parseInt(s.split(":")[0]);
       
        if(s.charAt(index) =='P'){
            if(hh<12) hh += 12;
        }
        else{
            if(hh>=12) hh -= 12;
        }
       
        String converthh = String.format("%02d", hh);
        String dap = String.valueOf(converthh).concat(s.substring(2, 8));
        return dap;
    }

}

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();
    }
}

 

 

 

 

728x90
반응형
LIST