Can anyone point me to some Java snippet wherein i can get business (except Sat and Sun) days between two dates.
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
The
startCal.add
should add onto theCalendar.DATE
field, not theCalendar.DAY_OF_MONTH
, I was getting weird results with over Decemeber / January period.This thread is filled with failing solutions... I started by writing a little test file which met my needs, and saw that Roland's both solutions fails, Amir's too. I wanted a solution that uses java 8 and that does not uses loops because, do I have to say why ?
So here's the test file :
And here is my proposed solution, quite simple. Just let java count the number of weeks, multiply by five, and add the number of days needed to compensate the difference ; the only trick is adjusting the start and end to avoid week-ends :
And now I will look stupid if my code fails too :)
Solution without loop:
The
do
while
in the solution of Piyush is wrong, it should be :Using
java 8
it can be easily done, example function:Description:
off-days
in anEnumSet
(weekends in this case).EnumSet
.Note: this function can be optimized, but might be helpful as a starting point.
In groovy: