• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting hours between two LocalDateTime excluding Weekends using Java 8  RSS feed

 
Mahesh Suryawanshi
Greenhorn
Posts: 26
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Folks,

I will have 2 date's including time, i need to find out the number of hours between the dates excluding Weekends.

For sample example



Suppose it it executed today then output will be 72 hours [startDateTime>> 2017-10-27T19:31:49.793 endDateTime>>2017-10-24T19:31:49.793]

Suppose in between this date's Weekend will come then it should exclude that hours from the output.

Let's say it executed in between Monday & Friday it should consider as 24 hours only excluding Sat & Sun Hours.

Please suggest how to solve this problem.

Thank you


 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37507
552
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've got step one. You know the total number of hours. Next you need to find the total number of weekend hours.

For that, you'll need some logic. Any thoughts on how to tell if a day is a weekend? Or how to go through all the dates in the range?
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Surely end would be 2017-10-30...

I am not sure there is anything helpful in the Java™ Tutorials about that. Have you been through the methods of Duration, LocalDateTime and Period to see whether it says anything about weekends? You realise you can get the day of week (Sunday, Monday, Tuesday) from a local date easily enough?
 
Mahesh Suryawanshi
Greenhorn
Posts: 26
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:You've got step one. You know the total number of hours. Next you need to find the total number of weekend hours.

For that, you'll need some logic. Any thoughts on how to tell if a day is a weekend? Or how to go through all the dates in the range?


Thank you for response

Yeah, thats the tricky part to find weekend hours.

What i think is, May be need to check that given date is weekend or not if yes then add hours if not then skip.

I need to work on that. to find the concrete solution.
 
Mahesh Suryawanshi
Greenhorn
Posts: 26
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Surely end would be 2017-10-30...

I am not sure there is anything helpful in the Java™ Tutorials about that. Have you been through the methods of Duration, LocalDateTime and Period to see whether it says anything about weekends? You realise you can get the day of week (Sunday, Monday, Tuesday) from a local date easily enough?


Thank you for response

There a method to check the day something like dateTime.getDayOfWeek().getValue(), but i need to consider the day with time so that i can exclude from the final hours.
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My algorithm is like this:

Consider each date between the start and finish. There are three cases:

1. If the date is the start date, then add the number of hours after the start time.

2. Otherwise, if the date is the end date, then add the number of hours before the end time.

3. Otherwise, if the date is a weekday then add 24 hours.

However there are a couple of situations not covered by this:

1. If the start date and end date are the same date, you have to use a different rule.

2. In rule #3 above, if the date is the start or end of Daylight Saving Time then 24 isn't the right number to add. Now, all places I know which use DST start and end it on Sunday, but there may be some exceptions which I don't know about.

3. If the start date is Saturday or Sunday then rule #1 above doesn't apply. Ditto for the end date.

4. I'm sure I haven't thought of everything -- oh yeah, if the start date is after the end date you should probably throw an exception -- and there may be more issues.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!