// LocalDate, LocalDateTime,Date是带时区的,时间戳是从格林威治1970年01月01日00时00分00秒起至现在的总秒数。
1.LocalDate转Date
LocalDate nowLocalDate = LocalDate.now();
Date date = Date.from(localDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant());
2.LocalDateTime转Date
LocalDateTime localDateTime = LocalDateTime.now();
Date date = Date.from(localDateTime.atZone(ZoneOffset.ofHours(8)).toInstant());
3.Date转LocalDateTime(LocalDate)
Date date =newDate();
LocalDateTime localDateTime = date.toInstant().atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
LocalDate localDate = date.toInstant().atZone(ZoneOffset.ofHours(8)).toLocalDate();
4.LocalDate转时间戳
LocalDate localDate = LocalDate.now();
long timestamp = localDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant().toEpochMilli();
5.LocalDateTime转时间戳
LocalDateTime localDateTime = LocalDateTime.now();
long timestamp = localDateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
long timestamp = localDateTime.getTime();
6.时间戳转LocalDate
long timestamp = System.currentTimeMillis();
LocalDate localDate = Instant.ofEpochMilli(timestamp).atZone(ZoneOffset.ofHours(8)).toLocalDate();
7. 时间戳转LocalDateTime
long timestamp = System.currentTimeMillis();
LocalDateTime localDateTime = Instant.ofEpochMilli(timestamp).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
8. 字符串转LocalDate
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate selectDay = LocalDate.parse(selectedDate,format);
9. LocalDate 转 LocalDateTime
LocalDateTime startTime = selectDay.atTime(12, 1, 0);
LocalDateTime temp = selectDay.atStartOfDay();
LocalDateTime endTime = startTime.plusHours(11);
10. LocalDateTime转String
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime localDateTime = LocalDateTime.now();
String dateStr = localDateTime.format(fmt);
11. String转LocalDateTime
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime time = LocalDateTime.now();
String localTime = df.format(time);
LocalDateTime ldt = LocalDateTime.parse("2017-09-28 17:07:05",df);
12. Date转 LocalDate
Date startDate = summaryReq.getStartDate();
Date endDate = summaryReq.getEndDate();
// 计算两个时间之间的天数
long reportTotalDays = 0;
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd");
try {
LocalDate start = startDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
LocalDate end = endDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
reportTotalDays = start.until(end, ChronoUnit.DAYS);
} catch (Exception e) {
e.printStackTrace();
log.info("输入的日期参数错误!");
return null;
}
13. 获取指定时区的日期(时区用zoneIdString: )
LocalDate pairLocalDate = pairDate.toInstant().atZone(ZoneId.of("GMT")).toLocalDate();
ZonedDateTime pairZonedDateTime = pairLocalDate.plusDays(1).atStartOfDay(zoneId); // 获取date转天的0点
Timestamp pairTimestamp = new Timestamp(pairZonedDateTime.toInstant().toEpochMilli());
注意: 控制台打印的时间字符串是当地时区的时间
计算时间间隔:
按年月日分别计算时间差:
LocalDate startDate = LocalDate.of(2015, 2, 20);
LocalDate endDate = LocalDate.of(2017, 1, 15);
// startDate减endDate
Period period = Period.between(startDate, endDate);
Period period = Period.parse("P1Y2M3D");
period.getYears(); // 1
period.getMonths(); // 2
period.getDays(); // 3
2022-1-18 到 2023年6月28号
输出为: 1年, 5月, 10天
按年或月或日计算时间差:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate now = LocalDate.now();
System.out.println("now:"+now);
LocalDate startDate = LocalDate.parse("2021-05-26",formatter);
System.out.println("相差天数:" + startDate.until(now, ChronoUnit.DAYS));
System.out.println("相差月数:" + startDate.until(now, ChronoUnit.MONTHS));
System.out.println("相差年数:" + startDate.until(now, ChronoUnit.YEARS));
2022-1-18 到 2023年6月28号
输出为: 1年, 17个月, 520天(示例)
详细介绍参考地址:
https://knife.blog.csdn.net/article/details/122772818?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-122772818-blog-114585673.235%5Ev29%5Epc_relevant_default_base3&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-122772818-blog-114585673.235%5Ev29%5Epc_relevant_default_base3&utm_relevant_index=2
Date转Timestamp:
LocalDate startLocalDate = startDate.toInstant().atZone(ZoneId.of("GMT")).toLocalDate();
ZonedDateTime startZonedDateTime = startLocalDate.atStartOfDay(zoneId);
Timestamp startTimestamp = new Timestamp(startZonedDateTime.toInstant().toEpochMilli());
Java8中计算时间的四种方式:Period、Duration、ChronoUnit、Until 优秀参考文档
https://blog.csdn.net/qq_43842093/article/details/127200607?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-0-127200607-blog-122772818.235^v29^pc_relevant_default_base3&spm=1001.2101.3001.4242.1&utm_relevant_index=3