본문 바로가기
Programming/>> Algorithm

[Lucky Algorithm] Time Conversion

by 니키ᕕ( ᐛ )ᕗ 2017. 10. 4.

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

Note: Midnight is  on a -hour clock, and  on a -hour clock. Noon is  on a -hour clock, and  on a -hour clock.

Input Format

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

Output Format

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


import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    static String timeConversion(String s) {
        // Complete this function
        String result = "";
        try {
            String inDateFormat = "hh:mm:ssa";
            SimpleDateFormat inDate = new SimpleDateFormat(inDateFormat, Locale.US);
            Date sDate = inDate.parse(s);

            String outDateFormat = "HH:mm:ss";
            SimpleDateFormat outDate = new SimpleDateFormat(outDateFormat);

            result = outDate.format(sDate);
        } catch (Exception e) {
            e.printStackTrace();
        }
		 return result;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s = in.next();
        String result = timeConversion(s);
        System.out.println(result);
    }
}

P.S 인XXX pre coding test 문제랑 동일


'Programming > >> Algorithm' 카테고리의 다른 글

[Lucky Algorithm] Big Sorting  (0) 2017.10.04
[Lucky Algorithm] Super Reduced String  (0) 2017.10.04
[Lucky Algorithm] Birthday Cake Candles  (0) 2017.10.03
[Lucky Algorithm] Staircase  (0) 2017.10.03
[Lucky Algorithm] Plus Minus  (0) 2017.10.03

댓글