Working Hours Calculation - Java

Asked

Viewed 888 times

2

How to calculate the working hours worked, for example, in the company if it works 540 minutes per day, in the system of orders of services I need to compute the hours of service executed, in this scenario:

The service order was opened on: 19/04/2017 09:00 and it was closed on 20/04/2017 at 11:00, but I can only compute and working hours at above the workload of the day worked which is the 540 minutes, someone has some idea of how to do this ?

I can’t just subtract from each other because if it wouldn’t be more than 540 minutes because the workload is from 7:30 to 5:30 so after 5:30 I can’t compute.

  • Wouldn’t that be 480 minutes a day (8-hour journey x 60 minutes)? Lunch time is not counted in the journey.

2 answers

2

I made a system similar to this one, I’ll give you my example, maybe you get some information that can help you, I had the same problem in computing the hours in different days and I ended up doing the following way.

The service order is always opened by reading a bar code (OS number), so when the employee opens an OS, it automatically closes the previous one. There is a table Employees where I have saved the start and end times of each one, as well as their lunch time with start and end.

The tests I have are as follows::

  • If the OS was hit outside of duty hours or within lunch hours, I consider it invalid.

  • If the start date and time of the OS is the previous day: close to OS the previous day at the end of the employee’s working hours and create a new record of the start time of the working day until the current time with the same OS number.

So he worked in that Order of Service, until the end of the working day and resumed at the beginning of the working day until the current time

  • If the start time of OS is before the break time and the current date is after the break, the procedure is pretty much the same. I close the OS at the beginning of the break and create a new record of the end of the break until the current time.

In the same way, he worked at OS until lunch and resumed when he returned until the current time.


To conclude:

So not only do I have the amount of hours an OS was worked, but I also have what hours until the employee worked on it. This way I can provide more specific reports for leaders and managers in the areas.

1

From 07:30 to 17:30 is 600 minutes, so I’m assuming it’s a lunch hour, so the total of the day is 540 minutes.

So we have another problem, which is to know exactly what time it is for lunch so we can deduct it from the total count. For example, if the work order is opened at noon, is the employee considered to be on lunch break and will not start work until 13:00? But what if the clerk has lunch from 11:00 to 12:00?

So I’m going to make a calculation that nay considers lunch time, but then you can adapt the code easily to discount it.

First we set the start and end times of the journey. How you used the tag , I’ll use the class org.joda.time.LocalTime, defining an hour of the day:

LocalTime horaInicio = new LocalTime(7, 30);
LocalTime horaFim = new LocalTime(17, 30);

Note that this class represents only the hours, with no relation to the day, month or year.

Then we create the opening and closing dates of the work order. As we need the date (day, month and year) and the time, we use the class org.joda.time.DateTime:

// 19/04/2017 09:00
DateTime dataAbertura = new DateTime(2017, 4, 19, 9, 0);
// 20/04/2017 11:00
DateTime dataEncerramento = new DateTime(2017, 4, 20, 1, 0);

Then we make a loop starting from the opening date and continuing until the closing date, calculating the time elapsed each day. To accumulate the total time, we can use the class org.joda.time.Minutes, counting a number of minutes.

Minutes totalMinutos = Minutes.ZERO;
DateTime dt = dataAbertura;
// enquanto dt for <= dataEncerramento (ou seja, enquanto não for > que dataEncerramento)
while (!dt.isAfter(dataEncerramento)) {
    DateTime inicioDia = dt;
    // verifica se foi aberto antes do início do dia
    if (inicioDia.toLocalTime().isBefore(horaInicio)) {
        // ajusta o inicio do dia para 07:30
        inicioDia = inicioDia.withTime(horaInicio);
    }

    // fim do dia
    DateTime fimDia = dt.withTime(horaFim);
    // verifica se o dia do encerramento é hoje (compara somente a data)
    if (fimDia.toLocalDate().equals(dataEncerramento.toLocalDate())) {
        fimDia = dataEncerramento;
    }
    // verifica se ultrapassou o fim do dia
    if (fimDia.toLocalTime().isAfter(horaFim)) {
        fimDia = fimDia.withTime(horaFim);
    }

    // calcula os minutos do dia e soma ao total
    totalMinutos = totalMinutos.plus(Minutes.minutesBetween(inicioDia, fimDia));

    // vai para o dia seguinte 07:30
    dt = dt.plusDays(1).withTime(horaInicio);
}

// pegar o total como um inteiro
int total = totalMinutos.getMinutes();
System.out.println(total);

After that you should still discount the lunch hour, if applicable.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.