API ืฉื ืชืืจืื
Java 8 ืืืื ืืืฉืงื API ืืืฉืื ืืืืจื ืฉื ืชืืจืื ืืฉืขื ืืืืืืช java.time. ื-Date API ืืืืฉ ืืืื ืืกืคืจืืืช
Joda-Time , ืืืื ืืื ืืื ื ืืื. ืืืืืืืืช ืืืืืช ืืืกื ืืช ืืืืงืื ืืืฉืืืื ืืืืชืจ ืฉื ื-API ืืืืฉ.
ืฉืึธืขืึนื
ืฉืขืื ื ืืชื ืืืฉื ืืชืืจืื ืืืฉืขื ืื ืืืืืื. ืฉืขืื ืื ืืืืขืื ืืืืืจื ืืื ืืืื ื ืืชื ืืืฉืชืืฉ ืืื ืืืงืื
System.currentTimeMillis() ืืื ืืืืืืจ ืืช ืืืื ืื ืืืื ืืืืคืืืช ืฉื ืืืช. ืกืื ืื ืฉื ืืืืง ืืื ืืืืฆื ืื ืขื ืืื ืืืืืงื
Instant .
ื ืืชื ืืืฉืชืืฉ ืืืืืืืืช ืืืฆืืจืช ืืืืืืงืื
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
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 ืืฆืื ืืช ืชืืจืื ืืฉืขื. ืืื ืฉืืืื ืฉื ืืชืืจืื ืืืฉืขื ืืืคืืจืืื ืืขืื, ืืขืืชืง ืืื. ืืืคืขื 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
ืืื ืขื ืืืืข ื ืืกืฃ ืขื ืืืืจ ืืื, ื ืืชื ืืืืืจ ืืช ืืืืคืข ืืืื ืกืื ื. ื ืืชื ืืืืืจ ืืืืืืืช ืืงืืืช ืืกืืืื ืืืฉื ืื ืฉืืืจืฉืื ื-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 {}
ืืืืฆืขืืช ืืคืฉืจืืช ืฉื ืืื, ืืืืจ Java ืืืืืจ ืืืืคื ืืจืืื ืืช ืืืขืจื @Hint. ืื ืืฉืื ืืงืจืืืช ืืืืข ืืขืจืืช ืืืืฆืขืืช ืจืคืืงืฆืื.
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
ืืืจืืช ืฉืื ืืืจืื ื ืืช ืืืขืจื
@Hints ืขืืืจ ืืืืืงื
Person , ื ืืชื ืืงืจืื ืืืชื ืืืืฆืขืืช ืืฉืืื
getAnnotation(Hints.class) . ืขื ืืืช, ืฉืืื ื ืืื ืืืชืจ ืืื
getAnnotationsByType , ืืืกืคืงืช ืืืฉื ืืื ืืืขืจืืช ืืืืฆืขืืช ืืขืจืช
@Hint . ืื ืืกืฃ, ืืฉืืืืฉ ืืืขืจืืช ื-Java 8 ืืชืจืื ืืฉืชื ืืืจืืช:
@Target({ElementType.TYPE_PARAMETER, ElementType.TYPE_USE}) @interface MyAnnotation {}
ืื ืืื
ืืืืจืื ืฉืื ืืชืื ืืช Java 8 ืืืฉืื. ืื ืืชื ืจืืฆื ืืืืื ืขืื ืขื ืื ืืืืืงืืช ืืืืฉืืช ืืชืืื ืืช ื-API ื-JDK 8, ืคืฉืื ืงืจื ืืช ืืืืืจ ืืื ืฉืื. ืื ืืขืืืจ ืื ืืืืื ืืช ืื ืืืืืงืืช ืืืืฉืืช ืืืชืืื ืืช ืื ืกืชืจืืช ื-JDK 8, ืืืื
Arrays.parallelSort ,
StampedLock ื-
ComletableFuture .
GO TO FULL VERSION