자바의 currentTimeMillis() 를 활용하면, 코틀린에서도 현재 시간을 출력할 수 있다.
val currentTime : Long = System.currentTimeMillis() // ms로 반환 println(currentTime)
하지만 ms값으로 반환하기 때문에 날짜나 시간을 직관적으로 출력하고 싶다면 포맷을 변경해주어야 한다.
fun main(){ val currentTime : Long = System.currentTimeMillis() // ms로 반환 println(currentTime) val dataFormat1 = SimpleDateFormat("yyyy-MM-dd") // 년 월 일 val dataFormat2 = SimpleDateFormat("yy-MM-dd-E") // 년(20XX) 월 일 요일 val dataFormat3 = SimpleDateFormat("hh:mm:ss") // 시(1~12) 분 초 val dataFormat4 = SimpleDateFormat("HH:mm:ss") // 시(0~23) 분 초 val dataFormat5 = SimpleDateFormat("현재시각은 \nyyyy-MM-dd \nhh:mm:ss") println(dataFormat1.format(currentTime)) println(dataFormat2.format(currentTime)) println(dataFormat3.format(currentTime)) println(dataFormat4.format(currentTime)) println(dataFormat5.format(currentTime)) }
포맷들은 자바 문서에 잘 정리되어 있다.
docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html
SimpleDateFormat (Java Platform SE 8 )
Parses text from a string to produce a Date. The method attempts to parse text starting at the index given by pos. If parsing succeeds, then the index of pos is updated to the index after the last character used (parsing does not necessarily use all charac
docs.oracle.com
* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.