本文仅简单总结了java8关于日期时间的package,类,api的用法;
日期,时间,日期时间,时间戳,日期间隔,时间间隔(对应的java8的类):
日期时间术语 |
Java8类表示 |
例子 |
日期 |
LocalDate |
2023-10-05 |
时间 |
LocalTime |
15:23:01.481 |
日期时间 |
LocalDateTime |
2023-10-05 15:23:01 |
时间戳 |
Instant |
2023-10-05T07:53:38.223Z 或1696492418223 |
日期间隔 |
Period |
P1Y4D |
时间间隔 |
Duration |
【补充】
@Test
public void test1_LocalDateTime() {
System.out.println("案例1_LocalDateTime ");
LocalDateTime nowOfLocalDateTime = LocalDateTime.now();
System.out.println(nowOfLocalDateTime); // 2023-10-05T15:41:16.611
// 指定年月日,时分秒
LocalDateTime specificLocalDateTime =
LocalDateTime.of(2023, 10, 5, 11, 49, 1);
System.out.println(specificLocalDateTime);// 2023-10-05T11:49:01
// 日期加减(加2天)
LocalDateTime plusedLocalDateTime = nowOfLocalDateTime.plusDays(2);
System.out.println(plusedLocalDateTime);// 2023-10-07T15:41:16.611
// 减
System.out.println("===================日期时间减法");
System.out.println(nowOfLocalDateTime.plusDays(-2));// 2023-10-03T15:41:16.611
System.out.println(nowOfLocalDateTime.minusDays(2));// 2023-10-03T15:41:16.611
System.out.println(nowOfLocalDateTime);// 2023-10-05T15:41:16.611
}
// 案例1 测试 LocalDate
@Test
public void test1_LocalDate() {
System.out.println("案例1_LocalDate ");
LocalDate nowOfLocalDate = LocalDate.now();
System.out.println(nowOfLocalDate); // 2023-10-05
// 指定年月日
LocalDate specificLocalDate =
LocalDate.of(2023, 10, 5);
System.out.println(specificLocalDate);// 2023-10-05
// 日期加减(加2天)
LocalDate plusedLocalDate = nowOfLocalDate.plusDays(2);
System.out.println(plusedLocalDate);// 2023-10-07
// 减
System.out.println("===================日期时间减法");
System.out.println(nowOfLocalDate.plusDays(-2));// 2023-10-03
System.out.println(nowOfLocalDate.minusDays(2));// 2023-10-03
System.out.println(nowOfLocalDate);// 2023-10-05
}
// 案例1 测试 LocalTime
@Test
public void test1_LocalTime() {
System.out.println("案例1_LocalTime ");
LocalTime nowOfLocalTime = LocalTime.now();
System.out.println(nowOfLocalTime); // 15:50:06.481
// 指定时分秒
LocalTime specificLocalTime =
LocalTime.of(15, 49, 5);
System.out.println(specificLocalTime);// 15:49:05
// 时间加减(加2小时)
LocalTime plusedLocalTime = nowOfLocalTime.plusHours(2);
System.out.println(plusedLocalTime);// 17:50:06.481
// 减
System.out.println("===================时间减法");
System.out.println(nowOfLocalTime.plusHours(-2));// 13:50:06.481
System.out.println(nowOfLocalTime.minusHours(2));// 13:50:06.481
System.out.println(nowOfLocalTime);// 15:50:06.481
}
Instant : 时间戳(以unix元年: 1970年1月1日 00:00:00 到某个时间的毫秒值)
// 案例2 Instant : 时间戳(以unix元年: 1970年1月1日 00:00:00 到某个时间的毫秒值)
@Test
public void test2() {
Instant nowOfInstant = Instant.now(); // 默认获取 UTC 时区
System.out.println(nowOfInstant);// 2023-10-05T07:53:38.223Z
// 计算偏移量
OffsetDateTime offsetDateTime = nowOfInstant.atOffset(ZoneOffset.ofHours(8));
System.out.println(offsetDateTime);// 2023-10-05T15:53:38.223+08:00
// 获取毫秒
System.out.println(nowOfInstant.toEpochMilli()); // 1696492418223
// 时间戳加减
Instant plusedInstant = Instant.ofEpochSecond(1);
System.out.println(plusedInstant); // 1970-01-01T00:00:01Z
}
// 案例3: Duration 计算时间间隔
@Test
public void test3_1_Duration() throws InterruptedException {
Instant instant1 = Instant.now();
TimeUnit.SECONDS.sleep(1);
Instant instant2 = Instant.now();
System.out.println("============ Duration 计算时间间隔-Instant");
Duration duration = Duration.between(instant1, instant2);
System.out.println(duration.toMillis()); // 1014
System.out.println("============ Duration 计算时间间隔-LocalTime");
LocalTime nowOfLocalTime1 = LocalTime.now();
TimeUnit.SECONDS.sleep(1);
LocalTime nowOfLocalTime2 = LocalTime.now();
System.out.println(Duration.between(nowOfLocalTime1, nowOfLocalTime2).toMillis()); // 1010
}
// 案例3: Period: 计算日期间隔
@Test
public void test3_2_Period() throws InterruptedException {
LocalDate localDate1 = LocalDate.of(2022, 10, 1);
TimeUnit.SECONDS.sleep(1);
LocalDate localDate2 = LocalDate.now();
Period period = Period.between(localDate1, localDate2);
System.out.println(period);// P1Y4D
System.out.println(period.getDays()); // 4
System.out.println(period.getMonths()); // 0
System.out.println(period.getYears()); // 1
}
【注意】 Period计算日期间隔的结果是分摊到年月日各个字段上的,比如相差366天,period.getDays()并不等于366,而等于1或0,而period.getYears()等于1;
// 案例5 : TemporalAdjusters 时间校正器
@Test
public void test5() {
LocalDateTime nowOfLocalDateTime = LocalDateTime.now(); // 2023-10-05T12:11:03.085
System.out.println(nowOfLocalDateTime);
LocalDateTime localDateTime2 = nowOfLocalDateTime.withDayOfMonth(10); // 2023-10-10T12:11:03.085
System.out.println(localDateTime2);
// 使用时间们校正器 TemporalAdjusters
// 下一个周日
LocalDateTime adjustedLocalDateTime = nowOfLocalDateTime.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
System.out.println(adjustedLocalDateTime); // 2023-10-08T12:12:58.352
// 自定义下一个工作日(实现 TemporalAdjuster 接口)
LocalDateTime computedLocalDateTime = nowOfLocalDateTime.with(temp -> {
LocalDateTime temp2 = (LocalDateTime) temp;
DayOfWeek dayOfWeek = temp2.getDayOfWeek();
if (dayOfWeek.equals(DayOfWeek.FRIDAY)) {
return temp2.plusDays(3);
}
if (dayOfWeek.equals(DayOfWeek.SATURDAY)) {
return temp2.plusDays(2);
}
return temp2.plusDays(1);
});
System.out.println("nowOfLocalDateTime.with = " + computedLocalDateTime); // 2023-10-06T12:22:27.614
}
// 案例7 ZonedDate ZonedTime ZonedDateTime 时区操作
@Test
public void test7() {
Set availableZoneIds = ZoneId.getAvailableZoneIds();// 获取所有时区
// 指定时区 specificZonedLocalDateTime = 2023-10-05T15:59:09.639
LocalDateTime specificZonedLocalDateTime = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
System.out.println("specificZonedLocalDateTime = " + specificZonedLocalDateTime);// 2023-10-05T15:59:09.639
// 带时区的日期时间 zonedDateTime=2023-10-05T12:42:09.052+08:00[Asia/Shanghai]
ZonedDateTime zonedDateTime = specificZonedLocalDateTime.atZone(ZoneId.of("Asia/Shanghai"));
System.out.println("zonedDateTime=" + zonedDateTime);// 2023-10-05T15:59:09.639+08:00[Asia/Shanghai]
}
// 案例6: DateTimeFormatter : 格式化时间或日期
@Test
public void test6() {
// 格式化日期时间
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_DATE_TIME;
System.out.println(dateTimeFormatter.format(LocalDateTime.now())); // 2023-10-05T16:01:17.284
// 格式化日期
DateTimeFormatter dateFormatter = DateTimeFormatter.ISO_DATE;
System.out.println(dateFormatter.format(LocalDateTime.now())); // 2023-10-05
// 自定义格式,把日期时间对象格式化为字符串
DateTimeFormatter specificDateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDateTimeStr = specificDateTimeFormatter.format(LocalDateTime.now());
System.out.println(formattedDateTimeStr); // 2023-10-05 16:01:17
// 自定义格式,把日期时间字符串解析为日期时间对象
LocalDateTime parsedLocalDateTime = LocalDateTime.parse(formattedDateTimeStr, specificDateTimeFormatter);
System.out.println(parsedLocalDateTime); // 2023-10-05T16:01:17
}