Programming/Kotlin

[Kotlin] ν˜„μž¬ λ‚ μ§œ/νŠΉμ • λ‚ μ§œ 및 μ‹œκ°„ κ°€μ Έμ˜€κΈ°

yuri lee 2023. 1. 12. 23:00
λ°˜μ‘ν˜•

Intro

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

 

How to do

ν˜„μž¬ λ‚ μ§œ, μ‹œκ°„

ν˜„μž¬ λ‚ μ§œμ™€ μ‹œκ°„μ€ μ•„λž˜μ˜ λ©”μ†Œλ“œλ₯Ό 톡해 κ°€μ Έμ˜¬ 수 μžˆμŠ΅λ‹ˆλ‹€. 

  • LocalDateTime.now() : ν˜„μž¬ λ‚ μ§œμ™€ μ‹œκ°„
  • LocalDate.now : ν˜„μž¬ λ‚ μ§œ 
import java.time.LocalDate
import java.time.LocalDateTime

fun main(args: Array<String>) {
    val dateTime: LocalDateTime = LocalDateTime.now()
    val date: LocalDate = LocalDate.now()
    println("Current dateTime: $dateTime") // Current dateTime: 2023-01-12T22:54:32.505572
    println("Current date: $date") // Current date: 2023-01-12
}

νŠΉμ • λ‚ μ§œ, μ‹œκ°„ 

ν˜„μž¬ λ‚ μ§œκ°€ μ•„λ‹Œ νŠΉμ • λ‚ μ§œλ₯Ό μƒμ„±ν•˜κ³  μ‹Άλ‹€λ©΄, λ‹€μŒκ³Ό 같이 of()λ₯Ό μ‚¬μš©ν•˜λ©΄ λ©λ‹ˆλ‹€. 

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

fun main(args: Array<String>) {
    val specificDate = LocalDate.of(2023, 2, 24)
    val specificDate2 = LocalDateTime.of(2023, 11, 23, 10, 10, 10) // 2019λ…„ 3μ›” 22일 10μ‹œ 10λΆ„ 10초
    println("specificDate: $specificDate") // specificDate: 2023-02-24
    println("specificDate2: $specificDate2") // specificDate2: 2023-11-23T10:10:10
}
λ°˜μ‘ν˜•