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

 

'Android > Android Weekly' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Downloadable and Preloaded Fonts on Android  (0) 2024.04.23