计算LocalDateTime相差天数
项目中mysql数据库涉及时间一般用datetime类型,java中涉及时间用LocalDateTime类型。
计算两个LocalDateTime类型之间的相差天数使用方法为:
最开始使用Period.between()方法计算,只能计算相同月份的相差天数:
//只能计算相同月之间相隔的天数
int daysNum = Period.between(O.getStartTime().toLocalDate(), O.getEndTime().toLocalDate()).getDays();
优化后使用toEpochDay()方法为:
int daysNum=(int)(o.getEndTime().toLocalDate().toEpochDay() - o.getStartTime().toLocalDate().toEpochDay());
遍历时间天数如下:
for(int i = 0 ; i <=daysNum; i++){
LocalDateTime betweenTime = o.getStartTime().plusDays(i);
}
还没有评论,来说两句吧...