一、接口中的静态方法和默认方法
二、新时间日期API
新时间日期API:
- LocalDate,LocalDateTime,LocalTime:人读的时间和日期
- Instant(时间戳):机读的时间戳
- Duration:计算两个时间之间的间隔
- Period:计算两个日期之间的间隔
OffsetTime:对时间做偏移量运算(比如加几个小时,减几个小时)
@Testpublic void test3() throws ExecutionException, InterruptedException {DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd");Callable<LocalDate> task = new Callable<LocalDate>() {@Overridepublic LocalDate call() throws Exception {return LocalDate.parse("20210717", dtf);}};ExecutorService pool = Executors.newFixedThreadPool(10);List<Future<LocalDate>> results = new ArrayList<>();for (int i = 0; i < 10; i++) {results.add(pool.submit(task));}for (Future<LocalDate> result : results) {System.out.println(result.get());}}
LocalDate,LocalTime,LocalDateTime的类的实例是不可变的对象,分别标识使用ISO-8601日期系统的提起、时间、日期和时间,他们提供了简单的日期或时间,并不包含当前的时间信息,也不包含与时区相关的信息。
1、本地时间与时间戳
public class LocalDateTimTest {/*** Duration:计算两个“时间”之间的间隔* Period:计算两个“日期”之间的间隔*/@Testpublic void test4() {Instant instant = Instant.now();try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}Instant instant1 = Instant.now();Duration duration = Duration.between(instant, instant1);System.out.println(duration); // PT1.009SSystem.out.println("-----------------");LocalTime time = LocalTime.now();try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}LocalTime time1 = LocalTime.now();long millis = Duration.between(time, time1).toMillis();System.out.println(millis); // 1006}/*** 以下是机读的——时间戳(以Unix元年——1970年1月1日00:00:00 到某个时间之间的毫秒值)*/@Testpublic void test3() {Instant now = Instant.now(); // 默认获取 UTC 时区(与中国差八个小时,比中国慢8个小时)System.out.println(now); // 2021-07-17T14:09:32.062ZOffsetDateTime offsetDateTime = now.atOffset(ZoneOffset.ofHours(8)); // 加上8个小时即为中国时间System.out.println(offsetDateTime); // 2021-07-17T22:09:32.062+08:00// 获取时间戳System.out.println(now.toEpochMilli()); // 1626531117816}/*** 以下是人读的*/@Testpublic void test() {LocalDateTime now = LocalDateTime.now(); // now() : 获取当前日期时间System.out.println(now); // 2021-07-17T21:55:51.507LocalDate now1 = LocalDate.now();System.out.println(now1); // 2021-07-17LocalTime now2 = LocalTime.now();System.out.println(now2); // 21:55:51.508}@Testpublic void test2() {LocalDateTime localDateTime = LocalDateTime.of(2020, 6, 6, 13, 14, 30, 345); // 指定日期时间System.out.println(localDateTime); // 2020-06-06T13:14:30.000000345LocalDate localDate = LocalDate.of(2019, 06, 4);System.out.println(localDate); // 2019-06-04}}
2、时间校正器
TemporalAdjuster:时间校正器。有时我们可能需要获取例如:将日期调整到“下个周日”等操作。
TemporalAdjusters:该类通过静态方法提供了大量的常用TemporalAdjuster的实现。
@Testpublic void test5() {LocalDateTime localDateTime = LocalDateTime.now();System.out.println(localDateTime); // 2021-07-18T08:46:10.854LocalDateTime localDateTime1 = localDateTime.withDayOfMonth(10);System.out.println(localDateTime1); // 2021-07-10T08:46:10.854LocalDateTime dateTime = localDateTime.with(TemporalAdjusters.next(DayOfWeek.FRIDAY));System.out.println(dateTime); // 2021-07-23T08:48:01.609}
3、时间格式化与时区的处理
/*** DateTimeFormatter:格式化时间/日期*/@Testpublic void test6() {DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_DATE;LocalDateTime localDateTime = LocalDateTime.now();System.out.println(localDateTime); // 2021-07-18T08:54:41.321// 根据自定义格式来格式化日期String format = localDateTime.format(dateTimeFormatter);System.out.println(format); // 2021-07-18}
/*** DateTimeFormatter:格式化时间/日期*/@Testpublic void test6() {DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_DATE;LocalDateTime localDateTime = LocalDateTime.now();System.out.println(localDateTime); // 2021-07-18T08:59:28.196// 根据指定格式来格式化日期String format = localDateTime.format(dateTimeFormatter);System.out.println(format); // 2021-07-18// 根据自定义格式来格式化日期DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");String format1 = dtf2.format(localDateTime);System.out.println(format1); // 2021年07月18日 08:59:28// 将字符串解析回时间日期CharSequence text;LocalDateTime newDateTime = localDateTime.parse(format1, dtf2);System.out.println(newDateTime); // 2021-07-18T08:59:28}
/*** 对时区的处理* ZoneDate、ZonedTime、ZonedDateTime*/@Testpublic void test7() {// 指定时区来获取时间LocalDateTime localDateTime = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));System.out.println(localDateTime); // 2021-07-18T09:07:19.661// 构建一个带时区的时间LocalDateTime localDateTime1 = LocalDateTime.now();ZonedDateTime zonedDateTime = localDateTime1.atZone(ZoneId.of("Asia/Shanghai"));System.out.println(zonedDateTime); // 2021-07-18T09:07:19.670+08:00[Asia/Shanghai]}
三、重复注解与类型注解
Java 8 对注解提供了两点改进:可重复的注解及可用于类型的注解。
1、重复注解
/*** 自定义注解*/@Repeatable(MyAnnotations.class) // 若想定义重复注解,必须用@Repeatable修饰,然后注入此注解的容器类@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})@Retention(RetentionPolicy.RUNTIME)public @interface MyAnnotation {String value() default "hgk";}
/*** 创建一个容器*/@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})@Retention(RetentionPolicy.RUNTIME)public @interface MyAnnotations {MyAnnotation[] value();}
```java public class AnnTest {
@Test public void test() throws NoSuchMethodException {
Class<AnnTest> clazz = AnnTest.class;Method showMethod = clazz.getMethod("show");MyAnnotation[] mas = showMethod.getDeclaredAnnotationsByType(MyAnnotation.class);for (MyAnnotation ma : mas) {System.out.println(ma.value());/*** 输出:* Hello* World*/}
}
@MyAnnotation(“Hello”) @MyAnnotation(“World”) public void show() { }
} ```
