ChronoUnit.MONTHS.between 不遵循半开方法
ChronoUnit.MONTHS.between not following Half-Open approach
我有 2 个 LocalDate
,我想找出它们之间的区别:
LocalDate date1 = LocalDate.of(2018,11,30);
LocalDate date2 = LocalDate.of(2019, 5, 1);
当我执行时:
long mthsDiff = ChronoUnit.MONTHS.between(date1, date2);
这个return是5,但实际上应该是6。
为什么 ChronoUnit.MONTHS.between
不尊重 Half-Open approach
?不应该是四月 return 个月 (2019-04-30
) 吗?
我错过了什么吗?
我知道第二个参数是排他性的。
以下是我预期的情况:
date1 = 2018-11-16
date2 = 2019-04-15
expect 5
date1 = 2018-11-16
date2 = 2019-04-16
expect 5
date1 = 2018-11-16
date2 = 2019-04-17
expect 6
date1 = 2018-11-16
date2 = 2019-04-15
expect 5
我认为 the documentation 在这里说明了一切:
The calculation returns a whole number, representing the number of
complete units between the two temporals. For example, the amount in
hours between the times 11:30 and 13:29 will only be one hour as it is
one minute short of two hours.
11 月 30 日到 5 月 1 日之间的整月数是 5:
- 十二月
- 1 月
- 二月
- 3 月
- 4 月
为什么你认为它应该是 6?半开就是说第一天包容,最后一天独享。除去 5 月 1 日什么都不做,整个 4 月都还在这个范围内。加上11月30日还不是整月,所以还是5.
正如文档所说 ChronoUnit.between
The calculation returns a whole number, representing the number of complete units between the two temporals.
和
// these two lines are equivalent
between = thisUnit.between(start, end);
between = start.until(end, thisUnit);
所以
之间有5个完整的月份
"2018-11-30"
December -> 1
January -> 2
February -> 3
March -> 4
May -> 5
"2019-05-01"
我有 2 个 LocalDate
,我想找出它们之间的区别:
LocalDate date1 = LocalDate.of(2018,11,30);
LocalDate date2 = LocalDate.of(2019, 5, 1);
当我执行时:
long mthsDiff = ChronoUnit.MONTHS.between(date1, date2);
这个return是5,但实际上应该是6。
为什么 ChronoUnit.MONTHS.between
不尊重 Half-Open approach
?不应该是四月 return 个月 (2019-04-30
) 吗?
我错过了什么吗?
我知道第二个参数是排他性的。 以下是我预期的情况:
date1 = 2018-11-16
date2 = 2019-04-15
expect 5
date1 = 2018-11-16
date2 = 2019-04-16
expect 5
date1 = 2018-11-16
date2 = 2019-04-17
expect 6
date1 = 2018-11-16
date2 = 2019-04-15
expect 5
我认为 the documentation 在这里说明了一切:
The calculation returns a whole number, representing the number of complete units between the two temporals. For example, the amount in hours between the times 11:30 and 13:29 will only be one hour as it is one minute short of two hours.
11 月 30 日到 5 月 1 日之间的整月数是 5:
- 十二月
- 1 月
- 二月
- 3 月
- 4 月
为什么你认为它应该是 6?半开就是说第一天包容,最后一天独享。除去 5 月 1 日什么都不做,整个 4 月都还在这个范围内。加上11月30日还不是整月,所以还是5.
正如文档所说 ChronoUnit.between
The calculation returns a whole number, representing the number of complete units between the two temporals.
和
// these two lines are equivalent
between = thisUnit.between(start, end);
between = start.until(end, thisUnit);
所以
之间有5个完整的月份"2018-11-30"
December -> 1
January -> 2
February -> 3
March -> 4
May -> 5
"2019-05-01"