两个日期范围内年,月,日计算遇到的一个问题
Date startDate = new Date(); Date endDate = new Date(); Instant instantStart = startDate.toInstant(); ZoneId startZone = ZoneId.systemDefault(); // atZone()方法返回在指定时区从此Instant生成的ZonedDateTime。 LocalDate startLocalDate = instantStart.atZone(startZone).toLocalDate(); Instant instantEnd = endDate.toInstant(); ZoneId endZone = ZoneId.systemDefault(); // atZone()方法返回在指定时区从此Instant生成的ZonedDateTime。 LocalDate endLocalDate = instantEnd.atZone(endZone).toLocalDate();
//计算
Period period = Period.between(startLocalDate, endLocalDate);
//获得年月日计算结果
int year = period.getYears() int month = period.getMonths() int day = period.getDays()
当两个日期为
"startDate":"2021-12-01",
"endDate":"2022-12-01"
我以为month会等于12,结果为0

浙公网安备 33010602011771号