java - java中月份两个日期之间的差异
问题描述
关于计算月份的查询,但有一些条件。使用 Joda 日期时间或日期工具。
Start Date : 01/01/2018
End Date : 31/12/2020
上述日期之间的总期间差异:36 months and 0 days
总计month =36
Start Date : 01/01/2018
End Date : 02/01/2021
上述日期之间的总期间差异:36 months and 2 days
。如果还有天数,则考虑单月。所以total month 36+1= 37
Date issueDate1=03/06/2017;
Date dateTo1=02/06/2020;
int investmentPeriod = Months.monthsBetween(issueDate1, dateTo1).getMonths();
上面几个月的jodacoming 35
是错误的。
开始日期=23/06/2017;
日期结束=06/07/2017;
这里相差不到一个月。所以它被认为是单月。
解决方案
ZoneId defaultZoneId = ZoneId.systemDefault();
String issueDate1="01/01/2017";
Date issueDate2=new SimpleDateFormat("dd/MM/yyyy").parse(issueDate1);
String dateTo1="31/12/2018";
Date dateTo2=new SimpleDateFormat("dd/MM/yyyy").parse(dateTo1);
这里年月日很容易找到。这给出了所有问题的答案。
Instant instant = issueDate2.toInstant();
LocalDate localDatestart = instant.atZone(defaultZoneId).toLocalDate();
Instant instant1 = dateTo2.toInstant();
LocalDate localDateend = instant1.atZone(defaultZoneId).toLocalDate().plusDays(1);
Period diff = Period.between(localDatestart, localDateend);
System.out.printf("\nDifference is %d years, %d months and %d days old\n\n",
diff.getYears(), diff.getMonths(), diff.getDays());
推荐阅读
- python - Scrapy:忽略扩展的规则不起作用
- angular - 如何从 Angular Material 扩展面板切换 openAll() 或 closeAll() (按钮)?
- python - 训练多维数据时出错
- javascript - DataTables 默认按数字排序
- ruby - 对 RUBY 真的很陌生,希望得到一些指导
- python - 消除 DataFrame 中键的重复值
- reporting-services - SSRS_条件总和
- python - 如何在 Dijkstra 的算法中实现 end_ver?
- yocto - 在 Yocto 中,我如何选择我的补丁应用于多个源中的哪一个?
- python - 测试测试数据记录列表中的所有条目