본문 바로가기
1 Day 1 Algorithms

[2019.01.25] Time Conversion

by 곰돌찌 2019. 1. 25.

Problem


Given a time in -hour AM/PM format, convert it to military (24-hour) time.

Note: Midnight is 12:00:00AM on a 12-hour clock, and 00:00:00 on a 24-hour clock. Noon is 12:00:00PM on a 12-hour clock, and 12:00:00 on a 24-hour clock.

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):

  • s: a string representing time in  hour format

Input Format

A single string  containing a time in -hour clock format (i.e.:  or ), where  and .

Constraints

  • All input times are valid

Output Format

Convert and print the given time in -hour format, where .

Sample Input 0

07:05:45PM

Sample Output 0

19:05:45


How I solved the problem


# AM일때와 PM일 때를 나눠서 로직이 짜여야함

# AM일 경우 12:00:00AM을 00:00:00으로 바꿀 수 있도록 체크해야함. 나머지는 그냥 그대로 나오면 됨.

# PM일 경우 12:00:00PM을 제외하고 나머지 시간은 12시간을 더해줘야함.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;
 
public class Solution {
 
    /*
     * Complete the timeConversion function below.
     */
    static String timeConversion(String s) {
        /*
         * Write your code here.
         */
 
         //AM
         if (s.indexOf("AM"!= -1) {
             s = s.substring(0, s.indexOf("AM"));
             String[] amSplit = s.split(":");
            
             //12:00AM ~ 11:59AM
             int hour = Integer.parseInt(amSplit[0]);
             
             if (hour == 12) {
                 return "00:" + amSplit[1+ ":" + amSplit[2];
             }
             
             return s;
         }
 
         //PM //12:00 PM ~ 11:59PM
         s = s.substring(0, s.indexOf("PM"));
         String[] timeSplit = s.split(":");
         //hour check
         int hour = Integer.parseInt(timeSplit[0]);
 
         if (hour != 12) {
            hour = hour + 12;
         }
         
         String militaryTime = hour + ":" + timeSplit[1+ ":" + timeSplit[2];
         return militaryTime;
    }
 
    private static final Scanner scan = new Scanner(System.in);
 
    public static void main(String[] args) throws IOException {
        BufferedWriter bw = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
 
        String s = scan.nextLine();
 
        String result = timeConversion(s);
 
        bw.write(result);
        bw.newLine();
 
        bw.close();
    }
}
 
cs


[출처 : https://www.hackerrank.com ]


'1 Day 1 Algorithms' 카테고리의 다른 글

[2019.01.29] Apple and Orange  (0) 2019.01.29
[2019.01.28] Grading Students  (0) 2019.01.28
[2019.01.24] Birthday Cake Candles  (0) 2019.01.24
[2019.01.23] Bitwise AND  (1) 2019.01.23
[2019.01.22] RegEx, Patterns, and Intro to Databases  (0) 2019.01.22

댓글