๋ ์ง API
Java 8์๋ java.time ํจํค์ง์ ์์ ํ ์๋ก์ด ๋ ์ง ๋ฐ ์๊ฐ API๊ฐ ํฌํจ๋์ด ์์ต๋๋ค. ์๋ก์ด Date API๋
Joda-Time ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ์ ์ฌ ํ์ง๋ง ๋์ผํ์ง๋ ์์ต๋๋ค. ๋ค์ ์์์๋ ์ API์ ๊ฐ์ฅ ์ค์ํ ๋ถ๋ถ์ ๋ค๋ฃน๋๋ค.
์๊ณ
์๊ณ๋ฅผ ํตํด ํ์ฌ ๋ ์ง์ ์๊ฐ์ ์ก์ธ์คํ ์ ์์ต๋๋ค.
์๊ณ๋ ์๊ฐ๋๋ฅผ ์ธ์ํ๋ฏ๋ก System.currentTimeMillis() ๋์ ์ฌ์ฉํ์ฌ ํ์ฌ ์๊ฐ์ ๋ฐ๋ฆฌ์ด ๋จ์๋ก ๋ฐํํ ์ ์์ต๋๋ค.
์ด๋ฌํ ์ข
๋ฅ์ ์๊ฐ ์ ๋ฐ๋๋ Instant ํด๋์ค ๋ก๋ ํํ๋ฉ๋๋ค .
Instants๋ ์์๋
java.util.Date ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ๋ฐ ์ฌ์ฉ๋ ์ ์์ต๋๋ค .
Clock clock = Clock.systemDefaultZone(); long millis = clock.millis(); Instant instant = clock.instant(); Date legacyDate = Date.from(instant); // legacy java.util.Date
์๊ฐ๋
์๊ฐ๋๋ ์ถ์ ํด๋์ค
ZoneId ๋ก ํ์๋ฉ๋๋ค . ์ด๋ ํต๊ณ์ ํฉํ ๋ฆฌ ๋ฐฉ๋ฒ์ ์ฌ์ฉํ์ฌ ์ฝ๊ฒ ์ก์ธ์คํ ์ ์์ต๋๋ค. ์๊ฐ๋๋ ํ์ฌ ๋ ์ง์ ํ์ง ๋ ์ง ๋ฐ ์๊ฐ ๊ฐ์ ๋ณํ์ ์ค์ํ ์คํ์
์ ์ ์ํฉ๋๋ค.
System.out.println(ZoneId.getAvailableZoneIds()); // prints all available timezone ids ZoneId zone1 = ZoneId.of("Europe/Berlin"); ZoneId zone2 = ZoneId.of("Brazil/East"); System.out.println(zone1.getRules()); System.out.println(zone2.getRules()); // ZoneRules[currentStandardOffset=+01:00] // ZoneRules[currentStandardOffset=-03:00]
ํ์ง ์๊ฐ
LocalTime์ ์๊ฐ๋ ์์ด ์๊ฐ์ ํ์ํฉ๋๋ค(์: 17:30:15). ๋ค์ ์์์๋ ์์ ์ ์๋ ์๊ฐ๋์ ๋ํ ๋ ๊ฐ์ ํ์ง ์๊ฐ ๊ฐ์ฒด๋ฅผ ๋ง๋ญ๋๋ค. ๊ทธ๋ฐ ๋ค์ ๋ ๊ฐ์ฒด๋ฅผ ๋น๊ตํ๊ณ ์๊ฐ๊ณผ ๋ถ์ ์ฐจ์ด๋ฅผ ๊ณ์ฐํฉ๋๋ค.
LocalTime now1 = LocalTime.now(zone1); LocalTime now2 = LocalTime.now(zone2); System.out.println(now1.isBefore(now2)); // false long hoursBetween = ChronoUnit.HOURS.between(now1, now2); long minutesBetween = ChronoUnit.MINUTES.between(now1, now2); System.out.println(hoursBetween); // -3 System.out.println(minutesBetween); // -239
LocalTime์๋ ๋ฌธ์์ด ๊ตฌ๋ฌธ ๋ถ์์ ํฌํจํ์ฌ ์ธ์คํด์คํ๋ฅผ ๋ ์ฝ๊ฒ ๋ง๋๋ ๋ค์ํ ํฉํฐ๋ฆฌ ๋ฉ์๋๊ฐ ์ ๊ณต๋ฉ๋๋ค.
LocalTime late = LocalTime.of(23, 59, 59); System.out.println(late); // 23:59:59 DateTimeFormatter germanFormatter = DateTimeFormatter .ofLocalizedTime(FormatStyle.SHORT) .withLocale(Locale.GERMAN); LocalTime leetTime = LocalTime.parse("13:37", germanFormatter); System.out.println(leetTime); // 13:37
ํ์ง ๋ ์ง
LocalDate๋ ํน์ ๋ ์ง(์: 2014-03-11)๋ฅผ ํ์ํฉ๋๋ค. ์ธ์คํด์ค๋ ๋ณ๊ฒฝํ ์ ์์ผ๋ฉฐ LocalTime๊ณผ ์ ์ฌํ๊ฒ ์๋ํฉ๋๋ค. ์ด ์์์๋ ์ผ, ์ ๋๋ ์ฐ๋๋ฅผ ๋ํ๊ฑฐ๋ ๋นผ์ ์ ๋ ์ง๋ฅผ ๊ณ์ฐํ๋ ๋ฐฉ๋ฒ์ ๋ณด์ฌ์ค๋๋ค. ๊ฐ์ฒด์ ๋ํ ๋ชจ๋ ์์
์ ์ ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ค๋ ์ ์ ๋ช
์ฌํ์ธ์.
LocalDate today = LocalDate.now(); LocalDate tomorrow = today.plus(1, ChronoUnit.DAYS); LocalDate yesterday = tomorrow.minusDays(2); LocalDate independenceDay = LocalDate.of(2014, Month.JULY, 4); DayOfWeek dayOfWeek = independenceDay.getDayOfWeek(); System.out.println(dayOfWeek); // FRIDAY
๋ฌธ์์ด์์ LocalDate๋ฅผ ๊ตฌ๋ฌธ ๋ถ์ํ๋ ๊ฒ์ LocalTime์ ๊ตฌ๋ฌธ ๋ถ์ํ๋ ๊ฒ๋งํผ ๊ฐ๋จํฉ๋๋ค.
DateTimeFormatter germanFormatter = DateTimeFormatter .ofLocalizedDate(FormatStyle.MEDIUM) .withLocale(Locale.GERMAN); LocalDate xmas = LocalDate.parse("24.12.2014", germanFormatter); System.out.println(xmas); // 2014-12-24
ํ์ง ๋ ์ง์๊ฐ
LocalDateTime์ ๋ ์ง/์๊ฐ์ ํ์ํฉ๋๋ค. ์ด๋ ์์ ์ ๊ณต๋ ๋ ์ง์ ์๊ฐ์ ํ๋์ ์ฌ๋ณธ์ผ๋ก ์กฐํฉํ ๊ฒ์
๋๋ค. LocalDateTime ์ธ์คํด์ค๋ ๋ณ๊ฒฝํ ์ ์์ผ๋ฉฐ LocalTime ๋ฐ LocalDate์ ์ ์ฌํ๊ฒ ์๋ํฉ๋๋ค. ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ํ์ํ ์ธ์คํด์ค ์์ฑ ๊ฐ์ ๊ฒ์ํ ์ ์์ต๋๋ค.
LocalDateTime sylvester = LocalDateTime.of(2014, Month.DECEMBER, 31, 23, 59, 59); DayOfWeek dayOfWeek = sylvester.getDayOfWeek(); System.out.println(dayOfWeek); // WEDNESDAY Month month = sylvester.getMonth(); System.out.println(month); // DECEMBER long minuteOfDay = sylvester.getLong(ChronoField.MINUTE_OF_DAY); System.out.println(minuteOfDay); // 1439
์ถ๊ฐ ์๊ฐ๋ ์ ๋ณด์ ํจ๊ป ์ธ์คํด์ค๋ฅผ ์ธ์คํดํธ๋ก ๋ณํํ ์ ์์ต๋๋ค. Instants๋ java.util.Date์์ ์์๋ ์ด์ ์ ํ์ผ๋ก ์ฝ๊ฒ ๋ณํ๋ ์ ์์ต๋๋ค.
Instant instant = sylvester .atZone(ZoneId.systemDefault()) .toInstant(); Date legacyDate = Date.from(instant); System.out.println(legacyDate); // Wed Dec 31 23:59:59 CET 2014
๋ ์ง-์๊ฐ ํ์ ์ง์ ์ ๋ ์ง ๋๋ ์๊ฐ ํ์ ์ง์ ๊ณผ ๋์ผํ ๋ฐฉ์์ผ๋ก ์๋ํฉ๋๋ค. ๋ฏธ๋ฆฌ ์ ์๋ ๋ ์ง ํ์์ ์ฌ์ฉํ๋ ๋์ ์๋์ผ๋ก ์ ์๋ ํ์์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
DateTimeFormatter formatter = DateTimeFormatter .ofPattern("MMM dd, yyyy - HH:mm"); LocalDateTime parsed = LocalDateTime.parse("Nov 03, 2014 - 07:13", formatter); String string = formatter.format(parsed); System.out.println(string); // Nov 03, 2014 - 07:13
java.text.NumberFormat๊ณผ ๋ฌ๋ฆฌ ์๋ก์ด DateTimeFormatter๋ ๋ณ๊ฒฝํ ์ ์์ผ๋ฉฐ ์ค๋ ๋๋ก๋ถํฐ ์์ ํฉ๋๋ค. ํ์ ์์ฑ ๊ตฌ๋ฌธ์ ๋ํ ์์ธํ ๋ด์ฉ์
์ฌ๊ธฐ๋ฅผ ์ฐธ์กฐํ์ธ์ .
์ฃผ์
Java 8์ ์ฃผ์์ ๋ฐ๋ณต ๊ฐ๋ฅํฉ๋๋ค. ์ด๊ฒ์ด ์ด๋ป๊ฒ ์๋ํ๋์ง ์ค๋ช
ํ๋ ์๋ฅผ ์ดํด๋ณด๊ฒ ์ต๋๋ค. ์์ํ๋ ค๋ฉด ์ ํจํ ์ฃผ์ ๋ฐฐ์ด์ ์ ์ฅํ๋ ์ฃผ์ ๋ํผ๋ฅผ ์ ์ํ๊ฒ ์ต๋๋ค.
@interface Hints { Hint[] value(); } @Repeatable(Hints.class) @interface Hint { String value(); }
Java 8์์๋ @Repeatable ์ฃผ์์ ์ ์ธํ์ฌ ๋์ผํ ์ ํ์ ์ฌ๋ฌ ์ฃผ์์ ์ฌ์ฉํ ์ ์์ต๋๋ค. ์ต์
1: ์ปจํ
์ด๋ ์ฃผ์ ์ฌ์ฉ(๊ตฌ์)(Java 8์์๋
@Repeatable ์ฃผ์์ ์ ์ธํ์ฌ ๋์ผํ ์ ํ์ ์ฌ๋ฌ ์ฃผ์์ ์ฌ์ฉํ ์ ์์ต๋๋ค .) ์ต์
1: ์ฃผ์ ์ปจํ
์ด๋ ์ฌ์ฉ(๊ตฌ์)
@Hints({@Hint("hint1"), @Hint("hint2")}) class Person {}
์ต์
2: ๋ฐ๋ณต ๊ฐ๋ฅํ ์ฃผ์ ์ฌ์ฉ( ์๋ก์ด ๊ธฐ๋ฅ)
@Hint("hint1") @Hint("hint2") class Person {}
์ต์
2๋ฅผ ์ฌ์ฉํ๋ฉด Java ์ปดํ์ผ๋ฌ๊ฐ ์์์ ์ผ๋ก @Hint ์ฃผ์์ ์ค์ ํฉ๋๋ค. ์ด๋ ๋ฆฌํ๋ ์
์ ํตํด ์ฃผ์ ์ ๋ณด๋ฅผ ์ฝ๋ ๋ฐ ์ค์ํฉ๋๋ค.
Person ํด๋์ค ์ ๋ํด
@Hints
Hint hint = Person.class.getAnnotation(Hint.class); System.out.println(hint); // null Hints hints1 = Person.class.getAnnotation(Hints.class); System.out.println(hints1.value().length); // 2 Hint[] hints2 = Person.class.getAnnotationsByType(Hint.class); System.out.println(hints2.length); // 2
์ฃผ์์ ์ ์ธํ์ง ์์์ง๋ง
getAnnotation(Hints.class) ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ์ฝ์ ์ ์์ต๋๋ค . ๊ทธ๋ฌ๋ ๋ ํธ๋ฆฌํ ๋ฐฉ๋ฒ์
@Hint ์ฃผ์์ ์ฌ์ฉํ์ฌ ๋ชจ๋ ์ฃผ์์ ๋ํ ์ก์ธ์ค๋ฅผ ์ ๊ณตํ๋
getAnnotationsByType ์
๋๋ค . ๋ํ Java 8์ ์ฃผ์ ์ฌ์ฉ์ ๋ ๊ฐ์ง ๋ชฉ์ ์ผ๋ก ํ์ฅ๋ฉ๋๋ค.
@Target({ElementType.TYPE_PARAMETER, ElementType.TYPE_USE}) @interface MyAnnotation {}
์ด๊ฒ ๋ค์ผ
๋ด Java 8 ํ๋ก๊ทธ๋๋ฐ ํํ ๋ฆฌ์ผ์ด ์๋ฃ๋์์ต๋๋ค. JDK 8์ ๋ชจ๋ ์๋ก์ด ํด๋์ค์ API ๊ธฐ๋ฅ์ ๋ํด ์์ธํ ์์๋ณด๋ ค๋ฉด ๋ค์ ๊ธฐ์ฌ๋ฅผ ์ฝ์ด๋ณด์ธ์.
์ด๋ Arrays.parallelSort ,
StampedLock ๋ฐ
CompletableFuture ์ ๊ฐ์ JDK 8์ ๋ชจ๋ ์๋ก์ด ํด๋์ค์ ์จ๊ฒจ์ง ๊ธฐ๋ฅ์ ์ดํดํ๋ ๋ฐ ๋์์ด ๋ฉ๋๋ค .
GO TO FULL VERSION