MapleStory Finger Point

๐ŸŸค JAVA/๐ŸŸค JAVA ๊ฐœ๋…์ •๋ฆฌ

[JAVA 8+] java.time ํŒจํ‚ค์ง€

HYEJU01 2024. 12. 9. 22:11

https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html

 

java.time (Java Platform SE 8 )

Class Summary  Class Description Clock A clock providing access to the current instant, date and time using a time-zone. Duration A time-based amount of time, such as '34.5 seconds'. Instant An instantaneous point on the time-line. LocalDate A date withou

docs.oracle.com

 

 java.time ํŒจํ‚ค์ง€

 

๊ธฐ์กด `java.util.Date`์™€ `java.util.Calendar` api ๋‹จ์ ์„ ๋ณด์™„ํ•œ๋‹ค.

์ง๊ด€์ ์ด๊ณ  ํŽธ๋ฆฌํ•œ ๊ธฐ๋Šฅ ์ œ๊ณตํ•จ

 

( 1.Date ๊ฐ์ฒด๋Š” ๋ฐ€๋ฆฌ์ดˆ ํ˜•ํƒœ๋กœ ๊ฐ’์„ ์ €์žฅํ•ด์„œ ์‚ฌ๋žŒ์ด ์ฝ๊ธฐ ์–ด๋ ค์›€

2. ์›” ๊ฐ’์ด 0๋ถ€ํ„ฐ ์‹œ์ž‘ (0~11) ๋น„์ง๊ด€์  ์„ค๊ณ„

3.ํƒ€์ž„์กด ์„ค์ •์ด ๋ณ„๋„๋กœ ํ•„์š”ํ•จ ..๋“ฑ๋“ฑ)

 


 

์„ค๊ณ„ /๊ตฌ์กฐ ์žฅ์ 

 

 

๋ถˆ๋ณ€์„ฑ

java.time ํด๋ž˜์Šค๋Š” ๋Œ€๋ถ€๋ถ„ ๋ถˆ๋ณ€(immutable)
์Šค๋ ˆ๋“œ ์•ˆ์ „์„ฑ

๋ถˆ๋ณ€์„ฑ์„ ๊ฐ€์ง€๋ฏ€๋กœ ์Šค๋ ˆ๋“œ ์•ˆ์ „์„ฑ์ด ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ณด์žฅ
api ์ง๊ด€์„ฑ

๋” ์ฝ๊ธฐ ์‰ฝ๊ณ  ์ง๊ด€์ ์ธ API๋ฅผ ์ œ๊ณต
๋‚ ์งœํ‘œํ˜„

LocalDate, LocalTime, LocalDateTime์œผ๋กœ ๊ตฌ๋ถ„
ํƒ€์ž„์กด ๊ด€๋ฆฌ 

ZonedDateTime, OffsetDateTime์œผ๋กœ ์‰ฝ๊ฒŒ ๊ด€๋ฆฌ
์‹œ๊ฐ„ ์—ฐ์‚ฐ

๋ฉ”์„œ๋“œ ์ฒด์ด๋‹์œผ๋กœ ์‰ฝ๊ฒŒ ์—ฐ์‚ฐ ๊ฐ€๋Šฅ (plusDays, minusHours ๋“ฑ).
ํฌ๋งทํŒ…/ํŒŒ์‹ฑ DateTimeFormatter ํด๋ž˜์Šค ์‚ฌ์šฉ (์Šค๋ ˆ๋“œ ์•ˆ์ „).

 

 

* ํƒ€์ž„์กด

์ง€๊ตฌ์˜ ์„œ๋กœ ๋‹ค๋ฅธ ์ง€์—ญ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ํ‘œ์ค€ ์‹œ๊ฐ„

์ง€์—ญ๋งˆ๋‹ค ํ‘œ์ค€์‹œ๊ฐ„์„ ์„ค์ •ํ•œ๋‹ค.

์ง€๊ตฌ๋ฅผ 24๊ฐœ์˜ ์‹œ๊ฐ„๋Œ€๋กœ ๋‚˜๋ˆ„๋ฉฐ, ๊ฐ ์‹œ๊ฐ„๋Œ€๋Š” UTC(ํ˜‘์ • ์„ธ๊ณ„์‹œ)๋ฅผ ๊ธฐ์ค€์œผ๋กœ ±X์‹œ๊ฐ„์œผ๋กœ ํ‘œ์‹œ๋œ๋‹ค.

ํ•œ๊ตญ์˜ ๊ฒฝ์šฐ KST (Korea Standard Time), ์ฆ‰ UTC+9๊ฐ€ ํ‘œ์ค€ ์‹œ๊ฐ„์ž„

 

* ๋ถˆ๋ณ€์„ฑ

๊ฐ์ฒด์˜ ์ƒํƒœ๊ฐ€ ํ•œ ๋ฒˆ ์ƒ์„ฑ ๋œ ์ดํ›„ ๋ณ€๊ฒฝ๋˜์ง€ ์•Š๋Š” ํŠน์„ฑ

 

* ๋ฉ”์„œ๋“œ ์ฒด์ด๋‹

๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ์„ ์—ฐ์†์ ์œผ๋กœ ์—ฐ๊ฒฐํ•˜์—ฌ ์—ฌ๋Ÿฌ ๋ฉ”์„œ๋“œ๋ฅผ ํ•œ ๋ฒˆ์— ํ˜ธ์ถœํ•˜๋Š” ๊ธฐ๋ฒ•

 

 


 

java.time ํด๋ž˜์Šค


* ์‹œ๊ฐ„๋Œ€๋ฅผ ์ €์žฅํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์˜๋ฏธ :  ์‹œ๊ฐ„(์‹œ, ๋ถ„, ์ดˆ)๋งŒ์„ ์ €์žฅ, ์‹œ๊ฐ„๋Œ€(์˜ˆ: UTC, GMT, Asia/Seoul ๋“ฑ) ์ •๋ณด๋Š” ํฌํ•จ๋˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๋œป

  • Clock  ํ˜„์žฌ ์‹œ๊ฐ„(Instant), ๋‚ ์งœ ๋ฐ ์‹œ๊ฐ„์„ ํŠน์ • ํƒ€์ž„์กด์—์„œ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐ ์‚ฌ์šฉ
  • Duration   ์‹œ๊ฐ„ ๊ธฐ๋ฐ˜ ์‹œ๊ฐ„ ๊ฐ„๊ฒฉ ` '34.5์ดˆ' `
  • Instant    UTC ์‹œ๊ฐ„์„ ์„ ๊ธฐ์ค€์œผ๋กœ ํ•œ ํŠน์ • ์‹œ์   ` 1970-01-01T00:00:00Z `
  • LocalDate : ์‹œ๊ฐ„๋Œ€๋ฅผ ํฌํ•จํ•˜์ง€ ์•Š๋Š” ๋‚ ์งœ๋ฅผ ๋‚˜ํƒ€๋ƒ„. `2007-12-03`
LocalDate.now(): ํ˜„์žฌ ๋‚ ์งœ.
LocalDate.of(2024, 12, 25): ํŠน์ • ๋‚ ์งœ ์ƒ์„ฑ.
  • LocalDateTime :  ์‹œ๊ฐ„๋Œ€๋ฅผ ํฌํ•จํ•˜์ง€ ์•Š๋Š” ๋‚ ์งœ์™€ ์‹œ๊ฐ„. ` 2007-12-03T10:15:30`
LocalTime.now(): ํ˜„์žฌ ์‹œ๊ฐ„.
localTime.minusHours(3): 3์‹œ๊ฐ„ ๊ฐ์†Œ.

 

  • LocalTime   ์‹œ๊ฐ„๋Œ€๋ฅผ ํฌํ•จํ•˜์ง€ ์•Š๋Š” ์‹œ๊ฐ„. `10:15:30`
LocalTime.now(): ํ˜„์žฌ ์‹œ๊ฐ„.
localTime.minusHours(3): 3์‹œ๊ฐ„ ๊ฐ์†Œ.

 

 

  • MonthDay   ์—ฐ๋„ ์—†์ด ์›”-์ผ๋งŒ ๋‚˜ํƒ€๋ƒ„ `--12-03`
  • OffsetDateTime   UTC ์˜คํ”„์…‹์ด ์žˆ๋Š” ๋‚ ์งœ-์‹œ๊ฐ„.
  • OffsetTime   UTC ์˜คํ”„์…‹์ด ์žˆ๋Š” ์‹œ๊ฐ„.
  • Period   ๋‚ ์งœ ๊ธฐ๋ฐ˜์˜ ์‹œ๊ฐ„ ๊ฐ„๊ฒฉ (์—ฐ๋„, ์›”, ์ผ ๋‹จ์œ„)  ` '2๋…„, 3๊ฐœ์›”, 4์ผ' `
  • Year   ์—ฐ๋„๋งŒ ํ‘œํ˜„ `2024`
  • YearMonth   ์—ฐ๋„์™€ ์›” `2024-12`
  • ZonedDateTime   ์‹œ๊ฐ„๋Œ€๋ฅผ ํฌํ•จํ•˜๋Š” ๋‚ ์งœ-์‹œ๊ฐ„ `2007-12-03T10:15:30+01:00[Europe/Paris]`
  • ZoneId   ์‹œ๊ฐ„๋Œ€ ID  `Europe/Paris`
  • ZoneOffset   UTC ์‹œ๊ฐ„์œผ๋กœ๋ถ€ํ„ฐ์˜ ์˜คํ”„์…‹ `+02:00`