JavaRush /Java 博客 /Random-ZH /Java 8 指南。第2部分。
ramhead
第 13 级

Java 8 指南。第2部分。

已在 Random-ZH 群组中发布

日期API

Java 8 在 java.time 包中包含全新的日期和时间 API。新的 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 显示特定日期,例如 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 与附加时区信息一起,可以将实例转换为即时实例。即时值可以轻松转换为继承自 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 虽然我们还没有为 Person类声明 @Hints注解,但是可以使用 getAnnotation(Hints.class)方法读取它。然而,更方便的方法是 getAnnotationsByType ,它使用 @Hint注释提供对所有注释的访问。此外,Java 8 中注释的使用扩展到两个目的: @Target({ElementType.TYPE_PARAMETER, ElementType.TYPE_USE}) @interface MyAnnotation {}

这就是全部

我的 Java 8 编程教程已完成。如果您想了解有关 JDK 8 中所有新类和 API 功能的更多信息,请阅读我的下一篇文章。这将帮助您了解 JDK 8 中的所有新类和隐藏功能,例如 Arrays.parallelSortStampedLockCompletableFuture
评论
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION