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.