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 jodatime, 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.
Wouldn’t that be 480 minutes a day (8-hour journey x 60 minutes)? Lunch time is not counted in the journey.
– user28595