两个日期范围内年,月,日计算遇到的一个问题

复制代码
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



posted @   时光里的少年  阅读(19)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示