1.阅读
1.1Instant
①获取当前时间Instant.now(),得到一个时间戳
②当前的时间戳加上或减取某个Duration
1.2Duration
可以计算两个时间间的差值,计算的结果是 秒+纳秒,分两个字段存储
因此纳秒存储的是说,相差了n秒零纳秒,所以int就可以存储纳秒
1.3Period
1.4ZonedTime ZonedDateTime
1.5LocalTime
1.6LocalDate
1.7LocalDateTime
本来是想整理好的,可是Java核心卷的东西实在看的头疼。我感觉在开发中用到最多的就是
LocalXXX 和 Instant /Period 和 Duration
2.场景练习
2.1Date和Instant和LocalDateTime的转换
Date—-> Instant
Instant instant = Instant.ofEpochMilli(date.getTime());
Instant—>Date
Date date1 = new Date(Instant.now().toEpochMilli());
Date date2 =Date.from(instant)
Date—>LocalDateTime(Date -> Instant -> ZoneDateTime)
LocalDateTime localDateTime1 = Instant.ofEpochMilli(date2.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
LocalDateTime —>Date( LocalDateTime—> ZoneDatetime—> Instant—>Date)
Instant instant1 = now.atZone(ZoneId.systemDefault()).toInstant();
Date from1 = Date.from(instant1);
2.2常用操作
①LocalDateTime获取指定日期 00:00:00 和 23:59:59
@Test
public void testGetMaxAndMin()
{
LocalDateTime localDateTime = LocalDateTime.now();
LocalDateTime minTime = LocalDateTime.of(localDateTime.toLocalDate(), LocalTime.MIN);
System.out.println(minTime);
LocalDateTime maxTime = LocalDateTime.of(localDateTime.toLocalDate(), LocalTime.MAX);
System.out.println(maxTime);
}
②计算A和B的LocalDateTime的相差天数(精确到秒)
@Test
public void testBetween() {
LocalDateTime now = LocalDateTime.now();
System.out.println(now);
LocalDateTime localDateTime = now.plusDays(20);
System.out.println(localDateTime);
Duration between = Duration.between(now, localDateTime);
long l = between.toDays();
System.out.println(l);
}