Create an int variable that recognizes numbers in time format

Asked

Viewed 99 times

0

I have a variable int that is taking the current time of android and comparing with more dua variables int of my database in the parse that contains the opening and closing time of a company, in companies of commercial schedule works, but when a company opens day and closes at dawn for example, I already have a problem, because if a company closes at 1:00 and it 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 for example I use some code that makes recognize, 00 < 01 < 02 < 03, and so on, just as the hours are recognized in a normal watch.

  • What value parseUser4.get("horaAbertura").toString() or parseUser5.get("horaFechamento").toString() ?

  • The "opening hours" contain the opening hours of the company, for example 0700, as well as the "opening hours" contains the closing hours, for example 1730.

1 answer

1


Separate consideration should be given to cases where the timetable is changed from one day to the next.

In your code if the time is 1730 as 0100 so if we consider the hours 1800 he will enter the second if:

else if (horaAtual > horarioFechado) {

Because 1800 is greater than 100 and will present FECHADO.

To solve this problem just add one more block of if for when the time goes from one day to the next, so:

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

  • Boy, I don’t know how to thank you, it worked perfectly, thank you so much for taking the time to help me, hugs!

  • @Alekssanderaugusto You’re welcome, good luck to the rest of the project.

  • Thank you very much, good for you!

Browser other questions tagged

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