java时间范围,确定Java中时间范围表示的天数

Using Java 8 time I am simply trying to figure out the number of days represented in a time range. Consider the following:

LocalDate start = LocalDate.of(2016, Month.MARCH, 28);

LocalDate end = LocalDate.of(2016, Month.MARCH, 31);

Period period = Period.between(start, end);

The number of days in period is 3 which represents the number of days between the 2 dates, inclusive of start and exclusive of end. What I want is the number of days represented by the 2 dates which is actually 4 (March 28, March 29, March 30, March 31).

I know I can just add 1 to the number of days returned from Period.between() but I guess I was surprised that I couldn't find another call to return exactly what I want. Am I missing something or is adding 1 the only solution?

解决方案

tl;dr

Always define your spans of time by the Half-Open approach where:

Beginning is inclusive.

Ending is exclusive.

When you want the four days of March 28, March 29, March 30, March 31, make the beginning March 28 and the ending April 1. Run through those dates starting at the first (March 28th) while going up to, but not including, the last (April 1st).

2016-03-28/2016-04-01

Half-Open

Am I missing something

You may be missing an appreciation for the usefulness of the Half-Open approach in defining spans of time.

Generally, the best practice for defining spans of time is the Half-Open approach. In Half-Open, the beginning is inclusive while the ending is exclusive.

This approach solves the problem of dealing with fractional seconds. Intuitively, many programmers will try to find the last possible moment as the ending of a span of time. But that last moment involves an infinitely divisible last second. You might think, "Well, just go to three decimal place for milliseconds, 12:59.59.999 for the end of noon lunch break, as that is all the resolution I will ever need, and that is the resolution of the legacy java.util.Date class.”. But then you would fail to find matches in your database like Postgres that store date-time values with a resolution of microseconds, 12:59:59.999999. So you decide to use six decimal places of fraction, x.999999. But the start experiencing mismatches with date-time values in the java.time classes, and you learn the offer a resolution of nanoseconds for nine digits of fractional second, x.999999999. You can disembark this carousel of frustrating bugs by using the Half-Open approach where the ending runs up to, but does not include, the next whole second.

I believe you will find consistent use of the Half-Open approach throughout your date-time handling code (whether fractional seconds may be involved or not) will:

Make your code easier to read and comprehend.

Ease the cognitive load overall.

Knowing all your spans of time carry the same definition eliminates ambiguity.

Reduce bugs.

Examples:

A noon lunch period starts at the moment the clock strikes noon (12:00:00) and runs up to, but does not include, the moment when the clock strikes one o’clock. That means 12:00:00 to 13:00:00.

A full day starts at the first moment of the day (not always 00:00:00, by the way) and runs up to, but does not include, the first moment of the following day.

A week starts on a Monday and runs up to, but does not include, the following Monday. That means seven days in Monday-Monday.

A month starts of the first of the month and runs up to, but not including, the first of the following month. So the month of March is March 1 to April 1.

kE3r8.png

LocalDate

Rather than one adding 1 to get a total of days, define your span of time as Half-Open: beginning-is-inclusive, ending-is-exclusive. If you are trying to represent the four dates of March 28, 29, 30, and 31, then I suggest you define a span of time from March 28 to April 1.

LocalDate start = LocalDate.of( 2016, Month.MARCH, 28 ) ; // inclusive

LocalDate stop = LocalDate.of( 2016, Month.APRIL, 1 ) ; // exclusive

Period

The java.time classes wisely use the Half-Open approach. So the Period.between method treats the ending as exclusive, as noted in the Question. I suggest you go-with-the-flow here rather than fight it. Search Stack Overflow for many more examples of how well this approach works.

Period p = Period.between( start , stop );

p.toString(): P4D

ChronoUnit

If you want a total number of days, such as 45 for a month and a half, use the ChronoUnit enum, an implementation of TemporalUnit. See this Question for discussion.

Again, the java.time classes use the Half-Open approach. So

long daysBetween = ChronoUnit.DAYS.between( start, stop );

4

Live code

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值