行业知识
Java获取两个日期之间的时间间隔
2026-10-31 15:18:48
来源:互联网转载
public BigDecimal selectHoliday(Date workDate) { if(StringUtils.isEmpty(workDate.toString())) { return new BigDecimal(0); } // 获取当前年月 LocalDate currentDate = LocalDate.now(); // 计算员工连续工作的年数 Period period = Period.between(workDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(), currentDate); int workYears = period.getYears(); return new BigDecimal(workYears); } 以下为period 的返回类型
- `Period`类是Java日期时间API中用于表示日期之间的时间间隔的类。
- `Period.between(startdate, enddate)`是一个静态方法,用于计算两个`LocalDate`对象(即日期对象)之间的时间间隔。`startdate`和`enddate`是两个`LocalDate`对象,代表了开始日期和结束日期。
- 计算的结果是一个`Period`对象,表示了从`startdate`到`enddate`之间的年、月、日的时间间隔。
例如,如果`startdate`是2022年1月1日,`enddate`是2022年12月31日,则`Period`对象可能会表示为1年的时间间隔,因为这两个日期之间相差一年。
上一篇:勇士服务器怎么改密码?