Create an int variable that recognizes numbers in time format

0

I have an int variable that is picking up the current android time and comparing with more dua int variables from my database in the parse that contains the opening and closing times of a company in business hours trading works, but when a company opens day and day dawn for example, I already have problem, because if a company closes at 1:00 and is 00:00, the application recognizes that it is closed.

SimpleDateFormat sdf = new SimpleDateFormat("HHmm", Locale.getDefault());
Date hora = Calendar.getInstance().getTime();

int horaAtual = Integer.parseInt((sdf.format(hora)));

int horarioAberto = Integer.parseInt(parseUser4.get("horaAbertura").toString());

int horarioFechado = Integer.parseInt(parseUser5.get("horaFechamento").toString());


if (horaAtual < horarioAberto) {
    horario.setText("FECHADO");
    horario.setTextColor(getContext().getColor(R.color.vermelho));
} else if (horaAtual > horarioFechado) {
    horario.setText("FECHADO");
    horario.setTextColor(getContext().getColor(R.color.vermelho));
} else if (horaAtual >= horarioAberto) {
    horario.setText("ABERTO");
    horario.setTextColor(getContext().getColor(R.color.verde_limao));
} else if (horaAtual <= horarioFechado) {
    horario.setText("ABERTO");
    horario.setTextColor(getContext().getColor(R.color.verde_limao));
}

I wanted to know if you have, for example, I use some code to recognize, 00 < 01 < 02 < 03, and so on, just as hours are recognized on a normal clock.

    
asked by anonymous 09.09.2017 / 09:17

1 answer

1

It is necessary to consider separately cases where the time passes from one day to the next.

In your code if time is from 1730 to 0100 so if we consider the hours 1800 it will enter the second if :

else if (horaAtual > horarioFechado) {

Because 1800 is greater than 100 and will display FECHADO .

To solve this problem, simply add another block of if to the time from one day to the next, like this:

if (horarioAberto < horarioFechado){ //horario normal
    if (horaAtual < horarioAberto || horaAtual > horarioFechado){ //if igual ao que tinha
        System.out.println("FECHADO");
    }
    else {
        System.out.println("ABERTO");
    }
}
else { //horario que passa o dia
    //teste especifico e diferente para quando passa o dia
    if (horaAtual >= horarioAberto || horaAtual <= horarioFechado){
        System.out.println("ABERTO");
    }
    else {
        System.out.println("FECHADO");  
    }
}

See the tests of this logic in Ideone

    
10.09.2017 / 17:59