Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Getting hours between two LocalDateTime excluding Weekends using Java 8

 
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


 
author & internet detective
Posts: 40035
809
Eclipse IDE VI Editor Java
  • 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?
 
Marshal
Posts: 69763
277
  • 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.
 
Marshal
Posts: 25669
69
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.
 
Gravity is a harsh mistress. But this tiny ad is pretty easy to deal with:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic