学习笔记-Java8新特性-第七节-新时间与日期API

新时间与日期API

本地时间

  • LocalDate

  • LocalTIme

  • LocalDateTime

简介

  • 人读的时间

  • 提供以ISO-8601为标准的日期和时间

  • 提供简单的日期或时间

  • 并不包含当前的时间信息

  • 也不包含与时区相关的信息

示例

public void test1() {
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDateTime);
// 2023-05-18T15:01:52.140

LocalDateTime localDateTime1 = LocalDateTime.of(2023, 5, 18, 14, 59, 10);
System.out.println(localDateTime1);
// 2023-05-18T14:59:10

LocalDateTime localDateTime2 = localDateTime.plusYears(2);
System.out.println(localDateTime2);
// 2025-05-18T15:01:52.140

LocalDateTime localDateTime3 = localDateTime.minusMinutes(2);
System.out.println(localDateTime3);
// 2023-05-18T14:59:52.140

System.out.println(localDateTime1.getYear() + " "
+ localDateTime1.getMonth() + " "
+ localDateTime1.getDayOfMonth() + " "
+ localDateTime1.getHour() + " "
+ localDateTime1.getMinute() + " "
+ localDateTime1.getSecond()
);
// 2023 MAY 18 14 59 10
}

时间戳

Instant

简介

  • 计算机读的时间

  • 以Unix元年(1970年1月1日 00:00:00)到某个时间之间的毫秒值

  • 默认获取UTC的时间

示例

@Test
public void test2() {
Instant instant = Instant.now();
System.out.println(instant); // 2023-05-18T07:17:56.307Z

// 获取差8个时区的时间偏移量
OffsetDateTime odt = instant.atOffset(ZoneOffset.ofHours(8));
System.out.println(odt); // 2023-05-18T15:17:56.307+08:00

// 转成毫秒时间
System.out.println(instant.toEpochMilli()); // 1684394382019

// 基于元年做运算
Instant instant1 = Instant.ofEpochSecond(60);
System.out.println(instant1); // 1970-01-01T00:01:00Z

}

间隔

  • Duration

    • 两个时间之间的间隔
  • Period

    • 两个本地日期之间的间隔

示例

public void test3() {
Instant instant = Instant.now();

try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("醒了");
}

Instant instant1 = Instant.now();

Duration duration = Duration.between(instant, instant1);
System.out.println(duration.toMillis()); // 1002

System.out.println("----------------------");

LocalDate localDate = LocalDate.of(2015, 1, 1);
LocalDate localDate1 = LocalDate.now();

Period period = Period.between(localDate, localDate1);
System.out.println(period); // P8Y4M17D
}

时间矫正器

  • TemporalAdjuster

    • 时间矫正器

    • 比如,将日期调整到下一个周日

  • TemporaAdiusters

    • 通过静态方法提供了大量的常用TemporalAdjuster

示例

public void test5() {
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDateTime); // 2023-05-18T15:52:42.128

// 将日期改成这个月的第十天
LocalDateTime localDateTime1 = localDateTime.withDayOfMonth(10);
System.out.println(localDateTime1); // 2023-05-10T15:52:42.128

// 将时间调整到下个周五
LocalDateTime localDateTime2 = localDateTime.with(TemporalAdjusters.next(DayOfWeek.FRIDAY));
System.out.println(localDateTime2); // 2023-05-19T15:52:42.128

//自定义:下一个工作日
LocalDateTime localDateTime4 = localDateTime.with((l) -> {
LocalDateTime localDateTime3 = (LocalDateTime) l;
if (localDateTime3.getDayOfWeek() == DayOfWeek.SATURDAY
|| localDateTime3.getDayOfWeek() == DayOfWeek.FRIDAY) {
return localDateTime3.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
} else {
return localDateTime3.plusDays(1);
}
});
System.out.println(localDateTime4); // 2023-05-19T15:52:42.128

}

时间格式化

DateTimeFormatter

示例

public void test6() {
DateTimeFormatter isoDate = DateTimeFormatter.ISO_DATE;
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDateTime); // 2023-05-18T16:04:52.139

String strDate = localDateTime.format(isoDate);
System.out.println(strDate); // 2023-05-18

// 自定义
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
strDate = localDateTime.format(dateTimeFormatter);
System.out.println(strDate); // 2023-05-18

// 从字符串获取日期
LocalDate localDate = LocalDate.parse(strDate, dateTimeFormatter);
System.out.println(localDate); // 2023-05-18

}

时区

  • ZoneDate

  • ZoneTime

  • ZoneDateTime

示例

public void test8() {
// 获得上海的时间
LocalDateTime shanghai = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
System.out.println(shanghai); // 2023-05-18T16:14:02.221
// 带时区的时间
ZonedDateTime shanghaiWithZone = shanghai.atZone(ZoneId.of("Asia/Shanghai"));
System.out.println(shanghaiWithZone); // 2023-05-18T16:14:02.221+08:00[Asia/Shanghai]
}

posted @ 2023-05-21 23:14  0Andl0  阅读(103)  评论(0)    收藏  举报