๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๊ฐœ๋ฐœ/Spring

ZonedDateTime (UTC -> KST)

dateTime์— “z”๋ฅผ ๋ถ™์—ฌ๋‹ฌ๋ผ๋Š” ํ”„๋ก ํŠธ์˜ ์š”์ฒญ์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.
LocalDateTime : "2022-02-23T12:51:17"
ZonedDateTime : "2022-02-23T12:51:17.233Z"
์œ„์˜ ์˜ˆ์‹œ์ฒ˜๋Ÿผ ๋ฐ์ดํ„ฐ ํ˜•์‹์— “z”๋กœ DateTime์˜ zone์„ ํ‘œํ˜„ํ•ด์ค๋‹ˆ๋‹ค. (UTC๊ธฐ์ค€์ผ๋•Œ!)

 

๋ชจ๋“  LocalDateTime ๋ณ€์ˆ˜๋“ค์„ ZonedDateTime์œผ๋กœ ๋ณ€๊ฒฝํ•˜๊ณ  DB์˜ ์นผ๋Ÿผ๋„ ZonedDateTime์œผ๋กœ ๋ณ€๊ฒฝํ•˜์˜€์Šต๋‹ˆ๋‹ค.

 

ํ”„๋ก ํŠธ์™€ ๋ฐฑ ์‚ฌ์ด์˜ ํ†ต์‹ ์€ ๋ชจ๋‘ UTC ๊ธฐ์ค€์œผ๋กœ ์ด๋ฃจ์–ด์ง‘๋‹ˆ๋‹ค.
UTC์™€ KST๋Š” +9์‹œ๊ฐ„์˜ ์ฐจ์ด๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์‹œ๊ฐ„์„ ๊ธฐ์ค€์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•  ๋•Œ ์œ ์˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
๋•Œ๋ฌธ์— ์‹œ๊ฐ„ ๊ธฐ์ค€์œผ๋กœ ์กฐํšŒ๋ฅผ ํ•  ๋•Œ UTC → KST →UTC๋กœ ๊ณผ์ •์„ ๊ฑฐ์ณ ์กฐํšŒํ•˜์˜€์Šต๋‹ˆ๋‹ค.
๋ฒˆ๊ฑฐ๋กญ๊ฒŒ ๋ณด์ผ ์ˆ˜ ์žˆ์ง€๋งŒ ํ†ต์‹ ์„ UTC ๊ธฐ์ค€์œผ๋กœ ํ•  ๋•Œ ์„œ๋น„์Šค ํ™•์žฅ์„ฑ์„ ๊ฐ€์ ธ๊ฐˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

final ZonedDateTime currentTimeOfKorea = ZonedDateTime.now(ZoneId.of("Asia/Seoul"));

final ZonedDateTime todayStart = ZonedDateTime.of(
                currentTimeOfKorea.getYear(),
                currentTimeOfKorea.getMonthValue(),
                currentTimeOfKorea.getDayOfMonth(),
                0, 0, 0, 0, ZoneId.of("Asia/Seoul"))
        .withZoneSameInstant(ZoneOffset.UTC);

 

 

 

๋„์›€์ด ๋˜๋Š” ์‚ฌ์ดํŠธ

 

ZonedDateTime (Java Platform SE 8 )

static ZonedDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond, ZoneId zone) Obtains an instance of ZonedDateTime from a year, month, day, hour, minute, second, nanosecond and time-zone.

docs.oracle.com

 

 

[Java8 Time API] ZonedDateTime ์‚ฌ์šฉ๋ฒ•

Engineering Blog by Dale Seo

www.daleseo.com

 

๋ฐ˜์‘ํ˜•