Java8中使用java.time.temporal.ChronoUnit辅助类计算两个ZonedDateTime日期对象的间隔月、间隔天数、间隔小时、间隔分钟数
作者:互联网
直接贴代码:
import java.time.temporal.ChronoUnit;
@Test
public void test计算两个ZonedDateTime日期的间隔时间() {
ZonedDateTime start = ZonedDateTime.of(2022,3,27,10,0,0,0, ZoneId.systemDefault());
ZonedDateTime end = ZonedDateTime.of(2022,3,29,10,0,0,0, ZoneId.systemDefault());
long betweenYears = ChronoUnit.YEARS.between(start, end); // 间隔年数
long betweenMonths = ChronoUnit.MONTHS.between(start, end); // 间隔月数
long betweenDays = ChronoUnit.DAYS.between(start, end); // 间隔天数
long betweenHours = ChronoUnit.HOURS.between(start, end); // 间隔小时
long betweenMinutes = ChronoUnit.MINUTES.between(start, end); // 间隔分钟
long betweenSeconds = ChronoUnit.SECONDS.between(start, end); // 间隔秒
log.info("间隔天数 = {}, 间隔小时 = {}, 间隔分钟 = {}", betweenDays, betweenHours, betweenMinutes);
}
end.
标签:end,java,间隔,long,start,between,ChronoUnit,temporal 来源: https://www.cnblogs.com/zhuwenjoyce/p/16062514.html