LocalDate、LocalDateTime与timestamp、Date的转换

    <!-- 文章内容 -->
<div data-note-content="" class="show-content">
<div class="show-content-free">
<h4>1.LocalDate转Date</h4>
LocalDate nowLocalDate = LocalDate.now();
Date date = Date.from(nowLocalDate.atStartOfDay(ZoneId.systemDefault()).toInstant());

2.LocalDateTime转Date

LocalDateTime localDateTime = LocalDateTime.now();
Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());

3.Date转LocalDateTime(LocalDate)

Date date = new Date();
LocalDateTime localDateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

4.LocalDate转时间戳

LocalDate localDate = LocalDate.now();
long timestamp = localDate.atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli();

5.LocalDateTime转时间戳

LocalDateTime localDateTime = LocalDateTime.now();
long timestamp = localDateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();

6.时间戳转LocalDateTime(LocalDate)

long timestamp = System.currentTimeMillis();
LocalDate localDate = Instant.ofEpochMilli(timestamp).atZone(ZoneId.systemDefault()).toLocalDate();
LocalDateTime localDateTime = Instant.ofEpochMilli(timestamp).atZone(ZoneId.systemDefault()).toLocalDateTime();
      </div>
</div>

原文地址:https://www.jianshu.com/p/b4629857fc6f

05-11 13:42