티끌모아 연산

Date 포맷 변경 - 알고리즘

Dev.Kyu 2019. 4. 18. 09:47

https://www.hackerrank.com/challenges/time-conversion/problem?h_r=next-challenge&h_v=zen&isFullScreen=false

 

Time Conversion | HackerRank

Convert time from an AM/PM format to a 24 hour format.

www.hackerrank.com

 


 

Problem 

 

Given a time in 12-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

 

Result

 

fun timeConversion(s: String): String {
    
    var simpleTimeFormat1 = SimpleDateFormat("hh:mm:ssaa")
    var simpleTimeFormat2 = SimpleDateFormat("HH:mm:ss")
    return simpleTimeFormat2.format(simpleTimeFormat1.parse(s))

}

fun main(args: Array<String>) {
    val scan = Scanner(System.`in`)

    val s = scan.nextLine()

    val result = timeConversion(s)

    println(result)
}

 

 

Solve

 

SimpleDateFormat을 활용하여 DateFormat 지정

 

SimpleDateFormat.parse(String) 을 활용하여 스트링 날짜를 해당 포맷에 맞게 Date로 변환

 

SimpleDateFormat.format(Date()) 를 활용하여 Date를 포맷에 맞게 String 변환

 

ETC(형식 지정자)

 

형식지정자 설명
G 시대 지정자
y 연도(yy, yyyy)
M 월(MM, MMM or MMMMM)
d 일(d, dd)
h 12시간 형식(1-12 AM/PM, hh)
H 24시간 형식(0-23, HH)
m 분(0-59, mm)
s 초(0-59, ss)
S 밀리초(0-999, SSS)
E 요일(e.g. Monday)
D 일(1-366)
F *째주 *요일(e.g. 1st Thursday of December)
w 1년의 *번째 주(1-53)
W 한달의 *번째 주(0-5)
a AM/PM(aa)

 

 


세계 최강을 꿈꾸는 안드로이드 Front-End 개발자입니다.

 

Github 주소: github.com/rkswlrbduf

이전 블로그 주소: blog.naver.com/rkswlrbduf => 블로그 이전중...