Programming/Kotlin

[Kotlin] μ½”ν‹€λ¦° ν˜„μž¬ λ‚ μ§œ DateTime μ—μ„œ μ‹œκ°„ μΆ”κ°€ν•˜λŠ” 법

yuri lee 2023. 2. 13. 08:11
λ°˜μ‘ν˜•

Intro

μ•ˆλ…•ν•˜μ„Έμš”. 이번 μ‹œκ°„μ—λŠ” μ½”ν‹€λ¦°μ—μ„œ ν˜„μž¬ λ‚ μ§œ DateTime μ—μ„œ μ‹œκ°„ μΆ”κ°€ν•˜λŠ” 법에 λŒ€ν•΄ μ•Œμ•„λ³΄κ² μŠ΅λ‹ˆλ‹€.

How to implement

μš°μ„  LocalDateTime 을 톡해 ν˜„μž¬ λ‚ μ§œ 및 μ‹œκ°„μ„ κ΅¬ν•©λ‹ˆλ‹€. κ·Έ 이후 plusHours λ©”μ†Œλ“œλ₯Ό 톡해 μ›ν•˜λŠ” κ°’λ§ŒνΌ μ‹œκ°„μ„ 더해주면 λ©λ‹ˆλ‹€.

import java.time.LocalDate
import java.time.LocalDateTime

fun main(args: Array<String>) {
    val todayDateTime: LocalDateTime = LocalDateTime.now()
    
    println("Current date and time: $todayDateTime")
    println("Add 9 Hour date and time: ${todayDateTime.plusHours(9)}")
}


κΆκΈˆν•˜μ‹  뢄듀은 kotlin playground μ—μ„œ 직접 ν•œλ²ˆ μ‹€ν–‰ν•΄λ³΄μ„Έμš”!
https://play.kotlinlang.org/

λ°˜μ‘ν˜•