[kotlin] 코틀린 표준 라이브러리에서 제공하는 날짜와 시간 처리 기능

코틀린 표준 라이브러리는 프로그래머가 날짜와 시간을 쉽게 처리할 수 있는 다양한 기능을 제공합니다. 이 기능들을 사용하여 어플리케이션에서 날짜와 시간을 효율적으로 다룰 수 있습니다.

1. java.time 패키지 활용

코틀린에서는 자바의 java.time 패키지를 활용하여 강력한 날짜와 시간 처리 기능을 제공합니다. 예를 들어, LocalDate 클래스를 사용하면 날짜 정보를 저장하고 다룰 수 있습니다.

import java.time.LocalDate

fun main() {
    val today: LocalDate = LocalDate.now()
    println(today)
}

2. 날짜 연산

코틀린의 java.time 패키지를 사용하면 날짜 연산을 쉽게 수행할 수 있습니다. 예를 들어, 다음과 같이 날짜를 더하거나 뺄 수 있습니다.

import java.time.LocalDate
import java.time.Period

fun main() {
    val today: LocalDate = LocalDate.now()
    val nextWeek: LocalDate = today.plus(Period.ofWeeks(1))
    println(nextWeek)
}

3. 날짜 형식화

코틀린에서는 DateTimeFormatter를 사용하여 날짜 형식을 지정하고 원하는 형식으로 변경할 수 있습니다.

import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

fun main() {
    val currentDateTime = LocalDateTime.now()
    val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
    val formattedDateTime = currentDateTime.format(formatter)
    println(formattedDateTime)
}

이러한 기능들을 활용하여 코틀린 앱에서 날짜와 시간을 쉽게 처리할 수 있습니다.


참고 문헌: