Date and Time Formatting in Kotlin with the DateTime Library
μ΅κ·Ό νμ¬ μλΉμ€ κ°λ° μ€, μλ²μμ λ΄λ €μ£Όλ 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.ISO
→2024-03-01T09:15:00
- LocalDate
LocalDate.Formats.ISO
→2024-03-01
LocalDate.Formats.ISO_BASIC
→20240301
- LocalTime
LocalTime.Formats.ISO
→09: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κ°μ§κ° μμ΅λλ€.
- Unicode Pattern
- 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/