Calculating working hours - Java

2

How to calculate the working hours worked, for example, in the company if you work 540 minutes a day, in the order system of services I need to compute the hours of service performed, in this scenario:

The work order was opened on 04/19/2017 09:00 and it was closed on 04/20/2017 at 11:00 AM, but I can only compute and working hours on hours worked, which is 540 minutes, someone has any idea how to do this?

I can not just subtract one from the other because if it would not give more than 540 minutes because the workload it is from 07:30 to 17:30 then after 17:30 I can not compute.

    
asked by anonymous 19.04.2017 / 15:43

2 answers

1

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

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

The tests I have are as follows:

  • If the OS was knocked out of business hours or within the lunch hour, I consider it to be invalid.

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

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

  • If the OS start time is before the interval time and the current date is after the interval, the procedure is practically the same. I close the OS at the beginning of the interval and create a new record from the end of the interval to the current time.

Likewise, he worked on OS until he went to lunch and picked up when he came back until the current time.

To conclude:

So not only do I have the number of hours an OS was worked, but I also have what hours until what time the employee worked on it. So I can provide more specific reports to the leaders and managers of the areas.

    
19.04.2017 / 16:02
1

From 07:30 to 17:30 it is 600 minutes, so I am assuming that it is discounted one hour of lunch, so that the total of the day is 540 minutes.

With this we have another problem, which is knowing exactly the lunch time so we can deduct from the total count. For example, if the service order is opened at noon, does the employee be considered to be at lunchtime and will only start work at 1:00 p.m.? But what if the employee has lunch from 11:00 am to noon?

So I'm going to do a calculation that does not consider lunch time, but then you can easily adapt the code to cash it.

First we define the start and end times of the journey. Since you have used the jodatime tag, I will use the org.joda.time.LocalTime class, which defines a time of 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 for the service order. Since we need the date (day, month and year) and 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);

Next we loop from the opening date to the closing date, calculating the time elapsed each day. To accumulate the total time, we can use the class org.joda.time.Minutes , which counts a quantity 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 lunchtime, if that is the case.

    
04.05.2018 / 14:09