I need help please, How can I calculate business hours between two dates? For example we have two dates; 01/01/2017 10:00 and 04/01/2017 15:00. And we have working hours 09:30 to 17:30 (8 hours) on weekdays. How can I calculate working hours and minutes with Java?
Thanks for your help.
Off the top of my head.
Let me suggest you begin by studying the
java.time
package with subpackages, the modern Java date and time API. There’s an Oracle tutorial as well as other good reading material on the net, go search.Next I would separate model (domain, business layer) from (user) interface. I am assuming that
01/01/2017 10:00
is an input string from somewhere. I would count this as interface. In your model you will want to use datetime classes and objects for your data.Think about whether you need to take time zone and summer time (DST) into account. Since summer time transitions usually happen in the night and not during working hours, I am thinking not, but make your own decision. If you (may) need this, rely on
ZonedDateTime
objects, otherwiseLocalDateTime
.I would start by writing a good unit test for the working hour calculation. You may also want to write one for the conversion from input strings to either
LocalDateTime
orZonedDateTime
, but no lot of test will be needed here.I would use a
DateTimeFormatter
for parsing your two datetime values intoLocalDateTime
objects. Then.atZone()
if you need to convert toZonedDateTime
.Model/working hour calculation
Inspired from this question I am thinking that it will be convenient to adjust the start and end datetime if they fall outside working hours. For example if the start time is Saturday at 3 AM, move it to Monday at the beginning of the working day. If the end time is Saturday 3 AM, move it to Friday at the end of the working day. You will need methods like
getDayOfWeek
,toLocalDate
,minusDays
,plusDays
andatTime
depending on the exact way you want to do this. You may also want to skip this step, you will see later whether you find it worthwhile. Or maybe only move the time-of-day but not change the day.Next you need the
ChronoUnit
enum. Start for example withChronoUnit.WEEKS.between(startDateTime, endDateTime)
to find the number of full weeks in your interval. Multiply by the number of working hours per week. An option that seems attractive is to store the hours into aDuration
object since I think we will need one later anyway. Also add the weeks to your start time so you have an interval for the remaining work time (days and hours).At this point it is getting more complicated since you need to look at whether the days are working days and at the end and start time of the working day. The
Duration
class may be convenient for summing up the hours, minutes and seconds of each working day from the new start datetime to the end datetime. You may use itsbetween
andplus
methods. You will probably need to handle the case where the new start datetime and the end datetime fall on the same date specially.See if this doesn’t get you started.
everyone! I faced similar problem (calculate working minutes between to TimeStamps) and finished with this solution. Hope, somebody finds it's useful: