๋๋ฃ ๋ถ๊ป์ ๋ฆฌํฌํ
์ ํด์ฃผ์
จ๋๋ฐ
ํ๋กํ ์์ ์์ ๋ ์ง๊ฐ ๊ฐํ์ ์ผ๋ก
๋ค๋ฅด๊ฒ ์
ํ
๋์ด ์๋ค๋ ์ ๋ณด์์ต๋๋ค.
์ํฉ์ ๋ค์ด๋ณด๋ ์๊ฐ์ ๋ฐ๋ผ์ ๋ฐ์ํ๋ ๊ฒ ๊ฐ์
์์ฌ๊ฐ๋ ์ํฉ์ผ๋ก ์ฌํ์ ํด๋ณด๋ ์ฑ๊ณต ํ์ต๋๋ค.
2024๋ 1์ 1์ผ ์์ผ > ํ๋กํ ์์ > ๐ 2023๋ 12์ 31์ผ?!
์๋ฒ๋ ISO8601 ํ์์ผ๋ก ๋ ์ง๋ฅผ ๋ด๋ ค์ฃผ๊ณ
์ฑ์ Material3 > DatePickerDialog ๋ก ๊ตฌํ ํ์ต๋๋ค.
DatePickerDialog ๊ตฌํ ์
์ปดํฌ์ฆ๋ต๊ฒ ๋ฐ์ดํฐ๋ ์ํ๋ก ๊ด๋ฆฌํ๋๋ฐ์.
rememberDatePickerState๋ก
์ฐ๋ ๋ ์ง๋ฅผ ์ค์ ํ ์ ์์ต๋๋ค.
ํ๋กํ ์์ ์ > ๊ธฐ์กด์ ๋ฐ์ดํฐ๊ฐ ์กด์ฌํ๋ฏ๋ก
initialSelectedDateMillis: Long ์
๋ฐ์ดํฐ๋ฅผ ๋ณด๊ณ Calandar๋ก ํ์ฑ ํ
time์ ๋๊ฒผ์์ต๋๋ค.
์์ ์ํฉ์ ์ดํดํ๊ธฐ ์ํด์
rememberDatePickerState๋ฅผ ์ดํดํ์ด์ผ ํ์ต๋๋ค.
(์ ํํ๋ initialSelectedDateMillis ํ๋ผ๋ฏธํฐ)
์๋๋ ํด๋น ํจ์์ ์ฃผ์์
๋๋ค.
ํด๋น ์ฃผ์์ ๋ณด๊ณ timestamp, UTC, epoch์ ๋ํด
์์ํด์ ์ฝ๊ฒ ์ดํดํ ์ ์์์ต๋๋ค.
๊ทธ๋์ ๋ ์ง์ ์๊ฐ์ ๋ํด์
์ดํดํ ๋ด์ฉ์ ์ ๋ฆฌํ๊ณ ์ ํฉ๋๋ค.
๐ UTC ํ๊ธฐ ๋ฐฉ๋ฒ
2024-03-30T09:00Z ๋๋ 20240330T0900Z
- UTC ์๊ฐ๋์์์ 2024๋ 3์ 30์ผ ์ค์ 9์
- Z๋(Zulu) UTC ์๊ฐ์ ๋ํ๋ด๋ ์ถ์ฝ๋ฌธ์์ด
- Z ๋ถ์ด ์์ผ๋ฉด ํด๋น ์๊ฐ์ UTC ๊ธฐ์ค
- T๋ ISO-8601 ํ์์์ ์๊ฐ์ฌ์ด์ ๊ตฌ๋ถ์(Delimiter for the time)
์ฐ์ ํฐ ํ์์ ๋ค์๊ณผ ๊ฐ์ด ๋๋๊ณ ์ ํฉ๋๋ค.
๋งํฌํ ๋ฌธ์์ ๋งํฌ๋ก ๊ฐ์๋ฉด
๋ค์๊ณผ ๊ฐ์ ๊ณตํต์ ๊ณผ ์ฐจ์ด์ ์ด ์กด์ฌํฉ๋๋ค.
๊ณตํต์
์ฐจ์ด์
์ฌ๊ธฐ์ ์๊ฐ์ ๋ํด ์ดํด๊ฐ ์กฐ๊ธ ๋ ํ์ ํฉ๋๋ค.
ํ์ฌ ํ๊ตญ์ ์๊ฐ๊ณผ ํ๋ฆฌ์ ์๊ฐ์ด ๋์ผํ๊ฐ์?
๊ทธ๋ ์ง ์์ต๋๋ค.
ํ๊ตญ์ UTC๋ฅผ ๊ธฐ์ค์ผ๋ก +9 ์๊ฐ(์คํ์ )์ ํ๋ฉด ํ๊ตญ ์๊ฐ์ ๋๋ค.
ํ๋ฆฌ๋ +1 ์๊ฐ(์คํ์
)์ ํ๋ฉด ๋์ง๋ง
ํด๋น ๊ตญ๊ฐ๋ Summer Time์ด ์กด์ฌ ํฉ๋๋ค.
์ด ๋๋ +2์๊ฐ์(์คํ์
) ์ ์ฉ ๋ฉ๋๋ค.
๊ทธ๋์ ํ๋ฆฌ๋ ์๊ฐ(์คํ์
)์ ๋ํ๋ ๊ฒ๋ณด๋ค > ์ง์ญID์ ๋ฐ๋ผ ์ฒ๋ฆฌ > TimeZoneId
์์ ๋ด์ฉ์ ์ ๋ฆฌํด๋ณด๋ฉด Time API ์์
๊ทธ๋ฌ๋ฉด ZoneId ์ ZoneOffset ์ ๊ฐ์ ๊ฐ๋ ์ ์๋์ง๋ง
๊ตญ๊ฐ๋ณ ์๊ฐ ์ฒ๋ฆฌ๋ฅผ ์ํด์๋
ZoneId๋ฅผ ํฌ๊ดํ๋ ๊ฐ๋ ์ผ๋ก
์ ์ฐํ๊ฒ ์ฒ๋ฆฌํ ์ ์๊ฒ ์ต๋๋ค.
ZoneId ๋ ๐๐ป TimeZones.html ์์ ๋ณผ ์ ์์ต๋๋ค.
์๊ฐ์ ๋ํ ๊ฐ๋
์ ์ด ์ ๋๋ก ์ ๋ฆฌํ๊ณ
์ด์ API๋ฅผ ์ธ์ ์ฌ์ฉํด์ผํ ์ง
์ฌ์ฉํ๋ ๋ฒ์ ์ ๋ฆฌํด๋ณด๊ฒ ์ต๋๋ค.
์ฐ์ ๋์ด์ ์ดํดํ๋ฉด ์กฐ๊ธ ๋ ์ง๊ด์ ์ ๋๋ค.
๋ถ์ฌ์ ๋ณด๋ฉด ์ด์ ์ดํด๊ฐ ๋ฉ๋๋ค.
- LocalDate: ํ์์กด ๊ฐ๋ ์ด ํ์์๋ ๋ ์ง ์ค์ ๊ด๋ จ API
- LocalTime: ํ์์กด ๊ฐ๋ ์ด ํ์์๋ ์๊ฐ ์ค์ ๊ด๋ จ API
- LocalDateTIme: ํ์์กด ๊ฐ๋ ์ด ํ์์๋ Date + Time API
- ZonedDateTime: ํ์์กด ๊ฐ๋ ์ด ํ์ํ Date + Time API
์์ API๋ค์ ์๊ฐ/๋ ์ง ์ฒ๋ฆฌ ๋ฐ ํ์ฑ์ ์ฐ๋ฆฌ๊ฐ ์ฌ์ฉํ๊ธฐ ์ฝ๊ฒ ์ ๊ณต์ ํด์ค๋๋ค.
ํฌ๊ฒ ์์ฑ, ์ฝ๊ธฐ, ์กฐ์, ๋ณ๊ฒฝ์ผ๋ก ๋ณผ ์ ์์ ๊ฒ ๊ฐ์ต๋๋ค.
๐ ์์ฑ
fun main() {
val localDateTime = LocalDateTime.now(ZoneId.systemDefault())
println("localDateTime: $localDateTime")
val localTime = LocalTime.now()
println("localTime: $localTime")
val zonedDateTime = ZonedDateTime.now()
println("zonedDateTIme: $zonedDateTime")
val myBirthDateTime = Year.of(1991).atMonth(12).atDay(2).atTime(20,0)
println("myBirthDateTime: $myBirthDateTime")
}
// result
localDateTime: 2024-04-07T18:43:17.322065
localTime: 18:43:17.326843
zonedDateTIme: 2024-04-07T18:43:17.327536+09:00[Asia/Seoul]
myBirthDateTime: 1991-12-02T20:00
๐ ์ฝ๊ธฐ
fun main() {
val localDateTime = LocalDateTime.now(ZoneId.systemDefault())
println("localDateTime > get Time: ${localDateTime.hour}")
val localTime = LocalTime.now()
println("localTime > get Minute: ${localTime.minute}")
val myBirthDate = Year.of(1991).atMonth(12).atDay(2).atTime(20,0)
val currentZonedDateTime = ZonedDateTime.now()
println("zonedDateTIme > DayOfWeek : ${currentZonedDateTime.dayOfWeek}")
println("zonedDateTime > is After myBirthDate : ${currentZonedDateTime.isAfter(myBirthDate.atZone(ZoneId.systemDefault()))}")
}
// result
localDateTime > get Time: 18
localTime > get Minute: 55
currentZonedDateTime > DayOfWeek : SUNDAY
currentZonedDateTime > is After myBirthDate : true
๐ ์กฐ์
๐ ๋ณ๊ฒฝ(ํ์ฑ)
fun main() {
val currentDateTimeUTC = "2024-03-30T20:20:20Z"
val parsedZonedDateTime = utcToZonedDateTime(currentDateTimeUTC)
val korZonedDateTime = parsedZonedDateTime.withZoneSameInstant(ZoneId.systemDefault())
println("korZonedDateTime: $korZonedDateTime")
// ์ํ๋ ํฌ๋งท์ผ๋ก ์ค์
val fullLocalDateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL)
println("korZonedDateTimeFullFormat: ${korZonedDateTime.format(fullLocalDateTimeFormatter)}")
}
private fun utcToZonedDateTime(utcTime: String): ZonedDateTime {
return ZonedDateTime.parse(utcTime)
}
// ๊ฒฐ๊ณผ
korZonedDateTime: 2024-03-31T05:20:20+09:00[Asia/Seoul]
korZonedDateTimeFullFormat: 2024๋
3์ 31์ผ ์ผ์์ผ ์ค์ 5์ 20๋ถ 20์ด ๋ํ๋ฏผ๊ตญ ํ์ค์
fun main() {
val currentDateTimeUTC = "2024-03-30T20:20:20Z"
val systemZonedDateTime = utcToSystemZonedDateTime(currentDateTimeUTC)
println("systemZonedDateTime: $systemZonedDateTime")
val formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG, FormatStyle.SHORT) // Here ๐
println("koreaStyleZonedDateTime: ${systemZonedDateTime.format(formatter)}")
}
private fun utcToSystemZonedDateTime(utcTime: String): ZonedDateTime {
val instant = Instant.parse(utcTime)
return ZonedDateTime.ofInstant(instant, ZoneId.systemDefault())
}
// ๊ฒฐ๊ณผ
systemZonedDateTime: 2024-03-31T05:20:20+09:00[Asia/Seoul]
koreaStyleZonedDateTime: 2024๋
3์ 31์ผ ์ค์ 5:20
This class models a single instantaneous point on the time-line. This might be used to record event time-stamps in the application
์์ ์ค๋ช
์ ๋ณด๋ฉด Instant ํด๋์ค๋
์ ํ๋ฆฌ์ผ์ด์
์์ ์๊ฐ์ ํ์์คํฌํ๋ก ๋ค๋ฃจ๊ธฐ ์ํด์
์ฌ์ฉ๋๋ API ๋ผ๊ณ ๋์ด์์ต๋๋ค.
์์ epoch ๋ผ๋ ๊ฐ๋ ์ด ์ฌ๊ธฐ์ ๋ฑ์ฅํ๋๋ฐ์
The epoch-seconds are measured from the standard Java epoch of 1970-01-01T00:00:00Z where instants after the epoch have positive values, and earlier instants have negative values.
์ค๋ช
์ ๋ณด๋ฉด 1970-01-01T00:00:00Z(Z๊ฐ ๋ถ์๊ฑธ๋ก ๋ด์ UTC ๊ธฐ์ค)๋ก๋ถํฐ
์ธก์ ๋๋ ๊ฒ์ epoch-seconds ๋ผ๊ณ ํฉ๋๋ค.
์ฝ๋๋ก ๋ณด๋ฉด ์๋์ ๊ฐ์ ํํ๋ก ์ถ๋ ฅ ๋ฉ๋๋ค.
fun main() {
val utcLong: Long = 1711830020
val utcTime = Instant.ofEpochSecond(utcLong)
println("utc Long($utcLong) to Instant > $utcTime")
val epoch = utcTime.epochSecond
println("Instant.epoch: $epoch")
}
// ๊ฒฐ๊ณผ
utc Long(1711830020) to Instant > 2024-03-30T20:20:20Z
Instant.epoch: 1711830020
์ฌ๊ธฐ๊น์ง ๊ฐ API์ ๋ํด์ ์ดํดํ ๊ฒ์ ๋ฐํ์ผ๋ก
์ต๋ํ ์ฝ๊ฒ ์ค๋ช
ํด ๋ณด์์ต๋๋ค.
์ปดํฌ์ฆ ํ๋ผ๋ฏธํฐ์ ์ฃผ์์ ๋ค์ ๋ณด๋ฉด ์ด์ ๋ ์ดํด๊ฐ ๊ฐ๋๋ค โญ๏ธ
// @param initialSelectedDateMillis timestamp in _UTC_ milliseconds from the epoch
timestamp > Instant ์ milliseconds epoch ๋ก ๊ฐ์ ์ ๋ฌํ๋ผ๋ ์๋ฏธ์์ต๋๋ค.
๊ทธ๋์ ํด๊ฒฐ์
1 Instant ํด๋์ค๋ก ๋ณํ ํ
2 .toEpochMilli()
์ ๋ฌํ์ฌ ํด๊ฒฐ ๋์ต๋๋ค.
DatePicker๋ก ์์ํ์ฌ
Date ์ Time ์ ๋ํด์
๊ฝค ํฐ ๋ฒ์๋ฅผ ๊ณต๋ถํด๋ณด๊ณ ์ ๋ฆฌ๊น์ง ํด๋ดค๋๋ฐ
์ค์๋ก ๋ถํฐ ๋ง์ ๊ฒ์ ๋ฐฐ์ด ๊ณ๊ธฐ๊ฐ ๋์์ต๋๋ค.
ํผ๋๋ฐฑ์ ์ธ์ ๋ ํ์์ด๋ฉฐ ๐๐ป
์ด๋ง ๋ง์น๊ฒ ์ต๋๋ค.