Android/Android Weekly

Date and Time Formatting in Kotlin with the DateTime Library

노루룽 2024. 3. 18. 21:00

졜근 νšŒμ‚¬ μ„œλΉ„μŠ€ 개발 쀑, μ„œλ²„μ—μ„œ λ‚΄λ €μ£ΌλŠ” DateFormat(”2024-03-11 07:18:25”)κ³Ό

λ””μžμΈμͺ½μ—μ„œ μ›ν•˜λŠ” DateFormat(”03/11/2024 07:18:25”)이 달라 λ°˜λ‚˜μ ˆμ„ μ‚½μ§ˆν•œ κ²½ν—˜μ΄ μžˆμ–΄μ„œ,,

이 주제λ₯Ό μ„ νƒν•˜κ²Œ λ˜μ—ˆμŠ΅λ‹ˆλ‹€πŸ₯Ή

 

kotlinx-datetime λΌμ΄λΈŒλŸ¬λ¦¬λŠ” “v0.6.0-RC”μ—μ„œ μ—…λ°μ΄νŠΈ λ˜μ—ˆμŠ΅λ‹ˆλ‹€.

 

기쑴의 λ‚ μ§œ 및 μ‹œκ°„μ— κ΄€ν•œ Formatting Options

예λ₯Όλ“€μ–΄ “March 1, 2024, at 9:15 AM”을 μ½”λ“œλ‘œ λ‚˜νƒ€λ‚΄κ³  μ‹Άλ‹€λ©΄,
LocalDateTime(2024, 3, 1, 9, 15, 0, 0) 으둜 μž‘μ„±ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

 

  • LocalDateTime
    • LocalDateTime.Formats.ISO2024-03-01T09:15:00
  • LocalDate
    • LocalDate.Formats.ISO2024-03-01
    • LocalDate.Formats.ISO_BASIC20240301
  • LocalTime
    • LocalTime.Formats.ISO09:15:00

kotlinx-datetime 라이브러리λ₯Ό μ“°κΈ°μœ„ν•΄μ„œλŠ”
build.gradle λ˜λŠ” gradle/libs.versions.toml 에 dependencyλ₯Ό μΆ”κ°€ν•˜λ©΄ λ©λ‹ˆλ‹€.

implementation "org.jetbrains.kotlinx:kotlinx-datetime:0.6.0-RC.2"

 

Formatting

λ‚ μ§œμ™€ μ‹œκ°„μ„ Formatting ν•˜λŠ” 방법은 2가지가 μžˆμŠ΅λ‹ˆλ‹€.

  1. Unicode Pattern
  2. Kotlin DSL

이와같은 방법을 μ‚¬μš©ν•˜λ €λ©΄ format(...) ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•΄μ•Ό ν•©λ‹ˆλ‹€.

val currentDateTime = LocalDateTime(2024, 3, 1, 9, 15, 0, 0)
val currentDate = currentDateTime.date
val currentTime = currentDateTime.time

currentDateTime.format(LocalDateTime.Format { 
    // Unicode Pattern λ˜λŠ” Kotlin DSL
})

currentDate.format(LocalDate.Format { 
    // Unicode Pattern λ˜λŠ” Kotlin DSL
})

currentTime.format(LocalTime.Format { 
    // Unicode Pattern λ˜λŠ” Kotlin DSL
})

 

Unicode Pattern

val currentDateTime = LocalDateTime(2024, 3, 1, 9, 15, 0, 0)
val currentDate = currentDateTime.date
val currentTime = currentDateTime.time

// 2024 03 01 - 09:15
currentDateTime.format(LocalDateTime.Format { byUnicodePattern("yyyy MM dd - HH:mm") })

// 2024/03/01
currentDate.format(LocalDate.Format { byUnicodePattern("yyyy/MM/dd") })

// 09:15 
currentTime.format(LocalTime.Format { byUnicodePattern("HH:mm") })

 

Kotlin DSL

val currentDateTime = LocalDateTime(2024, 3, 1, 9, 15, 0, 0)
val currentDate = currentDateTime.date
val currentTime = currentDateTime.time

// Mar 01 2024 - 09:15
currentDateTime.format(
    LocalDateTime.Format {
        date(
            LocalDate.Format {
                monthName(MonthNames.ENGLISH_ABBREVIATED)
                char(' ')
                dayOfMonth()
                chars(" ")
                year()
            }
        )
        chars(" - ")
        time(
            LocalTime.Format {
                hour(); char(':'); minute()
            }
        )
    }
)

// Mar 01, 2024
currentDate.format(LocalDate.Format {
    monthName(MonthNames.ENGLISH_ABBREVIATED)
    char(' ')
    dayOfMonth()
    chars(", ")
    year()
})

// 09:15
currentTime.format(LocalTime.Format {
    hour()
    char(':')
    minute()
})
  • hour() ν•¨μˆ˜λŠ” 0μ—μ„œ 23μ‹œκΉŒμ§€ μ‹œκ°„μ„ λ¦¬ν„΄ν•©λ‹ˆλ‹€.
  • λ§Œμ•½ 12μ‹œκ°„μ œλ‘œ μ‹œκ°„μ„ ν‘œμ‹œν•˜λ €λ©΄ amPmHour() ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•΄μ•Ό ν•©λ‹ˆλ‹€.

 

Kotlin DSL 방식을 μ‚¬μš©ν•˜λ©΄, padding λ§€κ°œλ³€μˆ˜λ‘œ year() monthNumber() dayOfMonth() hour() minute() 와 같은 ν•¨μˆ˜λ“€μ„ 전달할 수 있고, 이λ₯Ό 톡해 νŒ¨λ”© μŠ€νƒ€μΌμ„ λ³€κ²½ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

val time = LocalTime(hour = 9, minute = 8)

// "9:8"
time.format(LocalTime.Format { 
    hour(padding = Padding.NONE)
    char(':')
    minute(padding = Padding.NONE) 
})

// "09:08"
time.format(LocalTime.Format { 
    hour(padding = Padding.ZERO)
    char(':')
    minute(padding = Padding.ZERO) 
})

// " 9: 8"
time.format(LocalTime.Format { 
    hour(padding = Padding.SPACE)
    char(':')
    minute(padding = Padding.SPACE) 
})

 

Parsing

Formatting된 λ‚ μ§œμ™€ μ‹œκ°„μ€ parse ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

val formattedDateTime = "2024 03 01 - 09:15"
LocalDateTime.parse(
    input = formattedDateTime,
    format = LocalDateTime.Format { byUnicodePattern("yyyy MM dd - HH:mm") }
)

val formattedDate = "2024/03/01"
LocalDate.parse(
    input = formattedDate,
    format = LocalDate.Format { byUnicodePattern("yyyy/MM/dd") }
)

val formattedTime = "09:15"
LocalTime.parse(
    input = formattedTime,
    format = LocalTime.Formats.ISO
)

 

즉, Unicode Pattern λ˜λŠ” Kotlin DSL둜 λ‚ μ§œμ™€ μ‹œκ°„μ„ Formatting → parse ν•¨μˆ˜λ‘œ μ μ ˆν•œ 곳에 μ‚¬μš©!

 

Reference

https://alexzh.com/date-and-time-formatting-in-kotlin-with-the-datetime-library/

 

Date and Time Formatting in Kotlin with the DateTime Library

Explore the art of formatting date and time in Kotlin with the Kotlinx-datetime library. This guide introduces you to the efficient use of Unicode patterns and Kotlin DSL for formatting date and time types. Ideal for Kotlin Multiplatform projects.

alexzh.com