简体   繁体   English

计算包括两个给定日期的天数

[英]Count the number of days including the two given dates

How to count the number of days between two LocalDate 's in java? 如何计算Java中两个LocalDate之间的天数? I tried like this 我尝试过这样

LocalDate startDate = LocalDate.parse("2016-11-02");
LocalDate endDate = LocalDate.parse("2016-11-04");

long days = startDate.until(endDate, ChronoUnit.DAYS);

//and

long days= ChronoUnit.DAYS.between(startDate, endDate);

//and

long days= Duration.between(startDate.atStartOfDay(), endDate.atStartOfDay()).toDays();

All above are giving days between two dates, but how to count inclusive of both dates. 以上所有都给出了两个日期之间的天数,但是如何计算包括两个日期在内的日期。 As per above steps, getting 2 days as no. 按照上述步骤,获得2天否。 of days, but required 3 days. 天,但需要3天。

The javadoc is clear that the end date is not included in the calculation. Javadoc很清楚,结束日期不包括在计算中。 So you can simply add 1: 因此,您只需添加1:

long days = ChronoUnit.DAYS.between(startDate, endDate) + 1;

If you want literal 24 hour days, you can use the Duration class instead: 如果您想要字面上的24小时工作日,可以改用Duration类:

long days = Duration.between(startDate.atStartOfDay(), endDate.atStartOfDay()).toDays() + 1;

For more information, please see this document regarding Java SE 8 Date and Time. 有关更多信息,请参阅有关Java SE 8日期和时间的文档

tl;dr TL;博士

Beginning being inclusive , and ending being exclusive in a span of time is the norm. 在一段时间内开始具有包容性 ,结束具有排他性是规范。

If you absolutely cannot follow that approach, add one. 如果您绝对不能遵循该方法,请添加一个。

days = days + 1 ;

Half-Open 半开

A common and wise way to define a span of time is the Half-Open approach. 定义时间跨度的一种常见且明智的方法是半开放式方法。 The beginning is inclusive while the ending is exclusive . 开始是包容性的,而结尾是排他性的

Sometimes, but not always, we use this approach intuitively in everyday life. 有时但并非总是如此,我们在日常生活中会直观地使用此方法。 For example, a classroom's lunch period is said to be noon to 1 PM which means all the students are to be returned to class and ready before the class strikes 13:00. 例如,教室的午餐时间据说是中午至下午1点,这意味着所有学生都必须在上课时间13:00 之前返回课堂并准备就绪。 Class is said to run 1 PM to 2 PM. 据说上课时间是下午1点到下午2点。 So the spans can abut one another without overlapping, and without the tricky task of determining the last moment of an infinitely divisible last second of last minute of that hour. 因此,跨度可以彼此邻接而不重叠,也不需要确定该小时最后一分钟的无限可分最后一秒的最后一刻的棘手任务。

I suggest following this approach in all your date-time coding will make your code easier to read, easier to debug, and less error-prone by reducing the cognitive overload of resolving the inclusive-exclusive ambiguity. 我建议在您的所有日期时间编码中都采用这种方法,通过减少解决包容性-歧义性的认知负担,可以使您的代码更易于阅读,更易于调试并且不易出错。

The java.time classes use the Half-Open approach throughout. java.time类始终使用Half-Open方法。

long days = ChronoUnit.DAYS.between( start , stop ) ;  // Half-open

If you code must return an ending-inclusive result, just add one to the java.time results. 如果您的代码必须返回包含结尾的结果,则只需在java.time结果中添加一个即可。

long daysClosed = ( days + 1 ) ;

Ideally you would use Half-Open consistently in your code and in your user interface, while educating your users as to the issue of ambiguity. 理想情况下,您应该在代码和用户界面中一致地使用Half-Open,同时教育用户有关歧义性的问题。 I have seen countless mistakes made by businesspeople wrongly assuming that date ranges were open () , closed [] , or half-open [) , and even occasionally the opposite half closed (] while the author meant another. 我已经看到商人错误地假设日期范围是open () ,close []或half-open [) ,甚至偶尔有相反的一半close (]而作者则指的是另一个错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM